I am having issues while persisting an child entity while parent entity has OneToManay relationship with another entity. Could someone help what could be the issue with the following setup.
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name=PERSON)
Public class Person{ //parent class
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name='DB_ID)
private Long id;
@Id
@Column(name="PERSON_ID)
Private String personId;
@OneToMany(mappedBy="address", cascade={cascadeType.PERSIST,cascadeType.MERGE}){
private List<Address> addresses;
}
}
This is my child class
@Entity
@Table(name=EMPLOYEE)
Public class Employee extends Person{
@Id
@Column(name="NAME)
Private String name;
}
This is the 3rd class for @OneToMany from Person to Adress
@Entity
@Table(name=ADRESS)
Public class Adress{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name='DB_ID)
private Long id;
@Id
@Column(name="address)
Private String adress;
@ManytoOne
@joinColumn(name="parent_id", referenceColumn="PERSON_ID")
private Person person;
}
Repository for saving child class
public interface EmployeeRepository extends JpaRepository<Emploee, Long>{}
I am getting the below error.
cannot insert NULL into (“ADDRESS”.”PARENT_ID”)
Why would JPA throws this error even though i am setting “abc123” as person_id on Employee object before saving.
I am saving the entity using EmployeeRepository class but getting the following error. The child entity has requored values
Employee employee = Employee.builder()
.name("john does")
.personId("abc123")
.address(addressist).build();
employeeRepository.save(employee);
I am expecting that records will be persisted in all 3 tables PERSOB, EMPLOYEE and ADDRESS table but getting the error.