-->
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.  [ 4 posts ] 
Author Message
 Post subject: Collection or relation many-to-one
PostPosted: Wed Nov 10, 2004 2:16 pm 
Beginner
Beginner

Joined: Tue Sep 23, 2003 5:00 pm
Posts: 40
Hi everyone,

I don't really know when I should use a many-to-one relationship or a mapping with a Collection.
Let's assume one has two classes Parent and Child.

Could you explain to me, in what case(s) one should implement the solution 1 or the solution 2

Solution 1 (a Collection - as in the Hibernate Reference)
Code:
public class Parent {
    private long id;
    private Set children;
    public Set getChildren() {
        return children;
    }
    public void setChildren(Set children) {
        this.children = children;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
}///:-

public class Child {
    private long id;
    private String name;   
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}///:-


In this case, one would have a mapping with a Collection (as in the Hibernate reference)

Solution 2 (a many-to-one relationship)
Code:
public class Parent {
    private long id;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
}///:-
public class Child {
    private long id;
    private String name;
    private Parent parent;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Parent getParent() {
        return parent;
    }
    public void setParent(Parent parent) {
        this.parent = parent;
    }
}///:-


In this case, on would have a many-to-one relationship between Child and Parent.

I feel like the two solutions are correct. Am I wrong ?
If yes, can you explain me why ?
If no, when is it better to use the first or second solution.

Thanks in advance for your answers ... and sorry for this silly question ;)
sylvain_2020


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 10, 2004 9:32 pm 
Beginner
Beginner

Joined: Sun Oct 03, 2004 8:50 am
Posts: 32
Location: McLean, VA
Yes, either one works just fine. When you would use each depends on what you need to do. If you need to be able to get the Parent and from there get the children, you use the first options. If you get children from something and need to get thier parent, you use the second option.

There also isn't any reason you can't do both, it doesn't hurt anything. I, personally, usually do both because about the time I'm sure I'll only ever use one, I end up needing the other. Then I have to break my train of thought on what I'm currently working on and go add more stuff to the mapping.

_________________
- Chad


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 3:17 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Did you see chapter 16 of the reference docs?

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 10:49 am 
Beginner
Beginner

Joined: Tue Sep 23, 2003 5:00 pm
Posts: 40
Thanks clajoie for clarifying my thougths !

Ernst, re-read chapter 16 of the Hibernate Reference : everything is clearer now!

Thanks a lot guys
sylvain_2020


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.