-->
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.  [ 4 posts ] 
Author Message
 Post subject: Having Problem with Many to Many Mapping
PostPosted: Wed Dec 28, 2005 2:02 am 
Beginner
Beginner

Joined: Mon Oct 17, 2005 1:58 am
Posts: 27
I am trying to implement a many to many mapping between two table objects by introducing an intermediate link table. When I insert data into the two main tables I want the data to be inserted or updated automatically into the link table.

But data is not automatically inserted into the link table.Ihave to manually insert it.

Can anybody help me in this regard?

Thanks..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 3:52 am 
Regular
Regular

Joined: Mon Jul 18, 2005 4:10 am
Posts: 92
Location: Poland
Properly mapped many-to-many collection should maintain "link table" transparently.

Could you post a code sample (your mapping at least)?

_________________
michal


Top
 Profile  
 
 Post subject: These are the Mapping Files
PostPosted: Wed Dec 28, 2005 5:17 am 
Beginner
Beginner

Joined: Mon Oct 17, 2005 1:58 am
Posts: 27
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-access="nosetter.camelcase">
<class name="QandADomainModel.QandAAuthenticationQuestion, QandADomainModel" table="QA_AUTHENTICATION_QUESTIONS" >
<id name="Id" column="QUESTION_ID" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property name="Text" column="TEXT" type="string" not-null="true" />
<property name="QuesLanguage" column="QUES_LANGUAGE" type="string" not-null="true" />
<property name="CreatedDate" column="CREATION_DATE" type="DateTime" not-null="true" />
<many-to-one name="QAClientApp" column="QA_CLIENT_APP_ID" class="QandADomainModel.QandAClientApplication, QandADomainModel" not-null="true" />
<property name="Status" column="STATUS" type="string" not-null="true" />
<bag name="QAAuthAnswers" lazy="false" inverse="false" cascade="all-delete-orphan" >
<key column="QUESTION_ID"/>
<one-to-many class="QandADomainModel.QandAAuthenticationAnswer, QandADomainModel"/>
</bag>
<bag name="QAUsers" table="QA_QUESTIONS_USERS" inverse="false" >
<key column="QUESTION_ID" />
<many-to-many column="USER_ID" class="QandADomainModel.QandAUser,QandADomainModel" />
</bag>
</class>

<class name="QandADomainModel.QandAUser, QandADomainModel" table="QA_USERS">
<id name="Id" type="Int32" column="USER_ID">
<generator class="native" />
</id>
<bag name="QAAuthenticationQuestions" table="QA_QUESTIONS_USERS" lazy="true" inverse="true" >
<key column="USER_ID" />
<many-to-many column="QUESTION_ID" class="QandADomainModel.QandAAuthenticationQuestion,QandADomainModel" />
</bag>
<property name="UserName" column="USER_NAME" type="String" />
<property name="CreationDate" column="CREATION_DATE" type="DateTime" />
<property name="Status" column="STATUS" type="String" />
<many-to-one name="QAClientApplication" column="CLIENT_APP_ID" class="QandADomainModel.QandAClientApplication, QandADomainModel" />
</class>

<class name="QandADomainModel.QandAClientApplication,QandADomainModel" table="QA_CLIENT_APPLICATIONS" >
<id name="Id" column="QA_CLIENT_APP_ID" type="Int32" unsaved-value="0" >
<generator class="native"/>
</id>

<property name="QAClientApplication" column="QA_CLIENT_APPLICATION_NAME" type="string" not-null="true" unique="true" />
<property name="KeyName" column="KEY_NAME" type="string" not-null="true" unique="true" />
<property name="CreatedDate" column="CREATION_DATE" type="DateTime" not-null="true" />
<property name="Status" column="STATUS" not-null="true" />
<bag name="QAAuthQuestions" lazy="false" inverse="true" cascade="all-delete-orphan" >
<key column="QA_CLIENT_APP_ID"/>
<one-to-many class="QandADomainModel.QandAAuthenticationQuestion,QandADomainModel"/>
</bag>
</class>

<class name="QandADomainModel.QandAAuthenticationAnswer, QandADomainModel" table="QA_AUTHENTICATION_ANSWERS" >
<id name="Id" column="ANSWER_ID" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property name="Text" column="TEXT" type="string" not-null="true" />
<many-to-one name="User" column="USER_ID" class="QandADomainModel.QandAUser, QandADomainModel" not-null="true" />
<many-to-one name="QAAuthQuestion" column="QUESTION_ID" class="QandADomainModel.QandAAuthenticationQuestion, QandADomainModel" not-null="true" />
<many-to-one name="QAClientApp" column="QA_CLIENT_APP_ID" class="QandADomainModel.QandAClientApplication, QandADomainModel" not-null="true" />
<property name="Status" column="STATUS" type="string" not-null="true" />
<property name="CreatedDate" column="CREATION_DATE" type="DateTime" />
<property name="LastModifiedDate" column="LAST_MODIFICATION_DATE" type="DateTime"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 5:58 am 
Regular
Regular

Joined: Mon Jul 18, 2005 4:10 am
Posts: 92
Location: Poland
First - I am not a many-to-many guru, but I have several such collections performing well in my apps. If I were you I would try the following:

- try cascade="all" at non-inverse end of collection
- make sure you are adding objects to collection at non-inverse end ( QandAAuthenticationQuestion1.QAUsers.Add(QandAUser1) )
- replace "bag" with "set" - bags are VERY ineffective here (the whole collection must be deleted and recreated in the db with every change)

_________________
michal


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.