-->
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: manytomany persist generates a massive number of selects
PostPosted: Sun May 18, 2008 11:45 pm 
Newbie

Joined: Tue Jun 27, 2006 2:51 pm
Posts: 14
Hi guys,
I have a textbook example of mapping a manytomany association using annotations. From the mappings and code you'll see below it seems to work with one problem. A persist of a film entity with a newly added actor entity will result in a series of close to 1000 select statements! This doesn't happen in all cases, if I don't add cascade settings to the Film class's mapping of actors, OR if the actor I'm adding to a new Film entity is new as well, then the persist goes fine without the selects occurring. But as it stands now, a manytomany with cascading settings applied to both sides will create these selects. I've been getting my mapping details from http://www.hibernate.org/hib_docs/annot ... ntity.html

Has anybody seen this before? Searching the forums here I couldn't find anything about this. Thanks in advance


Hibernate version:
Core: 3.2.5ga
Annotations: 3.3.1.ga

Mapping documents:
In my Actor class:

@ManyToMany(
targetEntity=com.mike.hib3.hibernatework.pojos.Film.class,
cascade={CascadeType.MERGE, CascadeType.PERSIST}
)
@JoinTable(
name="film_actor",
joinColumns={@JoinColumn(name="actor_id")},
inverseJoinColumns={@JoinColumn(name="film_id")}
)
private Set<Film> films = new HashSet<Film>();

In my Film class:
@ManyToMany(
mappedBy="films",
cascade={CascadeType.MERGE, CascadeType.PERSIST},
targetEntity=com.mike.hib3.hibernatework.pojos.Actor.class
)
private Set<Actor> actors = new HashSet<Actor>();

Code between sessionFactory.openSession() and session.close():
hibSession.beginTransaction();
Actor actor = (Actor)hibSession.load(Actor.class, 1);
Film f = new Film();
//apply setters
f.addActor(actor);
hibSession.persist(f);


Name and version of the database you are using:
MySql 5.0.51a

