I am building a JavaEE application using Netbeans 7.0.1, Glassfish 3.1 and ObjectDB 2.3.3.
I have a Book class with entity field Chapter which has a CascadeType.ALL property. The Book fields are as follows:
@Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String bookTitle; @OneToMany(cascade= CascadeType.ALL, fetch= FetchType.EAGER) private List<Chapter> chapters;
The Chapter fields are as follows:
@Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String chapterTitle;
The idea is that after I make changes to the Book class on the client side (i.e. by adding or removing new Chapters), merging the Book class on the server side should result in new and modified Chapters being persisted and merged accordingly.
This works as expected on the Book table, however with side effect on the Chapter table. For every new Chapter added to the Book, there are two Chapter records (usually added in sequential id) on the Chapter table when viewed in Explorer.
Let me also mention an additional observation I made: if the new chapters are created and added to the Book entity ON THE SERVER SIDE, then the Chapter table is OK (no duplicate records).
I have attached all the classes involved in the error case. I would like to know if there is a way I can make the error case scenario work as that would result in much easier development for me.