Dear All,
I have following tables in the database::
fixture,team,fixture_team,fixture_team_player,player,inning
fixture and team have many-to-many relationship with fixture_team being the middle table.Further fixture_team and player have
many-to-many relationship with player with fixture_team_player the middle table.Also fixture_team and inning have one-to-many relationship...
I have mapped following entities::
Fixture,Team,FixtureTeam,Player,Inning
XML mapping of entities are::
Code:
<class name="Fixture" table="fixture">
<id name="id">
<generator class="native"/>
</id>
<set name="details" inverse="true" cascade="save-update">
<key column="fixture_id"/>
<one-to-many class="FixtureDetail"/>
</set>
<set name="teams" table="fixture_team" cascade="all,delete-orphan">
<key column="fixture_id"/>
<many-to-many column="team_id"
unique="true"
class="Team"/>
</set>
</class>
<class name="FixtureTeam" table="fixture_team">
<composite-id >
<key-many-to-one name="team" class="Team" column="team_id" />
<key-many-to-one name="fixture" class="Fixture" column="fixture_id" />
</composite-id>
<set name="inning" cascade="save-update">
<key>
<column name="fixture_id"/>
<column name="team_id"/>
</key>
<one-to-many class="Inning"/>
</set>
<set name="players" table="fixture_team_player" cascade="save-update">
<key>
<column name="fixture_id"/>
<column name="team_id"/>
</key>
<many-to-many column="player_id"
unique="true"
class="Player"/>
</set>
</class>
<class name="Team" table="team">
<id name="id">
<generator class="native"/>
</id>
<set name="fixtures" inverse="true" cascade="save-update">
<key column="team_id"/>
<one-to-many class="FixtureTeam"/>
</set>
</class>
<class name="Player" table="player">
<id name="id" column="id">
<generator class="native"/>
</id>
<set name="fixtureTeams" inverse="true" table="fixture_team_player" cascade="save-update">
<key>
<column name="player_id"/>
</key>
<many-to-many unique="true"
class="FixtureTeam">
<column name="fixture_id"/>
<column name="team_id"/>
</many-to-many>
</set>
</class>
<class name="Inning" table="inning">
<id name="id" column="id">
<generator class="native"/>
</id>
<many-to-one name="fixtureTeam" class="FixtureTeam" not-null="true">
<column name="fixture_id"/>
<column name="batting_team_id"/>
</many-to-one>
</class>
When the following code is executed to instantiate a new FixtureTeam and add set of Players to it,
Code:
FixtureTeam fixtureTeam = new FixtureTeam();
fixtureTeam.setTeam(team);
fixtureTeam.setFixture(fixture);
Player p1 = new Player();
Player p2 = new Player();
Player p3 = new Player();
AspectVariation battingStyle = (AspectVariation)s.load(AspectVariation.class, new Integer(4));
AspectVariation bowlingStyle = (AspectVariation)s.load(AspectVariation.class, new Integer(1));
p1.setBattingStyle(battingStyle);
p1.setBowlingStyle(bowlingStyle);
p2.setBattingStyle(battingStyle);
p2.setBowlingStyle(bowlingStyle);
p3.setBattingStyle(battingStyle);
p3.setBowlingStyle(bowlingStyle);
Set set = new HashSet();
set.add(p1);
set.add(p2);
set.add(p3);
fixtureTeam.setPlayers(set);
s.save(fixtureTeam);
s.getTransaction().commit();
it throws following exception
Code:
org.hibernate.exception.ConstraintViolationException: could not insert collection: [uk.co.planetbeyond.Beans.FixtureTeam.players#component[team,fixture]{fixture=uk.co.planetbeyond.Beans.Fixture#2, team=uk.co.planetbeyond.Beans.Team#19}]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1205)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at uk.co.planetbeyond.test.main(test.java:150)
Caused by: java.sql.SQLException: INSERT statement conflicted with TABLE FOREIGN KEY constraint 'FK__fixture_team_pla__10216507'. The conflict occurred in database 'cricketLive', table 'fixture_team'.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:365)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2781)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2224)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:628)
at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:525)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:487)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:421)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1168)
... 10 more
TABLE FOREIGN KEY constraint 'FK__fixture_team_pla__10216507' is the relationship b/w fixture_team_player and player.Exception shows that this relationship is being violated But it shud not .If anybody can help.Thanks in anticipation