休眠状态:无法更新/合并OneToMany以及OneToOne关系

时间:2019-09-05 07:58:11

标签: java hibernate orm sql-update hibernate-mapping

我有以下实体

Entity 1: 
Core
 -- List<Pos> //Contains list of Posentity

Entity 2:
External extends Core{
} 
Entity 3:
Pos{
}

合并时,我不能持久存储Pos和External,但可以在Core中合并数据

        Core core = employeeEntityManager.createNamedQuery("findByEmployeeId", EmployeeCore.class)
                                    .setParameter("employeeId", empId).getSingleResult(); //This returns all core data includes external as well as position.
        .
        .
        //Mapper used to map correct data into core from third party
        .
        . 
        if(core instanceof External) { //Core is getting printed with all updated data of Core, External as well as Pos (checked using toString)
            External ee = (External) core ;
            log.info("persist: core :" + ee.toString());
        } else {

            log.info("persist: core :" + core .toString());
        }


        if (employeeExists(core.getEmployeeId())) { //In Employee COre named query just to check employee exist
            try {
                userTransaction.begin();
                empEntityManager.merge(core);
                empEntityManager.flush();
                userTransaction.commit();
            } catch (Exception ex) {
                userTransaction.rollback();
                throw ex;
            }
        }

我能够保留新记录,但会在更新时发出问题。 任何想法如何解决这个问题?这是userTransaction的问题吗?或蚂蚁其他方式?

更新: 将以下CascaseType更改为All @OneToMany(mappedBy =“ posPK.Id”,orphanRemoval = true,fetch = FetchType.LAZY,cascade = {CascadeType.ALL}) 此文件已保存为oneToMany,但仍与OneToOne一起待处理

0 个答案:

没有答案