2017-05-16 1 views
0

다음 엔티티에 OneToOne 관계를 설정하려고합니다. 이벤트 & 계획. 문제는 계획서 (계획 작성)에 이벤트가 아무것도 제출되지 않는 경우입니다 (계획 작성).최대 절전 모드 - OneToOne 오류

IDE에서 브라우저에서 되돌아 오는 것을 검사 할 수 있으며 계획에는 값이 있습니다. 계획이 새롭기 때문에 .id가 null입니다.

콘솔에 event_id가 null이라는 오류가 표시됩니다 (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 (테이블 계획에 있음)는 null 일 수는 없지만 계획은 p로 제출됩니다. 이벤트 업데이트 기술 - 올바르게 설정된 관계의 결과로 event_id를 얻을 수 있습니까?

이벤트 서비스

Event savedEvent = eventRepository.save(event); // this line ok 
return eventRepository.save(event); // this line errors as above 

무슨 잘못 여기에?

+0

데이터 액세스 코드를 보지 않고도이 문제가 발생한 이유를 알 수 없습니다. –

+0

소유자 측은 mappedBy 특성이없는 쪽입니다. 그것은 Hibernate가 신경 쓰는 것입니다. 계획의 이벤트를 설정해야합니다 : 계획.setEvent (...). –

+0

브라우저에서 되돌아 오는 객체는 이벤트이며 일부 테스트 데이터와 함께 이미 계획 객체가 첨부되어 있습니다. 그 관계를 다시 설정해야하는 이유는 무엇입니까? –

답변

0

이것은 양방향 관계이므로 기본적으로 필요한 것은 관계의 각면이 다른면을 참조하는 경우입니다. 귀하의 경우에는 하나의 참조 만 가질 수 있습니다.

는 한쪽에서 매핑을 제거하여 단방향으로 관계를 켜거나 대답은 이벤트 테이블에 기록 된 plan_id을 가지고 있었다 누락 된 참조

0

를 주입 할 수 있습니다이 문제를 해결합니다. 그래서 주석되었다 :

이벤트 내가 실제로 양방향을 (이 솔루션은 위의 단방향) 할 필요가 없다

@Entity 
public class Plan { 
@Id 
private Long id; 
// 

@Entity 
public class Event { 
@Id 
private Long id; 
@OneToOne(cascade = CascadeType.ALL) 
private Plan plan; 
// 

계획.