-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: many-to-many (selfreferencing problem) vs double one-to-many
PostPosted: Tue Nov 17, 2009 8:17 pm 
Newbie

Joined: Mon Mar 30, 2009 2:47 pm
Posts: 15
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 building a user and contact management and I've managed to get a working "many-to-many" relationship between "contact" and "group pojo" in my environment with either hsqldb 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.

Code:
//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 question

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 it's the case here.Is there is 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'ld 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.1 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, ill be very pleased if you could shed some light on that.

Thanks for reading


Last edited by highjo on Wed Nov 18, 2009 6:17 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: many-to-many (selfreferencing problem) vs double one-to-many
PostPosted: Wed Nov 18, 2009 5:08 am 
Newbie

Joined: Mon Mar 30, 2009 2:47 pm
Posts: 15
edited the code.i think it's more clearer


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.