我想在以下实体上建立OneToOne关系事件&计划。问题是,当我提交(这是一个更新)事件没有进入计划表(创建计划)休眠 - OneToOne错误
在IDE中,我可以检查从浏览器返回的内容,并且计划中有一个值 - 尽管计划.id为空,因为计划是新的。
控制台显示event_id为空的错误(event_id是计划表上的FK)。
事件
@Entity
public class Event {
@Id
private Long id;
@OneToOne(mappedBy = "event", cascade = CascadeType.PERSIST)
private Plan plan;
//
计划
@Entity
public class Plan {
@Id
private Long id;
@OneToOne
@JoinColumn(name = "event_id", nullable = false)
private Event event
//
我有更新现有的事件,需要创建一个计划的形式。
当我提交了以下错误抛出形式:
浏览器:
There was an unexpected error (type=Internal Server Error, status=500).
could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
控制台
""2017-05-16 19:16:34 - Column 'event_id' cannot be null
""2017-05-16 19:16:34 - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is 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
"com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'event_id' cannot be null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
我明白,这个错误的基础是event_id(位于表格计划中)不能为空,但该计划作为p提交艺术的事件更新 - 所以event_id应该得到作为一个正确的设置关系的结果?
事件服务
Event savedEvent = eventRepository.save(event); // this line ok
return eventRepository.save(event); // this line errors as above
请告诉我错在这里?
没有看到数据访问代码,不可能知道为什么你有这个问题。 –
所有者方是没有mappedBy属性的一面。这是Hibernate关心的一个问题。您需要确保设置计划事件:计划。SetEvent的(......)。 –
从浏览器返回的对象是一个事件,它已经附加了一个计划对象,并带有一些测试数据。为什么我需要再次设置这种关系? –