使用 hibernate/jpa 无法将 UUID 存储到 MySql 数据库中
Failed to store UUID into MySql database using hibernate/jpa
我有一个数据库 table 包含此列:
uuid CHARACTER(36) NOT NULL
它也是一个 UNIQUE INDEX (uuid)
但不是主键。
实体class中uuid列的定义是这样的:
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(name = "uuid", updatable = false, nullable = false)
private UUID uuid;
然后有一个Repository接口就是spring数据Repository接口:
@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 uuid = UUID.randomUUID() ?
重复?
我有一个数据库 table 包含此列:
uuid CHARACTER(36) NOT NULL
它也是一个 UNIQUE INDEX (uuid)
但不是主键。
实体class中uuid列的定义是这样的:
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(name = "uuid", updatable = false, nullable = false)
private UUID uuid;
然后有一个Repository接口就是spring数据Repository接口:
@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 uuid = UUID.randomUUID() ?
重复?