Template
考虑一个用字段调用的实体:
@Version
@Column(name = "LAST_UPDATE", nullable = false)
private LocalDateTime lastUpdate; // Java 8's class
支持 Oracle 数据库字段是TIMESTAMP
.
现在执行以下代码:
// get EntityManager, start transaction
final var t = new Template();
t.setName("new template");
t.setSubject("subject");
em.persist(t);
t.setSubject("subject - mod");
tx.commit();
导致抛出异常:
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [Template#169]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:2525)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3357)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3231)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3632)
at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:146)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1453)
... 59 common frames omitted
如果我将类型更改lastUpdate
为java.sql.Timestamp
,则代码可以正常工作。可能是什么问题呢?
虽然文档中没有明确说明,但我认为 Hibernate 支持这种类型作为 @Version 字段。
此处提供了一个测试用例:https ://github.com/imeszaros/jooby-hbm-localdatetime