繁体   English   中英

JPA- onetomany casecade在子表中两次插入相同记录

[英]JPA- onetomany casecade inserting same record twice in child table

我正在尝试在casecadeALL中插入一个带有onetomany关系表的记录。 当表上发生任何DML时,我在审计表中都有带有insert,update或delete条目的审计信息。 当我运行插入时,它可以在基本表中插入记录,但是在审计过程中,我看到子表具有2个用于单个插入的条目。 它正在使用相同的记录更新表。 无法理解为什么casecade都在几毫秒内更新同一条记录。

        parent class

    public class Department
    {

       /** The destination id. */
       @Id
       @SequenceGenerator(name = ....", sequenceName = ...)
       @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = ....)
       @Column(name = "DEST_ID", nullable = false, unique = true)
       private Long                             destinationId;
       /** The destination name. */
       @Column(name = "DEST_NM")
       private String                           destinationName;
       /** The Std UTC hour operation . */
       @OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
       @JoinColumn(name = "DEST_ID", nullable = false)
       private List<Hours> hrList = new ArrayList<Hours>();

    }

    child class
    public class Hours
    {

        @Id
        @SequenceGenerator(name = ...., sequenceName = ....)
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = ....)
        @Column(name = "HR_ID")
        private Long hoursId;
        /** The Destination. */
        @ManyToOne(optional = true)
        @JoinColumn(name = "DEST_ID", nullable = false, insertable = false, updatable = false)
        private Department department;
    }


in service class calling -

departmentDao.saveOrupdate(department);

in DAO layer

public void saveOrUpdate(Department departmentToStore) {
        em.persist(departmentToStore);
}

我也有其他相关表格,但它们工作正常。 我只有表oneToMany关系有这个问题。

注意:表是单向的。 我正在使用persist方法插入记录。


请找到完整的代码-

@Entity
@Table(name = "DEPARTMENT")
@XmlRootElement(name = "Department")
public class Department {
    /** The destination id. */
    @Id
    @SequenceGenerator(name = "deptSeq", sequenceName = "SEQ1")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "deptSeq")
    @Column(name = "DEST_ID", nullable = false, unique = true)
    private Long destinationId;
    /** The destination name. */
    @Column(name = "DEST_NM")
    private String destinationName;
    /** The hour operation . */
    @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "department")
    private List<Hours> hoursList = new ArrayList<Hours>();
setter & getters ...
}
@Entity
@Table(name = "HOURS")
@XmlRootElement(name = "SpecialHoursOfOperation")
public class SpecialUTCHoursOfOperation {

    /** The hour id. */
    @Id
    @SequenceGenerator(name = "hourSeq", sequenceName = "SEQ2")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hourSeq")
    @Column(name = "HR_ID")
    private Long hourId;
    /** The Destination. */
    @ManyToOne(optional = true)
    @JoinColumn(name = "DEST_ID", nullable = false, insertable = false, updatable = false)
    private Department department;
    /** The hoursdate. */
    @Column(name = "SPEC_HR_OPRT_DT")
    private Date HoursDate;
setters and getters
}

DepartmentDAOImpl class -
@Override
    @Transactional
    public Department saveOrUpdate(Department departmentToStore) {
        Department department = new Department();
        try {
            department = persist(departmentToStore);
        } catch (PersistenceException pe) {
            pe.getMessage();
        }
        return department;
    }

in DeptService.java
public DepartmentVO storeDepartment(DepartmentVO departmentVO){
Department department = new Department();
department = Helper.populateDepartment(departmentVO);
department.setHoursList(Helper.populateHours(departmentVO, department));
department = departmentDAO.saveOrUpdate(department);
return departmentVO;
}

in Helper.java

public static Department populateDepartment(final DepartmentVO departmentVO) {
Department department = new Department();
department.setDestinationName(departmentVO.getDepartmentName());
return department;
}

public static List<Hours> populateHours(final DepartmentVO departmentVO, final Department department) {
List<Hours> hoursList = new ArrayList<Hours>();
List<HoursVO> hoursVOs = departmentVO.getSpecialDayHourVOs();
for (HoursVO hoursVO : hoursVOs) {
            Hours hoursObj = new Hours();
hoursObj.setDepartment(department);
            hoursObj.setHoursDate(hoursVO.getSpecialDate());
hoursList.add(hoursObj);
}
return hoursList;
}

数据库表-部门(dest_id(pk),dest_nm),小时(hr_id(pk),dest_id(fk),hr_dt)。

然后我有休息层与前端进行通信。 如果我运行此代码,当调试器到达保存方法时,它将引发异常。 UniqueCOnstraintviolation ORA-01400:无法将NULL插入(HOURS。“ DEST_ID”)

如果您使用某些拦截器来记录审核信息,则会由于单向映射而发生此问题。 通常,您的部门应该使用mapedBy,而小时应该通过joinColumn使用对部门的引用。 这将使其成为双向的。 那么在保存时,它不会触发额外的更新查询。 您可以在互联网上了解有关inverse = true / false的更多信息以及jpa的单向凹坑。 更改为bi-di以防止进行额外的更新查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM