JPA,Eclipselink - 在新交易中删除关系

时间:2010-12-16 13:39:29

标签: java jpa eclipselink

我正在用一点点JPA构建一个小的日食rcp。现在出现了一些奇怪的事情:

我使用一些SubCategories创建一些TopCategories,这可以按预期工作。插入物印在日志中。我关闭了我的应用程序,现在问题出现了:

类别与书籍有关系

Book.java

@Entity
public class Book implements Serializable, PropertyChangeListener {

    private static final long serialVersionUID = 4646743297687986216L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;
    private boolean active = true;

    @Temporal(TemporalType.TIMESTAMP)
    private Date updated;

    @Lob
    private Set<Group> allowedGroups;

    @Column(columnDefinition = "TEXT")
    private String text;

    private BookType type;

    @ManyToOne
    private TopCategory topCategory;

    @ManyToOne
    private SubCategory subCategory;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private BookAttachment attachment;


    @Transient
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    // ordinary getter/setter

    @PrePersist
    @PreUpdate
    private void updateUpdated() {
        this.updated = new Date();
    }

}

重新启动并查询Book select b from Book所有未使用的子类别被删除。如果SubCategory与Book有关系,它将保留在我的DB中。为什么会这样?

Category.java

@MappedSuperclass
public class Category implements Serializable {

    private static final long serialVersionUID = 6091963773161164543L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String name;

    @Temporal(TemporalType.TIMESTAMP)
    private Date updated;

    @Enumerated(EnumType.STRING)
    private CategoryType type;

    @Transient
    private List<Snippet> snippets = new LinkedList<Snippet>();

    // ordinary getter/setter

    @PrePersist
    @PreUpdate
    public void updateUpdated() {
        this.updated = new Date();
    }

}

TopCategory.java

@Entity
public class TopCategory extends Category {

    @OneToMany(cascade = CascadeType.ALL)
    private List<SubCategory> subCategories;

    public TopCategory() {
        setName("");
        setSubCategories(new ArrayList<SubCategory>());
    }

    public List<SubCategory> getSubCategories() {
        return subCategories;
    }

    public void setSubCategories(List<SubCategory> subCategories) {
        this.subCategories = subCategories;
    }

    @Override
    public void setType(CategoryType type) {
        super.setType(CategoryType.topCategory);
    }

SubCategory.java

@Entity
public class SubCategory extends Category {

    @ManyToOne(cascade = CascadeType.ALL)
    private TopCategory topCategory;

    public TopCategory getTopCategory() {
        return topCategory;
    }

    public void setTopCategory(TopCategory topCategory) {
        this.topCategory = topCategory;
    }

    @Override
    public void setType(CategoryType type) {
        super.setType(CategoryType.subCategory);
    }
}

我正在使用Eclipselink 2.1.1。

此致

1 个答案:

答案 0 :(得分:0)

好吧 - 我发现了问题:我的模型中的一个结果列表是从内容提供者修改的 - 这是未使用的子类别被删除,如果实体对象没有分离,它会导致实体操作员更新。