有
A表(id,name,sex) pk(id, name)
B表(id,name,region) pk(id, name)
C表(id,name,age) pk(id, name)
现在想C继承B,B继承A
pojo类也是这么设计的,A:B:C=1:1:1,如果插入数据到C中,A,B中也会有记录,插入数据到B中,A中有记录,但C没有
不知道hbm.xml文件怎么写?或者有更好的想法
请赐教!
A表(id,name,sex) pk(id, name)
B表(id,name,region) pk(id, name)
C表(id,name,age) pk(id, name)
现在想C继承B,B继承A
pojo类也是这么设计的,A:B:C=1:1:1,如果插入数据到C中,A,B中也会有记录,插入数据到B中,A中有记录,但C没有
不知道hbm.xml文件怎么写?或者有更好的想法
请赐教!
C关联AB,也就是一对一单端关联,B和A为双向关联C表中持有A和B对象的引用,保存C的话,无比先保存A和B设置cascade属性为true
B表中持有A的引用,设置cascade属性为true
<class name="Person" table="person">
<id name="id" column="id" type="integer">
<generator class="native"></generator>
</id>
<property name="name" not-null="true" />
<many-to-one name="address" class="Address" column="addressid"
cascade="all" unique="true">
</many-to-one>
</class>
</hibernate-mapping>
一对一的一端,cascade设定级联策略,all表示所有的操作都级联
另一端
<hibernate-mapping package="day03">
<class name="Address" table="address">
<id name="id" column="id" type="integer">
<generator class="native"></generator>
</id>
<property name="city" not-null="true" />
<!--property-ref="address" 指定外键对应的字段 -->
<one-to-one name="p" class="Person" property-ref="address"></one-to-one>
</class>
</hibernate-mapping>
每个类分层结构一张表(table per class hierarchy)
每个子类一张表(table per subclass)
每个具体类一张表(table per concrete class)
具体请看他的参考手册,楼主所说的应该是每个子类一张表(table per subclass)
作为学习研究可以,试一下
<class name="A" table="A">
<composite-id name="id" class="Aid">
<key-property name="id"/>
<key-property name="name"/>
</composite-id>
<property name="sex" column="sex"/>
...
<joined-subclass name="B" table="B">
<key>
<column name="id"></column>
<column name="name"></column>
</key>
<property name="region" column="region"/>
...
<joined-subclass name="C" table="C">
<key>
<column name="id"></column>
<column name="name"></column>
</key>
<property name="age" column="age"/>
......
</joined-subclass>
</joined-subclass>
</class>
多对多关系中,多采用中间表来关联两个有多多关系的表的.如Customer与CustomerDetails存在多对多的关系,那么往往是用另外一个表如CustomerBack表来保存它们的关系的,也就是中间表.◆name:属性的名称(可理解为中间表的名称)◆table:表的名称(可理解为中间表的表名称)◆key 里的cust_Id,是指中间表的cust_Id,为了保存Customer与CustomerDetails的关系,中间表中分别建了cust_Id(Customer的主键)与custD_Id(CustomerDetails的主键)◆many–to–many中的Class是指多对多中的另一个实例化类,column是指CustomerDetails与中间表关联的字段名.这里是指中间表的◆custD_Id,不能理解为CustomerDetails的主键.