-->
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: Hibernate: Composite ID as a Foreign Key
PostPosted: Tue Oct 27, 2009 12:22 pm 
Newbie

Joined: Wed Oct 07, 2009 2:53 am
Posts: 5
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


Top
 Profile  
 
 Post subject: Re: Hibernate: Composite ID as a Foreign Key
PostPosted: Tue Oct 27, 2009 12:34 pm 
Newbie

Joined: Wed Oct 07, 2009 2:53 am
Posts: 5
Again on executing this code to create a new FixtureTeam and assign a inning to it

Code:
           FixtureTeam fixtureTeam = new FixtureTeam();
           fixtureTeam.setTeam(team);
           fixtureTeam.setFixture(fixture);
     
           Inning ing1 = new Inning();
           Inning ing2 = new Inning();
           Set set = new HashSet();
           set.add(ing1);
           set.add(ing2);
           fixtureTeam.setInning(set);
       
           s.save(fixtureTeam);
           s.getTransaction().commit();


It throws following Exception::

Code:
org.hibernate.exception.ConstraintViolationException: could not insert: [uk.co.planetbeyond.Beans.Inning]
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
   at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2176)
   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2656)
   at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
   at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
   at  org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
   at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:535)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:527)
   at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:241)
   at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:292)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:240)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:193)
   at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:320)
   at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:266)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:243)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:193)
   at org.hibernate.engine.Cascade.cascade(Cascade.java:154)
   at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:479)
   at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:357)
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:144)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
   at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
   at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
   at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:563)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:551)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:324)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
   at $Proxy0.save(Unknown Source)
   at uk.co.planetbeyond.test.main(test.java:166)
Caused by: java.sql.SQLException: Cannot insert the value NULL into column 'batting_team_id', table 'cricketLive.dbo.inning'; column does not allow nulls. INSERT fails.
   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.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
   at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
   ... 41 more


I am not getting it.......


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.