• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2 Hibernate Qs: Collections, Annotation Mapping

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 1: When doing One-To-Many or Many-To-One Mappings, is it better to Sets, Collections, HashMaps, ArrayList...ect?

Question 2: When doing Many-To-Many using hibernate annotations is there any strategy for implimenting the beans? Examples for the private members and thier getters/setters/collections would be very helpful.

Thanks!
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilya Elkind:
Question 1: When doing One-To-Many or Many-To-One Mappings, is it better to Sets, Collections, HashMaps, ArrayList...ect?

Question 2: When doing Many-To-Many using hibernate annotations is there any strategy for implimenting the beans? Examples for the private members and thier getters/setters/collections would be very helpful.

Thanks!



1. Doesn't matter, so to speak. You object model would be what is best for your OO design, your database design should be what is best for the database model, them use Hibernate to map between the two.

2. Still kind of the same answer. Create what works best in your OO model, and what works best for your database model then use Hibernate to map between the two.

Hope that sort of helps.

Mark
 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So i figured out the difference between the Sets and Lists.. and i figured i would use the Sets for Entities seeings as i dont want repeated values and use Lists for Embeddable objects as it wont matter if I have repeats.

Now i'm still new to ManyToMany mapping relations so i'm not quite sure how to script them.
I'm trying to make an invitational relation between an event and a person. So a person can be invited to many events and the events can have many people invited, ie ManyToMany.

This is what i have and its not working.. seems like my mapping id is wrong and i dont know how to fix it.

EventEnt class


UserEnt class


Any ideas?
 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have another dillema.
I want to add a set of objects inside the object.

So for every user, there should be a set of "friends"

This is what i would normally do if i was working with collections.

Now this would normally work, if this was a 1 to Many mapping across another class, but this doesnt work becuase my setFriends method has to take a set of UserEnts.

Is there a way to set this collection set or should i not even bother with the add/remove methods?
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilya Elkind:
I have another dillema.
I want to add a set of objects inside the object.

So for every user, there should be a set of "friends"

This is what i would normally do if i was working with collections.

Now this would normally work, if this was a 1 to Many mapping across another class, but this doesnt work becuase my setFriends method has to take a set of UserEnts.

Is there a way to set this collection set or should i not even bother with the add/remove methods?



Actually, you want what you have.

The one thing I am trying to teach in my posts, is that keep doing OO as you do OO, forget about the database. And when working in the database forget about the OO model. Then after you have the best design in the particular paradigm, then worry about mapping them with Hibernate.

Hibernate has solutions for all mapping needs, so there is always a solution based on what OO and database model you chose.

Mark
 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, but can you give me a workaround to my addFriends and removeFriends methods?

Also I'm still lost on the Many-To-Many mappings above.

Thanks
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilya Elkind:
Ok, but can you give me a workaround to my addFriends and removeFriends methods?



What do you mean workaround? It sounded like you have it correct. I would just say that in your addFriend method that you check to see if the Collection of friends is not null. If it is, then you need to create one.

But you are right, the setFriends() will take a collection.

You mappings/annotations can be on the collection instance variable, or on the getter/setter.

Originally posted by Ilya Elkind:

Also I'm still lost on the Many-To-Many mappings above.

Thanks



Here is the Hibernate Docs on Many To Many
http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#entity-mapping-association-collections

Look for section
2.2.5.3.3.

Good Luck

Mark
 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Mark,
Thanks for your help but after continuing your train of thought and just using that function and current mapping strategy i get a "Duplicate entry in column 2" error in inserting friends.

mysql> describe userent_userent;
+------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------+------+-----+---------+-------+
| UserEnt_id | bigint(20) | NO | PRI | | |
| friends_id | bigint(20) | NO | PRI | | |
+------------+------------+------+-----+---------+-------+

Seems like both my columns got created as primary keys.. and as we know you must have a unique primary key. Is there a way to state which keys should be primary using annotations.

Here is my annotations code once again.


My code for the add is the same except i just commented out the final line which was giving me the hard time anyway.
 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've fixed my M-M problem.. here is the code for others to look at. I also had problems with the defualt lazy fetching, so i had to force it to eager.

UserEnt


EventEnt


still working on that other one though!
reply
    Bookmark Topic Watch Topic
  • New Topic