我有一个包含此列的数据库表:
uuid CHARACTER(36) NOT NULL
它也是一个UNIQUE INDEX (uuid)
,但不是主键。
在 Entity 类中,uuid 列的定义如下所示:
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(name = "uuid", updatable = false, nullable = false)
private UUID uuid;
然后有一个存储库接口,它弹簧数据存储库接口:
@Repository
public interface SaveRepository extends Repository<CoreEvent, Integer> {
}
当我调用saveRepository.save((时,它抱怨uuid值为空:
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:278)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:491)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
。
Caused by: java.sql.SQLIntegrityConstraintViolationException: Column 'uuid' cannot be null
在将记录保存到数据库之前,我是否错过了任何内容以生成uuid?
提前致谢:)
您可以使用一些事件(如 @PrePersist(来填充 UUID 字段 https://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/listeners.html
但是为什么在创建对象时不分配uuid uid = UUID.randomUUID((?
重复? 如何在春季启动时使用休眠生成自动UUID