添加实体不会刷新父级的集合 [英] Adding entity doesn't refresh parent's collection

查看:125
本文介绍了添加实体不会刷新父级的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题和问题非常简单,尽管很烦人,我正在寻找一个全局解决方案,因为这对我们来说是整个应用程序的问题.

the question and problem is pretty simple, though annoying and I am looking for a global solution, because it's application-wide problem for us.

下面的代码确实没有意思,但我将其发布以进行澄清!

我们将PostgreSQL数据库与JPA 2.0一起使用,并且生成了所有的外观和实体,当然,我们进行了一些编辑,但实际上并没有太多.

We use PostgreSQL database with JPA 2.0 and we generated all the facades and entities, of course we did some editing but not much really.

问题在于,每个实体都包含其子级集合,但是(仅对我们而言?)在创建后不会更新子级元素. 对象已写入数据库,您可以轻松选择它们,但我们真正希望看到的是刷新后的父对象中的子代集合.

The problem is that every entity contains a Collection of its children, which however (for us only?) is NOT updated after creation a children element. The objects are written to database, you can select them easily, but what we really would like to see is the refreshed collection of children in parent object.

为什么会这样?如果我们(手动)刷新父级em.refresh(parent)的实体,它将成功,但我认为这对我们意味着Facades中的许多工作.但是也许没有别的办法了吗?

Why is this happening? If we (manually) refresh the entity of parent em.refresh(parent) it does the trick but it would mean for us a lot of work in Facades I guess. But maybe there is no other way?

感谢支持!

/*编辑*/

我想这一定是注释问题或缓存之类的东西,但是我已经尝试过了

I guess it has to be some annotation problem or cache or something, but I've already tried

@OneToMany(mappedBy = "idquestion", orphanRemoval=true, fetch= FetchType.EAGER) 

@Cacheable(false)

无法正常工作.

/*编辑*/

一些用于理解的示例代码.

Some sample code for understanding.

数据库级别:

CREATE TABLE Question (
        idQuestion SERIAL,
        questionContent VARCHAR,    
    CONSTRAINT Question_idQuestion_PK PRIMARY KEY (idQuestion)
);

CREATE TABLE Answer (
        idAnswer SERIAL,
        answerContent VARCHAR,
        idQuestion INTEGER,
    CONSTRAINT Answer_idAnswer_PK PRIMARY KEY (idAnswer),
    CONSTRAINT Answer_idQuestion_FK FOREIGN KEY (idQuestion) REFERENCES Question(idQuestion)
);

我们在Netbeans 7.1中生成了一些实体,它们看起来都类似于:

Than we have generated some Entities in Netbeans 7.1, all of them look similar to:

@Entity
@Table(name = "question", catalog = "jobfairdb", schema = "public")
@XmlRootElement
@NamedQueries({ BLAH BLAH BLAH...})
public class Question implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name = "idquestion", nullable = false)
    private Integer idquestion;
    @Size(max = 2147483647)
    @Column(name = "questioncontent", length = 2147483647)
    private String questioncontent;
    @OneToMany(mappedBy = "idquestion", orphanRemoval=true) 
    private Collection<Answer> answerCollection;

Getters... setters...

我们(再次)为其使用生成的外观,全部实现AbstractFacade,如:

We use (again) generated facades for them, all implementing AbstractFacade like:

public abstract class CCAbstractFacade<T> {
    private Class<T> entityClass;

    public CCAbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
    }

推荐答案

如果您使用

The father entity is updated automatically if you use container managed transactions and you fetch the collection after the transaction is complete. Otherwise, you have to update yourself the collection.

本文详细说明了此行为: JPA实施模式:双向关联

This article explains in detail this behaviour: JPA implementation patterns: Bidirectional associations

使用容器托管事务的最简单方法是在persistence.xml中包含transaction-type="JTA"并使用

The simplest way to use Container Managed Transactions is to have transaction-type="JTA" in persistence.xml and use Container-Managed Entity Managers.

这篇关于添加实体不会刷新父级的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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