我正在将Spring Boot 2.0.4与Spring Data Jpa一起使用。我有一个带有用户表的User模型。用户ID是这样定义的UUID:
@Id
@Type(type="uuid-binary")
@GenericGenerator(name="uuid2", strategy="uuid2")
@GeneratedValue(generator="uuid2")
@Column(columnDefinition="BINARY(16)", unique=true, updatable=false)
private final UUID id = UUID.randomUUID();
到目前为止,一切正常。
我现在添加一个令牌模型和一个令牌表,在其中存储令牌(例如电子邮件确认令牌等)。令牌模型包括一个字段User user,我想使用@ManyToOne关系将其加入到用户表中,如下所示:
@ManyToOne(fetch = FetchType.LAZY, optional=false)
@JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false)
private User user;
当我仍在使用Long for User ID时,这一切运行良好。但是,现在我继续收到以下错误:
2018-08-23 18:33:26.700 WARN 97837 --- [nio-8081-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1452, SQLState: 23000
2018-08-23 18:33:26.700 ERROR 97837 --- [nio-8081-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : Cannot add or update a child row: a foreign key constraint fails (`auth`.`token`, CONSTRAINT `FKe32ek7ixanakfqsdaokm4q9y2` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`))
2018-08-23 18:33:26.701 ERROR 97837 --- [nio-8081-exec-2] o.h.i.ExceptionMapperStandardImpl : HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
2018-08-23 18:33:26.703 ERROR 97837 --- [nio-8081-exec-2] o.a.c.c.C.[.[.[/].[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
我已经搜索了一下,但是没有找到任何帮助,因此非常感谢任何帮助,因为我一直在努力解决这一问题,并试图在整个今天的下午尝试所有可能的排列方式,但是无论如何,错误仍然是相同的我尝试了什么。