gpt4 book ai didi

java - 使用 Java EE 的网关模式

转载 作者:搜寻专家 更新时间:2023-11-01 03:38:23 25 4
gpt4 key购买 nike

我在我拥有的 Java EE 项目中使用网关模式:

A stateful session bean with extended presistence context.

The bean has transaction attribute set to not supported in order to stop any transaction to be commited into the database.

The bean has a save method with transaction attribute set to require new which will lead to a new transaction that will commit all the changes of the entity and reflects them to the database.

查看网关:adam bein page-67

直到两天前,我还在使用 GlassFish 3,一切正常。我检索一个实体,进行更改,然后在不合并实体的情况下调用保存方法。实体更改被刷新到数据库。但是当我将 GlassFish 更改为 GlassFish 4 时,此操作停止了,现在我必须合并实体以便将更改后的实体刷新到数据库中。

最佳答案

Save 专门做一个插入。因此再次调用 save 将在数据库中产生另一行。

更新更新 session 中的对象。因此,如果对象在 session 中,它将更新。如果对象不在 session 中,则应调用合并。

因此,如果您检索一个已经存在的实体,您必须调用更新/合并而不是保存。

JPA specification

The semantics of the merge operation applied to an entity X are as follows: If X is a detached entity, the state of X is copied onto a pre-existing managed entity instance X' of the same identity or a new managed copy X' of X is created.

If X is a new entity instance, a new managed entity instance X' is created and the state of X is copied into the new managed entity instance X'.

If X is a removed entity instance, an IllegalArgumentException will be thrown by the merge operation (or the transaction commit will fail).

If X is a managed entity, it is ignored by the merge operation, however, the merge operation is cascaded to entities referenced by relationships from X if these relationships have been annotated with the cascade element value cascade=MERGE or cascade=ALL annotation. For all entities Y referenced by relationships from X having the cascade element value cascade=MERGE or cascade=ALL, Y is merged recursively as Y'. For all such Y referenced by X, X' is set to reference Y'. (Note that if X is managed then X is the same object as X'.)

If X is an entity merged to X', with a reference to another entity Y, where cascade=MERGE or cascade=ALL is not specified, then navigation of the same association from X' yields a reference to a managed object Y' with the same persistent identity as Y.

关于java - 使用 Java EE 的网关模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22386378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com