-->
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: many-to-many inserts not producing correct rows in database
PostPosted: Wed Apr 20, 2005 6:46 pm 
Newbie

Joined: Wed Apr 20, 2005 6:17 pm
Posts: 2
Hibernate version: 3.0

Mapping documents:

Snippet:

Team.hbm.xml:

<hibernate-mapping
>
<class
name="Team"
table="Teams"
>
<id
name="id"
column="team_id"
type="long"
unsaved-value="0"
>
<generator class="sequence">
<param name="sequence">team_seq</param>
</generator>
</id>

<bag
name="players"
table="team_players"
lazy="false"
inverse="false"
cascade="all"
>

<key
column="team_id"
>
</key>

<many-to-many
class="Player"
column="player_id"
outer-join="auto"
/>

</bag>

Player.hbm.xml:

<hibernate-mapping
>
<class
name="Player"
table="Players"
>
<bag
name="teams"
table="team_players"
lazy="false"
cascade="none"
inverse="true"
>

<key
column="player_id"
>
</key>

<many-to-many
class="Team"
column="team_id"
outer-join="auto"
/>

</bag>


Code between sessionFactory.openSession() and session.close():

Session session = sessionFactory.openSession();
Team team = new Team();
team.getPlayers().add(new Player());
team.getPlayers().add(new Player());
team.getPlayers().add(new Player());
session.save(team);
session.flush();
session.close();

Session session = sessionFactory.openSession();
Team team2 = new Team();
team2.setPlayers(team.getPlayers());
session.save(team2);
session.flush();
session.close();

Full stack trace of any exception that occurs: None

Name and version of the database you are using:
Oracle 10g

The generated SQL (show_sql=true):

For first team:

insert into teams ...
insert into players ...
nsert into players ...
nsert into players ...
insert into team_players ...
insert into team_players ...
insert into team_players ...

For second team:

insert into teams ...
update players ...
update players ...
update players ...
delete from team_players ... where team_id = ?
insert into team_players ...
insert into team_players ...
insert into team_players ...

-- ? is the old id presumably as only the rows associated
-- with team 2's id exists in team_players
-- Expected entries in team_players for both team 1 and 2

Question: why is hibernate doing the delete for the second team
save?

what should I be using to get the desired results as teams could have
the same players in them, e.g. player plays basketball and baseball.

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: solved:
PostPosted: Thu Apr 21, 2005 10:46 am 
Newbie

Joined: Wed Apr 20, 2005 6:17 pm
Posts: 2
Needed to do

Collection players2 = new LinkedList(team.getPlayers());
team2.setPlayers(players2);

Then save works as expected.
Thought I had tried this and it didn't work, but did an individual
player and it worked so tried above again and it worked.


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.