EclipseLink持久性并刷新两个关联实体 [英] EclipseLink persistence and refresh both association entities

查看:90
本文介绍了EclipseLink持久性并刷新两个关联实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在带有PostgreSQL数据库的Java EE项目中使用EclipseLink. 当我坚持/刷新属于oneToMany/manytoOne关联的实体时,我很难找到正确的注释以实现正确的行为.

I'm using EclipseLink in my Java EE project with a PostgreSQL database. I have trouble finding the right annotations for a correct behaviour when I persist/refresh entities which are part of oneToMany/manytoOne associations.

例如,我有一个实体"Project",该实体首先由用户通过UI创建.然后,用户可以将分析"添加到他们创建的项目中. 因此,分析是一个项目的一部分,一个项目可以进行多个分析. 持久操作可以正常工作(我在数据库中看到了与该项目关联的新分析),但是当我调用"getProject(idProject)"并显示其分析列表时,我看不到刷新的列表.

For example, I have an entity "Project" which is first created by the users through a UI. Then the users can add "Analysis" into the project they created. Hence, an analysis is part of one project and a project can have several analysis. The persist action works fine (I see a new analysis associated with the project in my database) but when I call "getProject(idProject)" and display the list of its analysis I don't get to see the refreshed list.

我应该怎么做?

当前注释为:

在Projet.java

In Projet.java

   // bi-directional many-to-one association to Analyse
   @OneToMany(mappedBy = "projet", cascade = { CascadeType.ALL })
   private Set<Analyse> analyses;

在Analyse.java中

In Analyse.java

  //bi-directional many-to-one association to Projet
@ManyToOne(cascade={CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name="id_projet", nullable=false)
private Projet projet;

用于保留新分析"的代码:

The code to persist a new "analyse" :

public boolean insertAnalyse(Analyse analyse) {
    System.out.println("insertAnalyse");
    boolean success = true;
    fac = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
    em = fac.createEntityManager();

    try {
        em.getTransaction().begin();
        em.persist(analyse);
            em.getTransaction().commit();
    }

    catch (Exception ex) {
        em.getTransaction().rollback();
        System.out.println("*** Erreur in MeansDAO. insertAnalyse() : "
                + ex.getMessage());
        success = false;

    } finally {
        em.close();
    }

    return success;
}

通过projectID检索当前项目的代码:

The code to retrieve the current project by projectID :

   public Projet getProject(String p) {
    System.out.println("getProject");
    Projet project = null;

    fac = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
    em = fac.createEntityManager();

    try {
        em.getTransaction().begin();
        StringBuilder sql = new StringBuilder();

        Projet proj = em.find(Projet.class, Integer.parseInt(p));

        if (proj != null) {
            project = proj;
        }

    } catch (Exception ex) {
        em.getTransaction().rollback();
        System.out.println("*** Erreur in MeansDAO. getProject() : "
                + ex.getMessage());
    } finally {
        em.close();
    }

    return project;
}

提前感谢您的帮助!

推荐答案

首先,如果您的代码正确,则无需刷新.当您坚持分析时,如果它与项目有关,则应该同时设置两种关系.我的猜测是您没有这样做.在JPA中,您有责任正确维护您的关系.

First of all, you should not need to refresh, if your code was correct. When you persist the analyse, if it is related to the project, then you should be setting the relationship both ways. My guess is you are not doing that. In JPA you are responsible for maintaining your relationships correctly.

看, http://en.wikibooks.org/wiki/Java_Persistence_then_after_update_up_ion_up_ion_relation_up_ion_side_up_Centre_side_side2.

之所以看到这种关系处于陈旧状态,是因为(因为您既没有维护双方,也有)因为EclipseLink默认情况下维护着一个共享缓存.您可以禁用它,

The reason you are seeing the stale state of the relationship is (because you did not maintain both sides, but also) because EclipseLink maintains a shared cache by default. You can disable this,

看, http://wiki.eclipse.org/EclipseLink/FAQ/How_to_disable_the_shared_cache%3F

要刷新JPA中的对象,您需要在其上调用refresh(),或使用刷新查询提示执行Query或find()操作.

To refresh an object in JPA you need to call refresh() on it, or execute a Query or find() operation using the refresh query hint.

有关缓存的更多信息,请参见 http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching

For more information on caching see, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching

这篇关于EclipseLink持久性并刷新两个关联实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