java - 合并操作无异常但对数据库没有影响 Glassfish 4.1 JPA

标签 java mysql jpa entitymanager glassfish-4.1

我得到了一个带有字符串字段的表,并尝试更新该字段并通​​过 EntityManager 将更新合并到数据库。这已经完成,无一异常(exception),但我发现对数据库没有任何影响,并且不知道那里有什么。希望有人有想法。在下面的示例中,通过 match != null 和两个打印语句,我将预期值写入到我的日志中。但即使提交终止,事务内的合并也不会影响数据库。

@Named
@SessionScoped
@ManagedBean
public class LoginController implements Serializable {

    @PersistenceUnit
    private EntityManagerFactory emf;

    public String mymethod(){
        ...
        match.setPwdResetId(rs);
        System.out.println("reset it is now "+match.getPwdResetId());
        try{
            ut.begin();
            emf.createEntityManager().merge(match);
            ut.commit();
        }
        catch(Exception e){
            FacesMessage m2 = new FacesMessage("Values could not be saved. ");
            FacesContext.getCurrentInstance().addMessage("ResetForm", m2);
            System.out.println("exceptio persisting "+e);
            return "message.jsf";
        }  
        System.out.println("reset it is now2 "+match.getPwdResetId());

最佳答案

请分离实体并重试。 您可以在 jpa 2 规范中阅读更多内容。我发布了合并操作的示例:

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.

The persistence provider must not merge fields marked LAZY that have not been fetched: it must ignore such fields when merging. Any Version columns used by the entity must be checked by the persistence runtime implementation during the merge operation and/or at flush or commit time. In the absence of Version columns there is no additional version checking done by the persistence provider runtime during the merge operation.

您可以找到整个pdf here

关于java - 合并操作无异常但对数据库没有影响 Glassfish 4.1 JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38489639/

相关文章:

java - 如何比较 mongoDB spring 数据中的两个字符串?

java.lang.NoSuchFieldError : DESCR

mysql - 如何在所有表和所有列中查找字符串?

mysql - Perl将许多插入语句插入mysql数据库

postgresql - 使用@Query JPA 注释将空参数传递给 native 查询

java - EntityManager.remove 和 EntityManager.persist 上的 JPA 重复条目错误

java - 一个方法可以返回不同的数据类型吗?

java - JPA:JQL 和 SQL 的区别

java - 输入中的错误值 - 没有正确的 UTF-8 编码? Tomcat ?

php - 需要帮助显示最后 5 行。 PHP、MySQL