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