我有一个带有实体和DAO类的quarkus应用程序。
现在,我尝试通过调用EntityManager的merge((函数来更新实体:
public void update(final T valueObject) {
getEntityManager().merge(valueObject);
}
EntityManager被注入:
@Inject
@PersistenceUnit("MY_DB")
protected EntityManager _entityManager;
DAO的类是@Singleton
当我调用update()
时,我没有得到异常,日志中也没有信息,但数据库没有更新。当我在entityManager上执行persist()
或remove()
时,它会按预期工作。
我处理交易:
@Inject
private EntityDAO entityDao;
QuarkusTransaction.begin();
entity.setValue("my value");
entityDao.update(entity);
QuarkusTransaction.commit();
有什么想法吗?这个问题可能出了什么问题?
编辑:日志。。。
DEBUG [org.hib.eve.int.AbstractFlushingEventListener] (executor-thread-0) Processing flush-time cascades
DEBUG [org.hib.eve.int.AbstractFlushingEventListener] (executor-thread-0) Dirty checking collections
DEBUG [org.hib.eve.int.AbstractFlushingEventListener] (executor-thread-0) Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
DEBUG [org.hib.eve.int.AbstractFlushingEventListener] (executor-thread-0) Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
DEBUG [org.hib.int.uti.EntityPrinter] (executor-thread-0) Listing entities:
DEBUG [org.hib.int.uti.EntityPrinter] (executor-thread-0) at....{THE UPDATED DATA}
DEBUG [at...LoggingInterceptor] (executor-thread-0) at....update() finished
所以我证明,如果休眠会话是脏的,
boolean before = _session.isDirty();
_session.merge(valueObject);
boolean after = _session.isDirty();
而且在合并之后会话是不脏的。
解决方案是错误的@Entity类
@Entity
public class ValueObject {
private String value;
@Column(name = "VALUE")
public getValue() {...}
public setValue(String value) {...}
}
必须更改为:
@Entity
public class ValueObject {
@Column(name = "VALUE")
private String value;
public getValue() {...}
public setValue(String value) {...}
}