是否可以使用Hibernate将Java对象映射到具有FK关系的多个表?
我有3个表以及上面描述的表之间的关系 我是新来Hibernate的,我想知道是否可以将上面的表映射到一个实体中?我知道@SecondaryTable注释,但我不确定是否可以在这里使用它,因为表1和表3之间没有直接关系 如果可能,hibernate是否足够聪明,可以先将行插入到表1中,然后使用返回的id将列插入到表2中是否可以使用Hibernate将Java对象映射到具有FK关系的多个表?,java,mysql,spring,hibernate,Java,Mysql,Spring,Hibernate,我有3个表以及上面描述的表之间的关系 我是新来Hibernate的,我想知道是否可以将上面的表映射到一个实体中?我知道@SecondaryTable注释,但我不确定是否可以在这里使用它,因为表1和表3之间没有直接关系 如果可能,hibernate是否足够聪明,可以先将行插入到表1中,然后使用返回的id将列插入到表2中 我已经进行了大量搜索,但尚未找到解决此问题的方法。您可以在Hibernate域对象中查找自动生成的id,而不是自动增加DB @Id @GeneratedValue(strategy
我已经进行了大量搜索,但尚未找到解决此问题的方法。您可以在Hibernate域对象中查找自动生成的id,而不是自动增加DB
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
protected Long c_id;
那么在你的流程中,你不能这样做吗
@Transactional(propagation = Propagation.REQUIRES_NEW)
method m1()
{
entity1 = entityManager.persist(entity1)
entityManager.flush();
// At this stage since entity Manager is flushed you will have the id's
// So manually set the id's to entity2
entity2.setForeignKey(entity1.getId());
entityManager.flush();
.
.
.
}