关于hibernate反向生成实体类的问题-没有主键的表映射
来源:互联网 发布:101是什么意思 知乎 编辑:程序博客网 时间:2024/06/05 20:11
问题描述:在使用hibernate反向工具的时候,生成的无主键实体类,在查询时结果为空的问题
特注:(本篇文章是针对于那些使用的Spring+Springmvc+hibernate开发框架的同学们的)
通常情况下,使用hibernate反向工具的时候,系统并不会提示你有哪几个表无主键,而是直接将无主键的对应表对应生成两个class文件(分别是xxx.class和xxxid.class [xxx : 对应的表名]),xxx.class中会给你无主键的表定义一个唯一的Id,然后通过映射文件将表中的所有字段整合为一个复合主键以供hibernate使用。
1.xxx.class示例:
/** * BlTmsRecipientsTab generated by hbm2java */public class BlTmsRecipientsTab implements java.io.Serializable {private BlTmsRecipientsTabId id;public BlTmsRecipientsTab() {}public BlTmsRecipientsTab(BlTmsRecipientsTabId id) {this.id = id;}public BlTmsRecipientsTabId getId() {return this.id;}public void setId(BlTmsRecipientsTabId id) {this.id = id;}public String getObjectId(){return this.id.getObjectId();}public void setObjectId(String objectId) {this.id.setObjectId(objectId);}public Date getModiDate() {return this.id.getModiDate();}public void setModiDate(Date modiDate) {this.id.setModiDate(modiDate);;}public String getModiUser() {return this.id.getModiUser();}public void setModiUser(String modiUser) {this.id.setModiUser(modiUser);;}public BigDecimal getDepartmentId() {return this.id.getDepartmentId();}public void setDepartmentId(BigDecimal departmentId) {this.id.setDepartmentId(departmentId);;}public BigDecimal getSubcompanyId() {return this.id.getSubcompanyId();}public void setSubcompanyId(BigDecimal subcompanyId) {this.id.setSubcompanyId(subcompanyId);;}}2.xxxId.class文件示例:
/** * BlTmsRecipientsTabId generated by hbm2java */public class BlTmsRecipientsTabId implements java.io.Serializable {private BigDecimal questId;private BigDecimal questdispenseId;private String objectId;private String enterUser;private Date enterDate;private Date modiDate;private String modiUser;private BigDecimal departmentId;private BigDecimal subcompanyId;public BlTmsRecipientsTabId() {} public BigDecimal getQuestId() {return this.questId;}public void setQuestId(BigDecimal questId) {this.questId = questId;}public BigDecimal getQuestdispenseId() {return this.questdispenseId;}public void setQuestdispenseId(BigDecimal questdispenseId) {this.questdispenseId = questdispenseId;}public String getObjectId() {return this.objectId;}public void setObjectId(String objectId) {this.objectId = objectId;}public String getEnterUser() {return this.enterUser;}public void setEnterUser(String enterUser) {this.enterUser = enterUser;}public Date getEnterDate() {return this.enterDate;}public void setEnterDate(Date enterDate) {this.enterDate = enterDate;}public Date getModiDate() {return this.modiDate;}public void setModiDate(Date modiDate) {this.modiDate = modiDate;}public String getModiUser() {return this.modiUser;}public void setModiUser(String modiUser) {this.modiUser = modiUser;}public BigDecimal getDepartmentId() {return this.departmentId;}public void setDepartmentId(BigDecimal departmentId) {this.departmentId = departmentId;}public BigDecimal getSubcompanyId() {return this.subcompanyId;}public void setSubcompanyId(BigDecimal subcompanyId) {this.subcompanyId = subcompanyId;}
3.映射文件xxx.hbm.xml<!-- Generated 2017-4-19 16:22:54 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping> <class name="com.tms.Beans.BlTmsRecipientsTab" table="BL_TMS_RECIPIENTS_TAB" schema="IFSAPP"> <composite-id name="id" class="com.tms.Beans.BlTmsRecipientsTabId"> <key-property name="questId" type="big_decimal"> <column name="QUEST_ID" precision="22" scale="0" /> </key-property> <key-property name="questdispenseId" type="big_decimal"> <column name="QUESTDISPENSE_ID" precision="22" scale="0" /> </key-property> <key-property name="enterUser" type="string"> <column name="ENTER_USER" length="15" /> </key-property> <key-property name="enterDate" type="date"> <column name="ENTER_DATE" length="7" /> </key-property> <key-property name="modiDate" type="date"> <column name="MODI_DATE" length="7" /> </key-property> <key-property name="modiUser" type="string"> <column name="MODI_USER" length="15" /> </key-property> <key-property name="departmentId" type="big_decimal"> <column name="DEPARTMENT_ID" precision="22" scale="0" /> </key-property> <key-property name="subcompanyId" type="big_decimal"> <column name="SUBCOMPANY_ID" precision="22" scale="0" /> </key-property> </composite-id> </class></hibernate-mapping>
那么问题来了,如果你的表中某个字段为空值的话,就会导致复合主键失效,进而导致查询的sql对应不到映射的表,然后就只能返回空值.
所以返回结果为空的原因:因为表中某字段的值为空导致
解决办法:将映射文件中可能出空值的字段从复合主键中移出,
具体操作:1. 将xxx.hbm.xml文件中可能为空值的字段移出<composite-id>标签,并将移出的<key-property>的统一改为<property>
示例:
<!-- Generated 2017-4-19 16:22:54 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping> <class name="com.tms.Beans.BlTmsRecipientsTab" table="BL_TMS_RECIPIENTS_TAB" schema="IFSAPP"> <composite-id name="id" class="com.tms.Beans.BlTmsRecipientsTabId"> <key-property name="questId" type="big_decimal"> <column name="QUEST_ID" precision="22" scale="0" /> </key-property> <key-property name="questdispenseId" type="big_decimal"> <column name="QUESTDISPENSE_ID" precision="22" scale="0" /> </key-property> <key-property name="enterUser" type="string"> <column name="ENTER_USER" length="15" /> </key-property> <key-property name="enterDate" type="date"> <column name="ENTER_DATE" length="7" /> </key-property> </composite-id> /** 修改部份 **/ <property name="objectId" type="string"> <column name="OBJECT_ID" length="1000" /> </property> <property name="modiDate" type="date"> <column name="MODI_DATE" length="7" /> </property> <property name="modiUser" type="string"> <column name="MODI_USER" length="15" /> </property> <property name="departmentId" type="big_decimal"> <column name="DEPARTMENT_ID" precision="22" scale="0" /> </property> <property name="subcompanyId" type="big_decimal"> <column name="SUBCOMPANY_ID" precision="22" scale="0" /> </property> /**************/ </class></hibernate-mapping>
2. 在xxx.class文件中加入上一步修改的property对应的变量的get()/set()方法。
然后就ok啦,测试一下,结果返回正常。
0 0
- 关于hibernate反向生成实体类的问题-没有主键的表映射
- 关于hibernate对实体类主键生成策略的注解
- hibernate 无主键的表映射实体类
- Hibernate映射实体类时解决没有ID的问题
- hibernate 实体类生成表 表没有生成的原因
- hibernate生成实体时对应的映射文件中主键的生成策略
- hibernate实体类映射不到的问题
- Myeclipse反向生成hibernate实体映射
- Eclipse反向生成hibernate实体类等的方法
- hibernate的实体类编写规则与主键生成策略
- 通过Hibernate code generation configration反向生成实体类出现的问题
- 关于复合主键和反射生成两个实体类的问题
- 关于eclipse没有hibernate包的问题及更改反向工程文件生成的属性类型问题
- 解决使用Hibernate QBC复合查询含有联合主键映射关系实体的问题
- 数据库中有两个主键的表反向生成的时候会出现俩个实体类
- eclipse 利用hibernate插件工具反向生成具有关联关系表的实体类
- Hibernate中关于主键生成策略的问题
- Hibernate处理无主键的表的映射问题
- c#
- Android集成Unity到App中
- 1028. List Sorting (25)
- JSP,Servlet,JSF 的区别
- BZOJ 1271: [BeiJingWc2008]秦腾与教学评估 二分查找
- 关于hibernate反向生成实体类的问题-没有主键的表映射
- Js网络视频播放器之VideoJs&&ckplayer(直播拉流rtmp、hls)
- 静夜思—何方(续)
- npm常用命令
- 说说 Hibernate 的映射策略
- 输入子系统(三)------调用关系
- Scala安装配置
- hadoop-hdfs学习1
- 开闭原则