关于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
原创粉丝点击