是否可以更改版本化的实体实例,并在不使用flush的情况下获取要递增的版本?因为从我阅读的内容来看,我害怕冲洗不是一个好习惯,因为这对性能甚至是数据损坏都有不良影响?林不知道:d合并并获取未刷新的待更新版本?
这里有一个简单的代码,并输出作为注释:
/*
Hibernate: select receivingg0_.id as id9_14_, receivingg0_.creationDate as creation2_9_14_, ... too long
the version before modification : 16
the version after modification : 16
after merge the modification, the version is : 16
Hibernate: update ReceivingGood set creationDate=?, modificationDate=?, usercreate_id=?, usermodify_id=?, ... too long
after flushing the modification, the version is finally : 17
*/
public void modifyHeaderAndGetUpdatedVersion() {
String id = "3b373f6a-9cd1-4c9c-9d46-240de37f6b0f";
ReceivingGood receivingGood = em.find(ReceivingGood.class, id);
System.out.println("the version before modification : " + receivingGood.getVersion());
receivingGood.setTransactionNumber("NUM001xyz");
System.out.println("the version after modification : " + receivingGood.getVersion());
receivingGood = em.merge(receivingGood);
System.out.println("after merge the modification, the version is : " + receivingGood.getVersion());
em.flush();
System.out.println("after flushing the modification, the version is finally : " + receivingGood.getVersion());
}
在我的测试,该版本得到冲洗后递增。从合并操作返回的实例不具有增加的版本。
但在我的情况下,我想以DTO的形式将实体返回到我的webui,实体在将其转换为DTO并将其返回到UI之前应具有version-after-flush/commit被渲染。然后用户界面可以有最新版本,并将通过这个版本的下一个提交。
有没有什么办法可以在没有刷新的情况下获得最新版本?
谢谢!
UPDATE
以我的经验,人工增加这可能是问题的,因为可以从下面这个例子可以看出。在这个例子中,我们有2个冲洗。
第一个是同步对db连接的更改,以便来自同一连接的存储过程调用可以看到从entityManager所做的更改。
第二次刷新被调用来获得最终版本。我们可以看到这是递增两次。因此,从手动增量版本获得版本而无需刷新将不会在这种情况下工作,因为我们必须真正计数正在进行的刷新次数。
/*
Hibernate: select receivingg0_.id as id9_14_, receivingg0_.creationDate as creation2_9_14_, .. too long
the version before modification : 18
the version after modification : 18
after merge the modification, the version is : 18
now flushing the modification, so that the stored procedure call from the same connection can see the changes
Hibernate: update ReceivingGood set creationDate=?, modificationDate=?, usercreate_id=?, .. too long
after flushing the modification, the version is : 19
Hibernate: update ReceivingGood set creationDate=?, modificationDate=?, usercreate_id=?, .. too long
after the second flush, the version got increased again into : 20
*/
public void modifyHeaderAndGetUpdatedVersionWith2Flushes() {
String id = "3b373f6a-9cd1-4c9c-9d46-240de37f6b0f";
ReceivingGood receivingGood = em.find(ReceivingGood.class, id);
System.out.println("the version before modification : " + receivingGood.getVersion());
//auditEntity(receivingGood, getUser("3978fee3-9690-4377-84bd-9fb05928a6fc"));
receivingGood.setTransactionNumber("NUM001xyz");
System.out.println("the version after modification : " + receivingGood.getVersion());
receivingGood = em.merge(receivingGood);
System.out.println("after merge the modification, the version is : " + receivingGood.getVersion());
System.out.println("now flushing the modification, so that the stored procedure call from the same connection can see the changes");
em.flush();
System.out.println("after flushing the modification, the version is : " + receivingGood.getVersion());
receivingGood.setTransactionNumber("NUM001abc");
em.flush();
System.out.println("after the second flush, the version got increased again into : " + receivingGood.getVersion());
}
这是否意味着我真的不得不依赖最后的flush来获取修改实体的最新版本?
更新2
下面是将更新ReceivingGood实体,应返回DTO具有最新版本的业务方法的简单例子。
public ReceivingGoodDTO update(ReceivingGood entity) {
// merge it
entity = entityManager.merge(entity);
// the version is not incremented yet, so do the flush to increment the version
entityManager.flush(); // if i dont do this, the dto below will get the unincremented one
// use a mapper, maybe like dozer, to copy the properties from the entity to the dto object, including the newest version of that entity
ReceivingGoodDTO dto = mapper.map(entity, dto);
return dto;
}
这里是利用该方法的一个例子:
@Transactional
public ReceivingGoodDTO doSomethingInTheServiceAndReturnDTO() {
// do xxx ..
// do yyy ..
dto = update(entity);
return dto; // and the transaction commits here, but dto's version isnt increased because it's not a managed entity, just a plain POJO
}
@partenon:我了解如何刷新和提交作品,以及版本如何增加他们。但是我仍然会阅读你推荐的这本书:-)无论如何,我已经更新了我原来的帖子,关于手动增量的其他一些问题。一如既往地感谢你。 – bertie 2011-03-11 09:29:31
我刚刚更新了答案,以解决您的新问题:-) – jpkrohling 2011-03-11 09:40:41
@partenon:所以,我终于可以得出结论,要创建一个DTO里面的活动交易,反映实体与最新版本,我真的必须做一个齐平()。谢谢 ! – bertie 2011-03-11 09:46:55