2017-03-22 69 views
0

所以我的用戶一個位置,如:獲得非空約束錯誤在休眠一對多映射

@ManyToOne(fetch = FetchType.EAGER) 
@JoinColumn(name = "location_id") 
private Location location = new Location(); 

和位置表有:

@OneToMany(mappedBy="location", cascade=CascadeType.ALL) 
private Set<User> userSet = new HashSet<User>(); 

只是運行一個簡單的插入到用戶表調用 我一直運行到這個錯誤:

Hibernate: insert into users (createdAt, email, firstName, gender, hashedPassword, lastName, location_id, type, updatedAt) values (?, ?, ?, ?, ?, ?, ?, ?, ?) 
Mar 22, 2017 3:06:57 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 
WARN: SQL Error: 0, SQLState: 23502 
Mar 22, 2017 3:06:57 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 
ERROR: ERROR: null value in column "location_id" violates not-null constraint 
Detail: Failing row contains (1, 2017-03-22 15:06:53.832, [email protected], ankit, null, turende, verma, 3, null, null). 
2017-03-22 15:06:57.822:WARN::/bemh/medhuddleapi/userInfo/insertUser 
org.hibernate.exception.ConstraintViolationException: could not execute statement 

有任何建議,請..

+0

通過顯示位置的構造函數可以改善問題,因爲它直接在顯示的代碼中調用。 User.location和Location.userSet的相關getter和setter也可能有所幫助。與Location.id屬性上的註釋一樣。 –

+0

https://stackoverflow.com/questions/42327971/postgresql-throw-null-value-in-column-violates-not-null-constraint-when-using-on也是相關的。這裏的答案和評論可能有幫助。 –

回答