[转自] http://blog.csdn.net/majian_1987/article/details/8725197
LZ在项目中需要处理这样一个业务,每天都有终端设备上传GPS位置信息到服务端,服务端要把GPS位置信息保存在本地,因为每天上传的GPS数据信息会有很多,
所以要把GPS数据按天分别存放在不同的表中,如2013年3月27日上传的GPS数据保存在Disa_GPS_20130327表中,而2013年3月28日上传的GPS数据保存在Disa_GPS_20130328表中,依次类推。这些表的数据结构完全一样,所以LZ想用一个实体去动态映射这些结构相同的表。在网上查了些资料,结果都是需要xml映射文件的,
而LZ用的是hibernate3.x的注解方式实现的映射,当时随便试了下,也没有调通(很惭愧,如果有研究注解方式可以实现的朋友,麻烦共享下,先在这里谢谢了)。所以,又另寻它法。后来在网上又找到了一个通过HQL的基础,SQLQuery实现的动态映射方法,经过LZ实验,终于调通。在此,与各位共享下。
【注】:DIsaGpsdata是实体类,里面要写注解或配置hbm.xml文件。
DisaGpsdata.java:
package com.supermap.earth.server.hibernate.disaster; import java.math.BigDecimal; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity @Table(name = "DISA_GPSDATA", schema = "EARTH_DISASTER") public class DisaGpsdata implements java.io.Serializable { private String id; private String temId; private String earthId; private String userId; private Double lng; private Double lat; private Double secretLng; private Double secretLat; private Date gpsTime; private Double gpsDir; private Double gpsSpeed; public DisaGpsdata() { } public DisaGpsdata(String id, String temId, Double lng, Double lat, Double secretLng, Double secretLat, Date gpsTime, Double gpsDir, Double gpsSpeed) { super(); this.id = id; this.temId = temId; this.lng = lng; this.lat = lat; secretLng = secretLng; secretLat = secretLat; this.gpsTime = gpsTime; this.gpsDir = gpsDir; this.gpsSpeed = gpsSpeed; } public DisaGpsdata(String id, String temId, String earthId, String userId, Double lng, Double lat, Double secretLng, Double secretLat, Date gpsTime, Double gpsDir, Double gpsSpeed) { super(); this.id = id; this.temId = temId; this.earthId = earthId; this.userId = userId; this.lng = lng; this.lat = lat; secretLng = secretLng; secretLat = secretLat; this.gpsTime = gpsTime; this.gpsDir = gpsDir; this.gpsSpeed = gpsSpeed; } @Id public String getId() { return id; } public void setId(String id) { this.id = id; } @Column(name="TEM_ID") public String getTemId() { return temId; } public void setTemId(String temId) { this.temId = temId; } public Double getLng() { return lng; } public void setLng(Double lng) { this.lng = lng; } public Double getLat() { return lat; } public void setLat(Double lat) { this.lat = lat; } @Column(name="SECRET_LNG") public Double getSecretLng() { return secretLng; } public void setSecretLng(Double secretLng) { secretLng = secretLng; } @Column(name="SECRET_LAT") public Double getSecretLat() { return secretLat; } public void setSecretLat(Double secretLat) { secretLat = secretLat; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "GPS_TIME") public Date getGpsTime() { return gpsTime; } public void setGpsTime(Date gpsTime) { this.gpsTime = gpsTime; } @Column(name = "GPS_DIR") public Double getGpsDir() { return gpsDir; } public void setGpsDir(Double gpsDir) { this.gpsDir = gpsDir; } @Column(name = "GPS_SPEED") public Double getGpsSpeed() { return gpsSpeed; } public void setGpsSpeed(Double gpsSpeed) { this.gpsSpeed = gpsSpeed; } @Column(name = "EARTH_ID") public String getEarthId() { return earthId; } public void setEarthId(String earthId) { this.earthId = earthId; } @Column(name = "USER_ID") public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } }
1.新增操作:
public void addDisaGpsData(DisaGpsdata data, String tabName) throws Exception { Transaction tx = session.beginTransaction(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date date = new Date(); String sql = "insert into disa_gpsdata_" + sdf.format(date) + " (id, tem_id, lng, lat, gps_time, gps_dir, gps_speed, earth_id, user_id, secret_lng, secret_lat)" + " values (?, ?, ?, ?, ?, ?, ?, ?, ?,?,?)"; SQLQuery query = session.createSQLQuery(sql); query.setString(0,data.getId()); query.setString(1,data.getTemId()); query.setDouble(2,data.getLng()); query.setDouble(3,data.getLat()); query.setDate(4,data.getGpsTime()); query.setDouble(5,data.getGpsDir()); query.setDouble(6,data.getGpsSpeed()); query.setString(7,data.getEarthId()); query.setString(8,data.getUserId()); query.setDouble(9,data.getLng()); query.setDouble(10,data.getLat()); try { query.executeUpdate(); } catch (Exception e) { tx.rollback(); e.printStackTrace(); } tx.commit(); }
2.删除操作:
public void removeDisaGpsDataById(String id, String tabName) throws Exception { Transaction tx = session.beginTransaction(); String sql = "delete " + tabName + " where id = ?"; SQLQuery query = session.createSQLQuery(sql); query.setString(0,id); try { query.executeUpdate(); tx.commit(); } catch (Exception e) { tx.rollback(); e.printStackTrace(); } }
3.查询操作:
注:这里写的是一个DEMO,
public List<DisaGpsdata> queryDisaGpsdatasByPara(Page page, Map map, String tabName) throws Exception { List<DisaGpsdata> datas = null; String sql = "select * from " + tabName + " where id = ?"; SQLQuery query = session.createSQLQuery(sql); query.addEntity(DisaGpsdata.class); query.setString(0,(String)map.get("id")); try { datas = query.list();//返回多条记录的结果集,如果返回一条记录的调用query.uniqueResult(); } catch (Exception e) { e.printStackTrace(); } return datas; }
【总结】:执行查询操作时,需要把查询出来的结果集和实体之间建立映射关系:通过query.addEntity(DisaGpsdata.class);就可以建立表和DisaGpsdata之间的映射关系。
以上便可以实现一个实现DisaGpsdata和多张表之间的动态映射功能。资料参考了【有思想的代码:http://wujuxiang.blog.51cto.com/2250829/403693】,感谢有思想的代码。
另:HibernateSessionFactory类是通过hibernate.cfg.xml配置文件得到的Session工厂,所以要保证hibernate.cfg.xml中一定要含有DisaGpsdata类的映射信息,LZ项目中用的是注解方式,所以在hibernate.cfg.xml中配置的是:
<mapping class="com.supermap.earth.server.hibernate.disaster.DisaGpsdata"/>
如果项目中用的是hbm.xml来映射实体和表之间的关系,则用<mapping resource="*.hbm.xml"/>来引入DisaGpsdata类的映射配置文件,否则报错:org.hibernate.MappingException: Unknown entity:com.supermap.earth.server.hibernate.disaster.DisaGpsdata,这点挺重要,LZ就犯了这个错误,费了好久才找到原因!