Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
shashank pratap
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
shashank pratap
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Manuel that was helpful.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic