Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Object Relational Mapping and the fly likes Mapping a Set in Hibernate. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Mapping a Set in Hibernate. " Watch "Mapping a Set in Hibernate. " New topic
Author

Mapping a Set in Hibernate.

Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 257
Hi Friends,
I am a novice Hibernate developer and starting to learn how to map collections in Hibernate. I am consulting the book "Java Persistence with Hibernate" written by Christian Bauer and Gavin King. I have reached to chapter 6 - "Mapping Collections and Entity Associations".
In that chapter I am reading the section "Mapping a set". Here the example is given of Item and Images. If we term it like this an Item has a set of Images. We should have 3 tables as :

Item
- Item_Id (long)
- Item_Name (String)
- Item_Image (Set)


Image
- Image_Id
- Filename

Item_Image
- Item_Id
- Image_Id



In the book only 2 tables are given namely - Item and Item_Image.

Also when the set mapping is described as follows :




they say <element> tag declares this collection as a collection of value type instances. By this do we mean that Image here is a value type ? Do we take that Item is an entity and Image is a value type ?.

Since we have a one-to-many association between Item and Image - One Item can have many images shouldn't we have mapping like this instead of <element> tag ?



Please do help me in clearing my concepts.

Thanks and Regards,
Sid.
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
Well it depends on the requirement.

You are trying to share an image with multiple items. So for you that is more of an association using reference concept and in such cases one can use one-to-many or many-to-many stuff.

But what if I don't want to share an image with any other entity and at the same time also want to make sure that image exists only if the associated item exists. That means....if I destroy item, associated image should also be destroyed.

Technically, such mapping can be done using both value type OR reference type but because image exits only because of item, it would make more sense to have it as value type.

Regards,
Naresh Waswani


Naresh Waswani
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 257
Hi Waswani,
Thanks a lot for your prompt reply. I have something to clarify.

1) If I am considering the Image as a value type then the mapping would be using the <element> tag.

2) If I am considering the Image as a separate entity then the mapping would be done using <one-to-many> OR <many-to-many> tags.

Is my understanding correct. ?

Thanks and Regards,
Sid
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
There you go. Exactly.

Regards,
Naresh Waswani
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 257
Hi Waswani,
I have a confusion regarding one-to-many and many-to-many mapping tags.

The example is this site http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-many-1.html gives a one to many example. While if you see the Student.hbm.xml mapping xml file in this site you would see that inside the <set> tag they have used <many-to-many> tag not <one-to-many> tag. Why so ?

Why is this ? I have seen several examples is various sites where such things are given. I want to clarify my concept where <one-to-many> tag is used and where <many-to-many> tag is used Or <many-to-one> tag is used.




Thanks and Regards,
Sid.
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
Hi Sid,

If there is a one-to-many association, it can be implemented using one-to-many concept OR using many-to-many with unique constraint on one column in the join table.

Well it depends, which approach one would go for. For instance, if the associated entities already exists in the database and the association would be done at a later point of time, one can go for many-to-many approach.

For instance....lets say we have Project and Employee entities with Project having one-to-many relationship with Employee and my requirement says that I should be able to create Project object and Employee object independently and later on associate them.

In this case, one can go for many-to-many approach with unique constraint on join table.

If I want to follow the one-to-many approach, I can still implement that but than the project_id column (foreign key to employee table) would be empty initially and later on when the association would be done, this column would be updated.


Regards,
Naresh Waswani
 
wood burning stoves
 
subject: Mapping a Set in Hibernate.
 
Similar Threads
Collection of components
CollectionsInHibernate
Foreign Key Constraint Fails
Item - Bid Unidirectional PROBLEM !!!
Question on Sets of Objects