-->
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.  [ 7 posts ] 
Author Message
 Post subject: Problem with Many to Many mapping
PostPosted: Thu Jul 31, 2008 8:54 am 
Newbie

Joined: Thu Jul 31, 2008 7:04 am
Posts: 4
I have two table of name User and Customer. Both are having m2m relationship with each other, where User is the primary owner of the relationship.

My problem is in one scenario, I do not need to associate any customer to User. So, here whenever I have tried to update user table, I need to associate with a customer.

My question is: Is there any way in Hibernate, where we can make the association as optional.. (May be defining any criteria or passing out some parameters).

Waiting for the answer quickly...


Top
 Profile  
 
 Post subject: Re: Problem with Many to Many mapping
PostPosted: Mon Aug 04, 2008 7:41 am 
Newbie

Joined: Thu Jul 31, 2008 7:04 am
Posts: 4
Can anyone please help me out?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 8:11 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I am not sure that I understand your problem correctly. In a many-to-many relation the entities in both ends can usually exist without the other. Eg. you can always have a User without any Customer(s) and a Customer without any User(s). In any case, the chances for getting help increases if you provide examples from your mapping files, code, stack traces, etc.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 9:15 am 
Newbie

Joined: Thu Jul 31, 2008 7:04 am
Posts: 4
I have used hibernate annotation to create the model classes. So, according to the requirement I have created 2 different models called User and Customer. And I have declared many to many relationship between them. where User is the Owner. That means, I have declared a Customer set variable in the User model and vice versa. Now, this relationship creates a third table called User_Customer table to maintain the relation. So, we can't access the intermediate table. Now, When I am updating User, it's automatically required one entry in user_customer table and for that I need to create a customer and associate with user.

Now, there is one situation when a particular user should not be assigned to any customer. Which is creating the problem. So, do we have any other way to create a user without assigning to any customer by using Hibernate relationship and annotation?

Please guide me...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 9:20 am 
Newbie

Joined: Sat Aug 02, 2008 7:53 am
Posts: 9
Location: Mumbai, India
according to my knowledge many to many can be done by a join table between two table.
For Example
Table1
id - Primary
name

Table2
id - Primary
name

Table3
tab1_id - primary
tab2_id - primary

lets assume table1 have 2 records. Say
id name
1 XYZ
2 ABC

and table2 have 2 record, Say
id name
1 PQR
2 MNO

now ur mapping table i.e table3 will have the mapping of both tables
like this
tab1_id tab2_id
1 1
1 2
2 1

And say u have many to many mapping like this
Table1.hbm.xml
--------------------
<hibernate-mapping>
<class name="com.infotech.Table1" table="Table1" lazy="false">
<id name="id" column="id" unsaved-value="null">
<generator class="identity">
</generator>
</id>
<property name="name" column="Name"/>

<set name="table2" table="Table3" lazy="false" cascade="save-update">
<key column="tab1_id" />
<many-to-many class="com.infotech.Table2" column="tab2_id" outer-join="auto"/>
</set>
</class>
</hibernate-mapping>

and for table2 u have
Table2.hbm.xml
------------------
<hibernate-mapping>
<class name="com.infotech.Table2" table="Table2" lazy="false">
<id name="id" column="id" unsaved-value="null">
<generator class="identity">
</generator>
</id>
<property name="name" column="Name"/>
</class>
</hibernate-mapping>

Now When u fetch the object of table 2 thru HQL
getHibernateTemplate().find("from Table1");
u'll get the list of object of table 1 which contains two object of table1.
in each object u'll have a Hashset/TreeSet of table2.
in 1st object of table 1 the Hashset / TreeSet of table2 contains two objects of table2, whereas in the second object of table1 Hashset / TreeSet of table2 contains only one object of table2.
so moral of the story is is there is a mapping of table 1 with table 2 exists in table 3 then only u can have table 2 collection in table1 object, else that collection/Set will be empty.

I hope u can get the idea how many to many mapping work when values r selected. :)

_________________
Sushant


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 9:21 am 
Newbie

Joined: Sat Aug 02, 2008 7:53 am
Posts: 9
Location: Mumbai, India
according to my knowledge many to many can be done by a join table between two table.
For Example
Table1
id - Primary
name

Table2
id - Primary
name

Table3
tab1_id - primary
tab2_id - primary

lets assume table1 have 2 records. Say
id name
1 XYZ
2 ABC

and table2 have 2 record, Say
id name
1 PQR
2 MNO

now ur mapping table i.e table3 will have the mapping of both tables
like this
tab1_id tab2_id
1 1
1 2
2 1

And say u have many to many mapping like this
Table1.hbm.xml
--------------------
<hibernate-mapping>
<class name="com.infotech.Table1" table="Table1" lazy="false">
<id name="id" column="id" unsaved-value="null">
<generator class="identity">
</generator>
</id>
<property name="name" column="Name"/>

<set name="table2" table="Table3" lazy="false" cascade="save-update">
<key column="tab1_id" />
<many-to-many class="com.infotech.Table2" column="tab2_id" outer-join="auto"/>
</set>
</class>
</hibernate-mapping>

and for table2 u have
Table2.hbm.xml
------------------
<hibernate-mapping>
<class name="com.infotech.Table2" table="Table2" lazy="false">
<id name="id" column="id" unsaved-value="null">
<generator class="identity">
</generator>
</id>
<property name="name" column="Name"/>
</class>
</hibernate-mapping>

Now When u fetch the object of table 2 thru HQL
getHibernateTemplate().find("from Table1");
u'll get the list of object of table 1 which contains two object of table1.
in each object u'll have a Hashset/TreeSet of table2.
in 1st object of table 1 the Hashset / TreeSet of table2 contains two objects of table2, whereas in the second object of table1 Hashset / TreeSet of table2 contains only one object of table2.
so moral of the story is is there is a mapping of table 1 with table 2 exists in table 3 then only u can have table 2 collection in table1 object, else that collection/Set will be empty.

I hope u can get the idea how many to many mapping work when values r selected. :)

_________________
Sushant


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 9:39 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
arin wrote:
.... Now, this relationship creates a third table called User_Customer table to maintain the relation. So, we can't access the intermediate table. Now, When I am updating User, it's automatically required one entry in user_customer table ....


From your description this seems like a common pattern that has been used a lot of times by other developers including myself (see also Hibernate documentation http://www.hibernate.org/hib_docs/v3/re ... irectional).

What I don't understand is why an entry is required in the User_Customer table in your case. This is certainly not normal, and I can't think of any way to create a mapping/annotations that can cause this behavior. What happens when you try to save a User that has no Customer? Do you get any error messages?


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