休眠保存对象(一对多关系)外键为空
2022-09-04 02:06:11
我在人类和汽车类之间有一对多的关系。一个人可以拥有许多汽车,反之亦然。我正在使用RESTFUL API来发布数据。我的注释和获取服务工作正常,但每当我尝试插入新数据时,我的邮政服务都会抛出“ 。子表作为 插入。java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL" error
foreign key
null
这是我的代码的一部分。
人.java
private List<Car> cars = new ArrayList<Car>();
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="person")
@JsonManagedReference
public List<Car> getCars() {
return cars;
}
汽车.java
private Person person;
@ManyToOne
@JoinColumn(name = "PERSON_ID", nullable = false, updatable = true, insertable = true)
@JsonBackReference
public Person getPerson() {
return person;
}
我的服务类:
@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();
}