I have two tables 'STUDENT'(pk - student_id autogenerated) and 'Address' . address_id is pk of Address table and it must be same value as student_id .
entities :
@Entity
@Table(name = "student")
Public class Student{
@Id
@GeneratedValue
@Column(name = "STUDENT_ID",updatable = false,insertable = false)
private long Id;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="STUDENT_ID")
private Address address;
.....
//getters setters
}
@Entity
@Table(name = "address")
Public class Address{
@Id
@Column(name = "ADDRSS_ID",updatable = false,insertable = false)
private long Id;
@OneToOne
private Student student;
.....
//getters setters
}
service
public class Service {
@Autowired StudentRepository repo;
public void saveStudent(Student s){
repo.save(s)
}
}
Problem that i am facing is because of cascade.ALL JPA trying to save child entity i.e. Address before parent entity Student, but ADDRESS_ID is having NOT NULL Constraint and I am getting Constraint Violation Exception. How to Save both at one go. PLEASE Help . Thanks in advance