获取错误java.sql.SQLIntegrityConstraintViolationException:ORA-01400:
org.springframework.dao.DataIntegrityViolationException:无法执行语句; SQL [不适用];约束[null];嵌套异常是org.hibernate.exception.ConstraintViolationException:无法执行语句]的根本原因 java.sql.SQLIntegrityConstraintViolationException:ORA-01400:无法插入NULL(" APP_LOCAL"。" PI_ADDRESS"。" EMP_ID")
以下是代码:
@Entity
@Table(name = "PI_EMPLOYEE")
@NamedQuery(name = "Employee.findAll", query = "SELECT a FROM Employee e")
@DynamicUpdate(value=true)
@DynamicInsert(value=true)
public class Employee{
@Id
@SequenceGenerator(name = "PI_EMPLOYEE_SEQ", sequenceName =
"PI_EMPLOYEE_SEQ",allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PI_EMPLOYEE_SEQ")
@Column(name = "EMP_ID")
private Long employeeId;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "EMP_ID")
@BatchSize(size=5)
private List<Address> addresses;
}
@Entity
@Table(name = "PI_ADDRESS")
@DynamicUpdate(value=true)
public class Address {
@Id
@SequenceGenerator(name = "PI_ADDRESS_SEQ", sequenceName = "PI_ADDRESS_SEQ",allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PI_ADDRESS_SEQ")
@Column(name = "ADDRESS_ID")
private Long addressId;
@Column(name = "EMP_ID")
private Long employeeId;
}
public class EmployeeRepositoryImpl implements EmployeeRepository {
@Override
public Employee saveOrUpdate(Employee employee) {
Employee employeeToSave = employee;
if (employee == null) {
throw new IllegalArgumentException("Employee should not be null");
}
try {
Address address = employee.getAddress();
employee.setAddress(null);
employeeToSave = employeeRepository.save(employee);
if (null != address) {
setEmployeeID(employeeToSave.getEmployeeId(), address);
addressRepository.save(address);
employeeToSave.setAddress(address);
}
return employeeToSave;
} catch (Exception e) {
LOGGER.error("Exception occurred while saving Employee", e);
throw new BaseException(e);
}
}
private void setEmployeeID(Long employeeId, Object object) {
try {
ReflectionUtil.setFieldValue(object, "employeeId", employeeId);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
LOGGER.error("Exception occured while setting employeeId " + e);
throw new BaseException(e);
}
}
}
答案 0 :(得分:0)
我通过在父端放置nullable = false并在子端放置insertable = false和updatable = false并在EmployeeRepositoryImpl中删除样板代码以在地址上设置employeeId来解决此问题。
@Entity
@Table(name = "PI_EMPLOYEE")
@NamedQuery(name = "Employee.findAll", query = "SELECT a FROM Employee e")
@DynamicUpdate(value=true)
@DynamicInsert(value=true)
public class Employee{
@Id
@SequenceGenerator(name = "PI_EMPLOYEE_SEQ", sequenceName =
"PI_EMPLOYEE_SEQ",allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"PI_EMPLOYEE_SEQ")
@Column(name = "EMP_ID")
private Long employeeId;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "EMP_ID",nullable =false)
@BatchSize(size=5)
private List<Address> addresses;
}
}
@Entity
@Table(name = "PI_ADDRESS")
@DynamicUpdate(value=true)
public class Address {
@Id
@SequenceGenerator(name = "PI_ADDRESS_SEQ", sequenceName =
"PI_ADDRESS_SEQ",allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"PI_ADDRESS_SEQ")
@Column(name = "ADDRESS_ID",nullable =false,insertable =false,updatable=false)
private Long addressId;
@Column(name = "EMP_ID")
private Long employeeId;
}