简体   繁体   English

在不更新数据库的情况下合并/重新连接IN JPA / Hibernate

[英]merging / re-attaching IN JPA / Hibernate without updating the DB

Working with JPA / Hibernate in an OSIV Web environment is driving me mad ;) 在OSIV Web环境中使用JPA / Hibernate使我发疯;)

Following scenario: I have an entity A that is loaded via JPA and has a collection of B entities. 以下情况:我有一个通过JPA加载的实体A,并且有一个B实体的集合。 Those B entities have a required field. 这些B实体具有必填字段。

When the user adds a new B to A by pressing a link in the webapp, that required field is not set (since there is no sensible default value). 当用户通过按下Web应用程序中的链接将新的B添加到A时,未设置必填字段(因为没有明智的默认值)。

Upon the next http request, the OSIV filter tries to merge the A entity, but this fails as Hibernate complains that the new B has a required field is not set. 在下一个http请求时,OSIV筛选器尝试合并A实体,但这失败,因为Hibernate抱怨新的B的必填字段未设置。

javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value

Reading the JPA spec, i see no sign that those checks are required in the merge phase (i have no transaction active) 阅读JPA规范,我看不到在合并阶段需要进行这些检查的迹象(我没有活动的事务)

I can't keep the collection of B's outside of A and only add them to A when the user presses 'save' (aka entitymanager.persist()) as the place where the save button is does not know about the B's, only about A. 我不能将B的集合保留在A之外,而仅当用户按下“保存”(又名entitymanager.persist())时将它们添加到A中,因为保存按钮所在的位置不知道B的地方,只知道一种。

Also A and B are only examples, i have similar stuff all over the place .. 另外,A和B只是示例,我到处都有类似的内容。

Any ideas? 有任何想法吗? Do other JPA implementaions behave the same here? 其他JPA实现在这里的行为是否相同?

Thanks in advance. 提前致谢。

I did a lot reading and testing. 我做了很多阅读和测试。 The problem come from my misunderstanding of JPA / Hibernate. 问题来自我对JPA / Hibernate的误解。 merge() always does a hit on the DB and also schedules an update for the entity. merge()总是会对数据库产生影响,并且还会为实体安排更新。 I did not find any mention of this in the JPA spec, but the 'Java Persistence with Hibernate' book does mention it. 在JPA规范中,我没有提到这一点,但是“带有Hibernate的Java持久性”书中确实提到了这一点。

Looking through the EntityManager (and Session as fallback) API it looks as if there is no means of just assigning an entity to the current persistent context WITHOUT scheduling an update. 通过EntityManager(和Session作为后备)API查看,似乎没有办法在不安排更新的情况下仅将实体分配给当前持久性上下文。 After all, what I want is to navigate the object graph, changing properties as needed and trigger an update (with version check if needed) later on. 毕竟,我要导航的是对象图,根据需要更改属性,然后稍后触发更新(如果需要,请进行版本检查)。 Something i think every Webapp out there using ORM must do? 我认为使用ORM的每个Web应用都必须做什么?

The basic workflow i 'm looking for: 我正在寻找的基本工作流程:

  1. load an entity from the DB (or create a new one) 从数据库加载一个实体(或创建一个新实体)
  2. let the entity (and all its associations become detached (as the EntitManager closes at the end of a HTTP request) 让实体(及其所有关联变为分离)(因为EntitManager在HTTP请求结束时关闭)
  3. when the next HTTP request comes in, work again with those objects, navigating the tree without fear of LazyInitExceptions 当下一个HTTP请求进入时,再次使用这些对象,在树中导航,而不必担心LazyInitExceptions
  4. call a method that persists all changes made during 1-3) 调用一种方法,该方法将保留在1-3期间所做的所有更改)

With the OSIV filter from spring in conjunction with an IModel implementation from wicket i thought i have archived this. 结合Spring的OSIV过滤器和wicket的IModel实现,我认为我已经对此进行了归档。

I basically see 2 possible ways out of it: 我基本上看到了两种可能的解决方法:

a) load the entity and all the associations needed when entering a certain page (use case), letting them become detached, adding/ changing them as needed in the course of several http requests. a)进入特定页面(用例)时,加载实体和所需的所有关联,让它们分离,并在几个http请求过程中根据需要添加/更改它们。 Than reattach them when the user initiates a save (validators will ensure a valid state) and submit them to the database. 比用户启动保存时重新附加它们(验证程序将确保状态有效)并将它们提交到数据库。

b) use the current setup, but make sure that all newly added entities have all their required fields set (probably using some wizard components). b)使用当前设置,但请确保所有新添加的实体均设置了其所有必填字段(可能使用某些向导组件)。 i would still have all the updates to the database for every merge(), but hopefully the database admin won't realize ;) 对于每个merge(),我仍将具有数据库的所有更新,但是希望数据库管理员不会意识到;)

How do other people work with JPA in a web environment? 其他人如何在Web环境中使用JPA? Any other options for me? 还有其他选择吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM