Hibernate save object (one to many relationship) foreign key is null
I have one to many relationships between person class and car class. A person can own many cars and vice versa. I am using restful API to post data. My annotations and Get service is working fine but my post service throws " java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL" error
whenever I try to insert new data. Child table foreign key
is being inserted as null
.
Here is part of my code.
Person.java
private List<Car> cars = new ArrayList<Car>();
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="person")
@JsonManagedReference
public List<Car> getCars() {
return cars;
}
Car.java
private Person person;
@ManyToOne
@JoinColumn(name = "PERSON_ID", nullable = false, updatable = true, insertable = true)
@JsonBackReference
public Person getPerson() {
return person;
}
My service class:
@POST
@Path("/PersonRegistration")
@Consumes(MediaType.APPLICATION_JSON)
public Response postPersonCars(Person person) throws Exception{
Session session = null;
ObjectMapper mapper = new ObjectMapper();
//Person per = new Person();
//Car cars = new Car();
try{
session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
//per.setCars(person.getCars());
session.save(person);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
if(null != session){
session.close();
}
}
return Response.status(201).entity(mapper.writeValueAsString(person)).build();
}
This annotation:
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="person")
has two consequences:
-
mappedBy
implies thatCar
is the owning side of the relationship. This means that whenever you want to establish a relationship betweenCar
andPerson
, you need to do it by setting theCar.person
property to the appropriate value. Changes toPerson.cars
will be ignored by Hibernate. -
cascade=CascadeType.ALL
means that whenever you save aPerson
, Hibernate will also invoke the save operation on all entities contained inPerson.cars
Result: you are calling Session.save()
on a bunch of Car
entities that do not have the Car.person
property set properly.
Solution: either change the owning side of the relationship (be aware that you will also need a @JoinColumn
on Person.cars
if you do not want an extra database table to be created) or loop through Person.cars
and set the Car.person
property properly in each of them.
cascade=CascadeType.ALL
suggests the first solution fits your use case better.