aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate Mapping for creating Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Mapping for creating "has a" relationship  one to many" Watch "Hibernate Mapping for creating "has a" relationship  one to many" New topic
Author

Hibernate Mapping for creating "has a" relationship one to many

shashank pratap
Greenhorn

Joined: Aug 11, 2009
Posts: 13
HI ALL,
i am experiencing problem in hibernate mapping as i want to develop a relationship between Item and User.
As the business logic is User is already registered in the system and he/she wants to upload his/her item, one by one, if he has more than one item.

So there are three classes,
(1) User(userid,.......,...........,.)
(2) Item(itemid,.....,...,.......);
(3) User_Item(userid,itemid)
all the attributes not worth mention.

MY PROBLEM IS I WANT INSERT INTO ITEM TABLE AND CREATE LINK IN USER_ITEM TABLE CORRESPONDING TO THE USER WHO UPLOADED ITEM (AS ONE USER MAY HAVE MANY ITEMS) BUT HERE ONE USER IS HAVING ONLY ONE ITEM.

BUT HIBERNATE IS RUNNING THIS QUERY BEFORE INSERTING IN USER_ITEM DELETING ALL THE ITEMS CORRESPONDING TO THAT USER.


Hibernate: insert into ITEM (.......................)
Hibernate: update USER set(.............)
Hibernate: delete from ITEM_USER where USERID=? <<--------------------------------------
Hibernate: insert into ITEM_USER (USERID, ITEMID) values (?, ?)


CODE.

User.hbm.xml



User.java
Set<Item> items = new HashSet<Item>(); other attributes and getter / setters.

Item.java has attribute and getter and setter.






Please help me
shashank.




Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

Please do not write in all caps. It is usually associated with yelling and reduces readability a lot.
For your question.
Is there a special reason that you made the entity item_user?
If I understand correctly you are trying to define a join table. This can be done more easily by hibernate itself via the mapping.
The wikibooks link oneToMany might help you.


Please correct my English.
shashank pratap
Greenhorn

Joined: Aug 11, 2009
Posts: 13

Thanks Manuel that was helpful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate Mapping for creating "has a" relationship one to many