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=?