两个实体类对应的数据库table需要拥有相同的主键.

比如A类和B类,A有属性id,B类也有相同属性id.

在A类中设置属性 B b;

在B类中设置属性 A a;

在表格A和B中,id都是主键.

在B中id不仅是主键还是外键.

 

此时对于A的配置文件如:

<id name="id" column="id" type="integer">
<generator class="assigned"/> </id>

B.hbm.xml中以a为外键的参数.也就是外键id对应a中的主键id.

<class name="B" table="B">
<!-- id是主键 -->
<id name="id" column="id" type="integer">
<generator class="foreign">
<param name="property">a</param>
</generator>
</id>

Test:

A a=new A(1);

B b=new B(null);

a.setB(b);

b.setA(a);

session.save(a);

session.save(b);

session.beginTransaction().commit();

 

相关文章:

  • 2022-12-23
  • 2021-09-30
  • 2021-07-14
  • 2021-08-13
  • 2022-01-08
  • 2021-11-11
  • 2022-01-20
  • 2021-10-02
猜你喜欢
  • 2021-10-30
  • 2022-03-09
  • 2021-06-05
  • 2021-10-11
  • 2021-07-12
  • 2021-06-24
  • 2022-12-23
相关资源
相似解决方案