在hibernate一对多映射中获取非空约束错误

时间:2017-03-22 23:16:11

标签: hibernate

所以我的用户有一个位置,例如:

@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, ankitv@uci.edu, 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

请提出任何建议..

1 个答案:

答案 0 :(得分:0)

尝试从User类中删除Location的实例化。 https://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation/似乎是一个非常类似的东西,但没有相应的实例化。

或者,确认在数据库中创建的列具有您期望的名称。