我正在尝试使用casecadeALL插入一个onetomany关系表的记录。 当表上发生任何DML时,我在审计表中有插入,更新或删除条目的审计信息。当我运行插入时,它在基表中插入记录正常但在审计期间我看到子表有单个插入的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)。
然后我有休息层与前端沟通。 如果我运行此代码当调试器到达save方法时,它会抛出异常。 UniqueCnstraintviolation ORA-01400:无法插入NULL(HOURS。“DEST_ID”)
答案 0 :(得分:3)
如果您使用某些拦截器来记录审计信息,则会出现此问题,原因是单向映射。通常你的部门应该使用mappedBy,而小时应该使用带有joinColumn的引用。这会使它成为双向的。然后在保存时它不会激发额外的更新查询。 你可以在互联网上阅读更多关于inverse = true / false和jpa的单向陷阱。更改为bi-di以防止额外的更新查询。