-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: @OneToMany unidirectionel: Too much SQL request generated !!
PostPosted: Sun Feb 24, 2008 2:37 pm 
Newbie

Joined: Wed Oct 25, 2006 1:13 pm
Posts: 6
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 !!!


Top
 Profile  
 
 Post subject: Re: @OneToMany unidirectionel: Too much SQL request generate
PostPosted: Thu Feb 28, 2008 12:07 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
What if you remove the merge line?


Farzad-


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.