2

I'm building a small eclipse rcp with a little bit of JPA. Now something strange happens:

I create some TopCategories with some SubCategories, this works as intended. The inserts are printed in the log. I close my application and now the problem raises up:

The Categories have a relation to books

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();
    }

}

After restart and querying Book with this select b from Book all SubCategories which aren't used getting deleted. If a SubCategory has a relation to Book it stays in my DB. Why this occures?

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);
    }
}

I'm using Eclipselink 2.1.1.

Regards

onigunn
  • 4,730
  • 10
  • 58
  • 89
  • Alright - I've found the problem: One of the result lists from my model is modified from a contentprovider - this is were not uses SubCategories are removed, in case that the entity objects weren't detached, it caused the entitymanger to update. – onigunn Dec 16 '10 at 18:03

1 Answers1

0

Alright - I've found the problem: One of the result lists from my model is modified from a contentprovider - this is were not used SubCategories are removed, in case that the entity objects weren't detached, it caused the entitymanger to update.

onigunn
  • 4,730
  • 10
  • 58
  • 89