0

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

如果我将类型更改lastUpdatejava.sql.Timestamp,则代码可以正常工作。可能是什么问题呢?

虽然文档中没有明确说明,但我认为 Hibernate 支持这种类型作为 @Version 字段。

此处提供了一个测试用例:https ://github.com/imeszaros/jooby-hbm-localdatetime

4

1 回答 1

0

仅作记录:Oracle的TIMESTAMP类型默认精度为6,只能存储毫秒。

然而, JavaLocalDateTime可能有纳秒,因此您需要TIMESTAMP(9)在数据库中使用才能在不损失精度的情况下存储值。

于 2020-10-05T12:02:11.160 回答