Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
Forums Register Login
Mapping a Set in Hibernate.
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_Id (long)
- Item_Name (String)
- Item_Image (Set)

- Image_Id
- Filename

- 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,
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.

Naresh Waswani
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,
There you go. Exactly.

Naresh Waswani
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,
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.

Naresh Waswani
Ever since I found this suit I've felt strange new needs. And a tiny ad:
global solutions you can do at home or in your backyard

This thread has been viewed 1398 times.

All times above are in ranch (not your local) time.
The current ranch time is
May 21, 2019 10:32:33.