Home Getting error java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("APP_LOCAL"."PI_ADDRESS"."EMP_ID")
Reply: 1

Getting error java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("APP_LOCAL"."PI_ADDRESS"."EMP_ID")

Pracheer Pancholi
1#
Pracheer Pancholi Published in 2018-01-11 16:13:38Z

Getting error java.sql.SQLIntegrityConstraintViolationException: ORA-01400:

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("APP_LOCAL"."PI_ADDRESS"."EMP_ID")

Below is the code :

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

}
Pracheer Pancholi
2#
Pracheer Pancholi Reply to 2018-01-12 08:55:00Z

I resolved this issue by putting nullable=false on parent side and putting insertable=false and updatable=false on child side and removing boilerplate code in EmployeeRepositoryImpl to set employeeId on address.

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

 }  
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.327968 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO