-->
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.  [ 5 posts ] 
Author Message
 Post subject: creating many-to-many relationship ...
PostPosted: Mon May 21, 2007 6:25 am 
Newbie

Joined: Tue Mar 20, 2007 5:13 am
Posts: 9
hi, i have problem trying to create a many-to-many relationship.

i have 2 classes:
Code:
public class Venue {
   private Integer venueId;
   private String venueType;
   private String venueName;
   private Set Club;

  // getter & setter
  // add & remove Club


Code:
public class Club{
   private Integer clubId;
   private String clubName;
   private Set Venue

  // getter & setter, add & remove


my hbm file is as follow
Venue.hbm.xml
Code:
...
        <set name="venueClubRelationship" table="club_venue" cascade="save-update">
            <key column="venueId"/>
            <many-to-many class="Club"/>
        </set>
...

Club.hbm.xml

Code:
...
        <set name="clubVenueRelationship" table="club_venue" cascade="save-update">
            <key column="clubId"/>
            <many-to-many class="Venue"/>
        </set>
...


when i try to test this code by running this following code:
Code:
...
Club club= (Club)new ClubDAO().getClubById(1);
Venue v = (Venue)new VenueDAO().getVenueById(7);
club.addVenue(v);
boolean success = new ClubDAO().save(club);
System.out.println("success: " + success);
...


it gives me this error:
Code:
   at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1394)
   at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:56)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at com.jjcs.fas.dbms.organisation.OrganisationDAO.save(OrganisationDAO.java:172)
   at com.jjcs.fas.dbms.organisation.Test_OrganisationDAO.<init>(Test_OrganisationDAO.java:43)
   at com.jjcs.fas.dbms.organisation.Test_OrganisationDAO.main(Test_OrganisationDAO.java:22)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Cannot insert the value NULL into column 'venueId', table 'DB_DBMS.dbo.club_venue'; column does not allow nulls. INSERT fails.
   at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
   at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
   at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
   at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
   at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
   at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
   at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
   at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
   at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
   at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
   at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
   at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
   at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
   at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
   at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1367)
   ... 10 more




how should i handle this?
I know the problem is because the field is not set as "allow-null" but how should I do that?

Could someone please explain me how should I fix this issue?

Thank you very much![/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 8:59 pm 
Newbie

Joined: Tue Mar 20, 2007 5:13 am
Posts: 9
no idea for this one?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 11:18 pm 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi gubrak,

In both HBM set you are referring same table. Dose it make sense


<set name="venueClubRelationship" table="club_venue" cascade="save-update">
<key column="venueId"/>
<many-to-many class="Club"/>
</set>

<set name="clubVenueRelationship" table="club_venue" cascade="save-update">
<key column="clubId"/>
<many-to-many class="Venue"/>
</set>

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 12:08 am 
Beginner
Beginner

Joined: Thu May 17, 2007 9:56 am
Posts: 21
Location: India
Hi,

I think you need to have same column name to map both the tables.

_________________
Kuzhali


Top
 Profile  
 
 Post subject: Not specifying owner/inverse of the relationship
PostPosted: Tue May 29, 2007 1:29 am 
Newbie

Joined: Tue May 29, 2007 12:33 am
Posts: 13
The way I see it, you have a many-to-many association but are not specifying which side is the owner and which is the inverse. In such situations, hibernate needs to know which side of the association it will use to perform the inserts, updates, .., etc.

Check this out:
http://www.hibernate.org/hib_docs/v3/re ... irectional


Then, in Venue.hbm.xml, change

Code:
<set name="venueClubRelationship" table="club_venue" cascade="save-update">
            <key column="venueId"/>
            <many-to-many class="Club"/>
        </set>


for

Code:
<set name="venueClubRelationship" table="club_venue" cascade="save-update" inverse="true">
            <key column="venueId"/>
            <many-to-many class="Club"/>
        </set>


Cheers!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.