Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3.2.6GA
Name and version of the database you are using: Mysql5.1
The generated SQL (show_sql=true):
Hibernate: select bookmark0_.id as id8_0_, bookmark0_.location as location8_0_, bookmark0_.name as name8_0_ from Bookmark bookmark0_ where bookmark0_.id=?
Hibernate: select entries0_.Bookmark_id as Bookmark1_1_, entries0_.entries_id as entries2_1_, bookmarken1_.id as id10_0_, bookmarken1_.description as descript2_10_0_, bookmarken1_.url as url10_0_ from Bookmark_BookmarkEntry entries0_ left outer join BookmarkEntry bookmarken1_ on entries0_.entries_id=bookmarken1_.id where entries0_.Bookmark_id=?
Hibernate: insert into BookmarkEntry (description, url) values (?, ?)
Hibernate: delete from Bookmark_BookmarkEntry where Bookmark_id=?
Hibernate: insert into Bookmark_BookmarkEntry (Bookmark_id, entries_id) values (?, ?)
Hibernate: insert into Bookmark_BookmarkEntry (Bookmark_id, entries_id) values (?, ?)
Hibernate: insert into Bookmark_BookmarkEntry (Bookmark_id, entries_id) values (?, ?)
Hibernate: insert into Bookmark_BookmarkEntry (Bookmark_id, entries_id) values (?, ?)
Hibernate: insert into Bookmark_BookmarkEntry (Bookmark_id, entries_id) values (?, ?)
A Bokmark Entity has a list of BookmarkEntry
@OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.LAZY)
public Collection<BookmarkEntry> getEntries() {
return _entries;
}
Nothing is defined in the Bookmark entry entity (unidirectional mapping)
Code to add an Entry in the bookmark:
EntityManagerFactory emf =
Persistence.createEntityManagerFactory(JPA_PROVIDER);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Bookmark bookmark = em.getReference(Bookmark.class, 1);
BookmarkEntry entry = new BookmarkEntry();
entry.setUrl("URL4");
bookmark.getEntries().add(entry);
em.merge(bookmark);
em.getTransaction().commit();
SQL request is in the SQL trace section.
Why is the delete generate before inserting elements ??
It seems not optimized to insert 1 element:
1 delete + n insert !!!