This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mapping a Set in Hibernate.

 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There you go. Exactly.

Regards,
Naresh Waswani
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic