views:

569

answers:

1

Hello! I'm facing some of the problem some of you already talked about and sorry to bring this out again.

It's just like I still don't get certain aspect of hibernate. I'm using maven2, hibernate 3.2.5 ga, spring 2.6.5 SEC01, hsqldb 1.8.0.10, netbeans 6.7.1.

I'm bulding a user and contat management and I've managed to get a working "many-to-many" relationship between "contact" and "group pojo" in my environment with either hsql or mysql 5.0.51 but not on a local test server .Because of that i'ld like t try the double one-to-many association so there are few things I would like to ask about that mapping with the introduction of an intermediary model.

Now here are my mappings (I've posted just the necessary to avoid long post )

//UserAccount POJO

@OneToMany(targetEntity=PhoneImpl.class, cascade= {CascadeType.ALL})
@org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Phone> phones = new HashSet<Phone>();

@OneToMany(targetEntity=ContactImpl.class, cascade={CascadeType.ALL}, mappedBy="userAccount")
@org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Contact> contacts = new HashSet<Contact>();

@OneToMany(targetEntity=GroupImpl.class, cascade={CascadeType.ALL}, mappedBy="userAccount")
@org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Group> groups = new HashSet<Group>();


//Contact POJO

@ManyToOne(targetEntity=UserAccountImpl.class)
@JoinColumn(name="USER_ACCOUNT_ID",nullable=false)
private UserAccount userAccount;

@OneToMany(targetEntity=PhoneImpl.class, cascade={CascadeType.ALL})
private Set<Phone> phones = new HashSet<Phone>();

@ManyToMany(targetEntity=GroupImpl.class, mappedBy="contacts")
private Set<Group> groups=new HashSet<Group>();


//Group  POJO
@ManyToOne(targetEntity=UserAccountImpl.class)
@JoinColumn(name="USER_ACCOUNT_ID",nullable=false)
private UserAccount userAccount;

@ManyToMany(targetEntity=ContactImpl.class,cascade={CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(name="GROUP_CONTACT_MAP", joinColumns={@JoinColumn(name="GROUP_ID")},
inverseJoinColumns={@JoinColumn(name="CONTACT_ID")})
private Set<Contact> contacts = new HashSet<Contact>();

SO this works fine on my machine with hsqldb and my local mysql however it's not the same for the local testing server using the same mysql 5.0.SO here are my questions:

1. on the local testing server there is a self referencing foreign key to Contact as well as for group.I've read about circular referencing but can't really tell if it's the case here.Is there in the mapping anything that could generate this kind of self referencing foreign key.Or i failed to avoid the circular referencing stuffs?

2. I'm using Manning java persistence with hibernate which is a great book from where I started practicing the usage of hibernate.in that book the is a double one-to-many association let's say in my case between contact and group with an intermediary model. I just don't know how to use it in practice because I'll like to switch to that approach.

2.1 So my main worries are how to get all the contacts that are in a group and all the groups in which a contact is? I just can't see it clearly.

2.2 if going by that approach how will the contact and group referencing to each other will be? will the ignore each other? maybe those are basic stuffs for experience java coders like you so forgive my ignorance, i'll be very pleased if you could shed some light on that.

Thanks for reading!

+1  A: 

a few ideas:

0: are you sure you've changed the hibernate dialect between using hsqldb and mysql?

1: i think you could expect an exception if you had an unresolvable / circular reference

2.**: you would create a new class called ContactToGroup, it would have a contact field and a group field, each of which is the one end of a many-to-one. Both Contact and Group classes would have a Set of contactToGroup. You could query it like:

from Contact c 
left join fetch c.contactToGroupSet joinTable 
left join fetch joinTable.Group

This would give you all the Contacts with their Sets of Group attached via ContactToGroup objects.

creating a Contact, ContactToGroup and Group object together would allow you to write to these tables too.

Good luck!

thanks man. for the question **0** i have separate properties files for mysql, derby and hsql like mysql.hibernate.properties and mysql.jdbc.properties. for the question **1** it surely breaks the tests and i could see the foreign key in contact table referencing contact(contact_id). for you suggestion i'll give a try and see if i can manage it.thanks
black sensei