2

我正在尝试使用 onetomany 关系表使用 casecadeALL 插入记录。当表上发生任何 DML 时,我会在审计表中使用插入、更新或删除条目的审计信息。当我运行插入时,它在基表中插入记录很好,但在审计期间我看到子表有 2 个用于单次插入的条目。它正在使用相同的记录更新表。无法理解为什么 casecade all 会在几毫秒内更新相同的记录。

        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;
}

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

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

4

1 回答 1

3

if you are using some interceptor to log audit info, this problem happens due to uni-directional mapping. normally your Department should have used a mappedBy and Hours should have used a ref to Department with joinColumn. that will make it bi-directional. then on save it will not fire extra update queries. you can read more about inverse=true/false on internet and uni-directional pit-falls for jpa. change to bi-di to prevent extra update queries.

于 2013-08-17T07:07:57.150 回答