Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 休眠-一通错误_Java_Hibernate_Servlets - Fatal编程技术网

Java 休眠-一通错误

Java 休眠-一通错误,java,hibernate,servlets,Java,Hibernate,Servlets,我正在尝试在以下实体事件和计划上建立OneToOne关系。问题是,当我提交(这是一个更新)事件时,没有任何内容进入计划表(为计划创建) 在IDE中,我可以检查从浏览器返回的内容,并且Plan中有一个值-尽管Plan.id是空的,因为Plan是新的 控制台显示一个错误,事件id为null(事件id是计划表上的FK) 事件 @Entity public class Event { @Id private Long id; @OneToOne(mappedBy = "event", cascade =

我正在尝试在以下实体事件和计划上建立OneToOne关系。问题是,当我提交(这是一个更新)事件时,没有任何内容进入计划表(为计划创建)

在IDE中,我可以检查从浏览器返回的内容,并且Plan中有一个值-尽管Plan.id是空的,因为Plan是新的

控制台显示一个错误,事件id为null(事件id是计划表上的FK)

事件

@Entity
public class Event {
@Id
private Long id;
@OneToOne(mappedBy = "event", cascade = CascadeType.PERSIST)
private Plan plan;
//
@Entity
public class Event {
@Id
private Long id;
@OneToOne(cascade = CascadeType.ALL)
private Plan plan;
//
计划

@Entity
public class Plan {
@Id
private Long id;
@OneToOne
@JoinColumn(name = "event_id", nullable = false)
private Event event
//
@Entity
public class Plan {
@Id
private Long id;
//
我有一个更新现有事件的表单,需要创建一个计划

提交表单时,会引发以下错误:

浏览器:

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 savedEvent = eventRepository.save(event); // this line ok
return eventRepository.save(event); // this line errors as above
据我所知,此错误的基础是事件\u id(在表计划中)不能为空-但计划是作为事件更新的一部分提交的-因此,应根据正确的设置关系获取事件\u id

活动服务

""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 savedEvent = eventRepository.save(event); // this line ok
return eventRepository.save(event); // this line errors as above

这里怎么了?

这是一个双向关系,所以基本上你需要的是关系的每一方都引用另一方,在你的例子中,你只有一个引用


要解决这个问题,您可以通过从一侧移除映射将关系变为单向关系,或者注入缺少的引用,答案是将计划id记录在事件表上。因此,注释变成:

事件

@Entity
public class Event {
@Id
private Long id;
@OneToOne(mappedBy = "event", cascade = CascadeType.PERSIST)
private Plan plan;
//
@Entity
public class Event {
@Id
private Long id;
@OneToOne(cascade = CascadeType.ALL)
private Plan plan;
//
计划

@Entity
public class Plan {
@Id
private Long id;
@OneToOne
@JoinColumn(name = "event_id", nullable = false)
private Event event
//
@Entity
public class Plan {
@Id
private Long id;
//

实际上,我从来不需要将其设置为双向(上面的解决方案是单向的)。

如果看不到数据访问代码,就不可能知道为什么会出现此问题。所有者端是没有mappedBy属性的端。这是Hibernate关心的。您需要确保设置plan的事件:plan.setEvent(…)。从浏览器返回的对象是一个事件,并且它已经附加了一个带有一些测试数据的plan对象。为什么我需要再次建立这种关系?再次强调,问题不在于活动没有计划。问题是该计划没有事件。您能给我看一段代码片段吗?因为我做的所有谷歌搜索都显示这是一个正确设置的双向OneTONE?