The generated SQL (show_sql=true):
Hibernate: select actor0_.actor_id as actor1_0_0_, actor0_.first_name as first2_0_0_, actor0_.last_name as last3_0_0_, actor0_.last_update as last4_0_0_ from actor actor0_ where actor0_.actor_id=?
Hibernate: select films0_.actor_id as actor1_2_, films0_.film_id as film2_2_, film1_.film_id as film1_1_0_, film1_.description as descript2_1_0_, film1_.language_id as language13_1_0_, film1_.last_update as last3_1_0_, film1_.length as length1_0_, film1_.original_language_id as original5_1_0_, film1_.rating as rating1_0_, film1_.release_year as release7_1_0_, film1_.rental_duration as rental8_1_0_, film1_.rental_rate as rental9_1_0_, film1_.replacement_cost as replace10_1_0_, film1_.special_features as special11_1_0_, film1_.title as title1_0_, language2_.language_id as language1_2_1_, language2_.last_update as last2_2_1_, language2_.name as name2_1_ from film_actor films0_ left outer join film film1_ on films0_.film_id=film1_.film_id inner join language language2_ on film1_.language_id=language2_.language_id where films0_.actor_id=?
Hibernate: insert into film (description, language_id, last_update, length, original_language_id, rating, release_year, rental_duration, rental_rate, replacement_cost, special_features, title) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select actors0_.film_id as film2_1_, actors0_.actor_id as actor1_1_, actor1_.actor_id as actor1_0_0_, actor1_.first_name as first2_0_0_, actor1_.last_name as last3_0_0_, actor1_.last_update as last4_0_0_ from film_actor actors0_ left outer join actor actor1_ on actors0_.actor_id=actor1_.actor_id where actors0_.film_id=?
Hibernate: select actors0_.film_id as film2_1_, actors0_.actor_id as actor1_1_, actor1_.actor_id as actor1_0_0_, actor1_.first_name as first2_0_0_, actor1_.last_name as last3_0_0_, actor1_.last_update as last4_0_0_ from film_actor actors0_ left outer join actor actor1_ on actors0_.actor_id=actor1_.actor_id where actors0_.film_id=?
Hibernate: select films0_.actor_id as actor1_2_, films0_.film_id as film2_2_, film1_.film_id as film1_1_0_, film1_.description as descript2_1_0_, film1_.language_id as language13_1_0_, film1_.last_update as last3_1_0_, film1_.length as length1_0_, film1_.original_language_id as original5_1_0_, film1_.rating as rating1_0_, film1_.release_year as release7_1_0_, film1_.rental_duration as rental8_1_0_, film1_.rental_rate as rental9_1_0_, film1_.replacement_cost as replace10_1_0_, film1_.special_features as special11_1_0_, film1_.title as title1_0_, language2_.language_id as language1_2_1_, language2_.last_update as last2_2_1_, language2_.name as name2_1_ from film_actor films0_ left outer join film film1_ on films0_.film_id=film1_.film_id inner join language language2_ on film1_.language_id=language2_.language_id where films0_.actor_id=?
Hibernate: select actors0_.film_id as film2_1_, actors0_.actor_id as actor1_1_, actor1_.actor_id as actor1_0_0_, actor1_.first_name as first2_0_0_, actor1_.last_name as last3_0_0_, actor1_.last_update as last4_0_0_ from film_actor actors0_ left outer join actor actor1_ on actors0_.actor_id=actor1_.actor_id where actors0_.film_id=?
Hibernate: select films0_.actor_id as actor1_2_, films0_.film_id as film2_2_, film1_.film_id as film1_1_0_, film1_.description as descript2_1_0_, film1_.language_id as language13_1_0_, film1_.last_update as last3_1_0_, film1_.length as length1_0_, film1_.original_language_id as original5_1_0_, film1_.rating as rating1_0_, film1_.release_year as release7_1_0_, film1_.rental_duration as rental8_1_0_, film1_.rental_rate as rental9_1_0_, film1_.replacement_cost as replace10_1_0_, film1_.special_features as special11_1_0_, film1_.title as title1_0_, language2_.language_id as language1_2_1_, language2_.last_update as last2_2_1_, language2_.name as name2_1_ from film_actor films0_ left outer join film film1_ on films0_.film_id=film1_.film_id inner join language language2_ on film1_.language_id=language2_.language_id where films0_.actor_id=?
Hibernate: select actors0_.film_id as film2_1_, actors0_.actor_id as actor1_1_, actor1_.actor_id as actor1_0_0_, actor1_.first_name as first2_0_0_, actor1_.last_name as last3_0_0_, actor1_.last_update as last4_0_0_ from film_actor actors0_ left outer join actor actor1_ on actors0_.actor_id=actor1_.actor_id where actors0_.film_id=?
Hibernate: select films0_.actor_id as actor1_2_, films0_.film_id as film2_2_, film1_.film_id as film1_1_0_, film1_.description as descript2_1_0_, film1_.language_id as language13_1_0_, film1_.last_update as last3_1_0_, film1_.length as length1_0_, film1_.original_language_id as original5_1_0_, film1_.rating as rating1_0_, film1_.release_year as release7_1_0_, film1_.rental_duration as rental8_1_0_, film1_.rental_rate as rental9_1_0_, film1_.replacement_cost as replace10_1_0_, film1_.special_features as special11_1_0_, film1_.title as title1_0_, language2_.language_id as language1_2_1_, language2_.last_update as last2_2_1_, language2_.name as name2_1_ from film_actor films0_ left outer join film film1_ on films0_.film_id=film1_.film_id inner join language language2_ on film1_.language_id=language2_.language_id where films0_.actor_id=?
Hibernate: select actors0_.film_id as film2_1_, actors0_.actor_id as actor1_1_, actor1_.actor_id as actor1_0_0_, actor1_.first_name as first2_0_0_, actor1_.last_name as last3_0_0_, actor1_.last_update as last4_0_0_ from film_actor actors0_ left outer join actor actor1_ on actors0_.actor_id=actor1_.actor_id where actors0_.film_id=?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 20, 2008 1:21 am 
Newbie

Joined: Tue Jun 27, 2006 2:51 pm
Posts: 14
hi guys, I figured it out and thought I'd share the knowledge. I was using the persist and merge versions of cascading types when I'm NOT using an entitymanager. I'm using straight hibernate3, no EJB3. So this for some reason causes all of those selects. Once I switched to org.hibernate.annotations.CascadeType all was well. Thanks


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.