JPA EclipseLink父子

时间:2015-02-13 09:50:19

标签: jpa eclipselink one-to-many entity-relationship

我目前正在使用2个具有OneToMany关系的类。一个类包含目录(您可以将其视为书);另一个类包含模板(您可以将其视为页面)。在这种情况下,一个模板只能属于一个目录,因此我使用了OneToMany关系。

我的应用程序运行良好,直到我重新启动服务。它目前在MaxDB下的Hana Cloud Platform上运行。我正在使用JPA和eclipselink(我使用@AdditionalCriteria来管理我的多租户,因为JPA提供的多租户不允许我对多个租户进行查询)。

以下是我的目录代码的摘录:

@Entity
@Table(name = "Catalog")
@AdditionalCriteria("(:adminAccess = 1 or this.customerId=:customerId) AND (:allStatus = 1 or this.statusRecord = :statusRecord)")


public class Catalog implements Serializable {  
private static final long   serialVersionUID    = -3906948030586841482L;

@Id
@GeneratedValue
private long                id;

[...]

@OneToMany(cascade = ALL, orphanRemoval = false, fetch = EAGER, mappedBy = "catalog")
private Set<Template>       templates           = new HashSet<Template>();

[...]

public void setTemplate(Template template) {
    this.templates.add(template);
}
}

模板的代码如下:

@Entity
@Table(name = "Template")
@AdditionalCriteria("(:adminAccess = 1 or this.customerId=:customerId) AND (:allStatus = 1 or this.statusRecord = :statusRecord)")
public class Template implements Serializable {
    private static final long       serialVersionUID    = 5268250318899275624L;

    @Id
    @GeneratedValue
    private long                    id;

    [...]

    @ManyToOne(cascade = ALL, fetch = EAGER)
    @JoinColumn(name = "catalog_id", referencedColumnName = "id")
    private Catalog                 catalog;

    public void setCatalog(Catalog catalog) {
        this.catalog = catalog;

        if(!catalog.getTemplate().contains(this))
            catalog.getTemplate().add(this);
    }
}

在我的Servlet中,我只使用Catalog来进行操作。如果我必须保存模板,我会从目录中读取它,在模板中进行修改并保留目录。

在我重新开始服务之前,它非常有用。

目录不再对模板有任何引用但模板仍然引用它曾经属于的目录。

你能指出我正确的方向吗?

由于

0 个答案:

没有答案