DataIntegrityViolationException:无法执行语句; SQL嵌套异常是org.hibernate.exception.ConstraintViolationException

时间:2016-01-30 13:00:53

标签: java mysql spring hibernate spring-boot

我的外键存在约束问题。我有两个具有manyToOne关系的对象(User和UserSession):用户可以拥有许多UserSession。我试图插入一个新的UserSession,但我得到了一个DataIntegrityViolationException。

这是我的usersession类:

public class UserSession implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(name = "id", columnDefinition = "VARCHAR(50)")
    private String userSessionUUId;

    other parameters ....
    @ManyToOne(targetEntity = User.class, fetch = FetchType.EAGER)
    @JoinColumn(name = "userUUId", referencedColumnName = "id", nullable = false)
    private User user;

我的用户类:

@Entity
@Table
public class User implements Serializable {

    some parameters

    @OneToMany(mappedBy = "user", targetEntity = UserSession.class, fetch = FetchType.LAZY)
    private List<UserSession> userSessions = new LinkedList<>();

控制器

@RequestMapping(value = "/path", method = RequestMethod.POST, consumes = "application/json")
public void addUserSession(@RequestBody @Valid UserSession userSession) {
    object.createEntity(userSession);
}

我发布了这个请求:

curl -i -v -X POST -H 'Content-Type: application/json' -d '{othersparameters, "userUUId": "EEEEEE"}' http://localhost:8080/path 

错误:

[dispatcherServlet]: 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 'useruuid' cannot be null
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

有人可以帮助我吗?

干杯

0 个答案:

没有答案