为公司之前的一个公交卡系统修改bug: 通过排查发现, 卡类型表和卡等级表是一对多的关系, 但是卡等级表中没有字段引用卡类型表的主键,而是引用了卡类型表中非主键的另外两个字段
通过查看hibernate实战这本书, 发现property-ref可以解决这个问题
property-ref: 是为了解决遗留系统中实体之间的关联,通过它指定被关联实体主键以外的字段作为关联字段。
这里记录下我的修改:
1.修改卡类型
修改卡类型实体的映射文件
<-- 卡类型表的两个字段 agentid 和 cardtype properties 标签可以同时指定两个字段 --> <properties name="agentidAndCardtype" unique="true" update="false"> <property name="agentid" type="java.lang.String"> <column name="AGENTID" length="3" not-null="true" /> </property> <property name="cardtype" type="java.lang.String"> <column name="CARDTYPE" length="3" not-null="true" /> </property> </properties>
2.修改卡等级
首先是卡等级实体的修改:
/** * 卡等级表实体类: agentids和cardtype引用的是卡类型表的字段, 用卡类型表的实体代替这两个字段 */ // private String agentids; // private String cardtype; private Acardtype cardtype;
在是卡等级实体映射文件的修改:
<-- 卡等级表 property-ref:使用此属性引用需要的字段 --> <many-to-one name="cardtype" class="com.foorich.entity.oracle.Acardtype" fetch="select" property-ref="agentidAndCardtype"> <column name="AGENTID" length="3" not-null="true" /> <column name="CARDTYPE" length="3" not-null="true" /> </many-to-one>
这样修改之后, cardtype 就相当于一个关系属性了, 通过cardtype.agentids 和 cardtype. cardtype,可以使用这个两个字段