0

I'm starting my first project with Hibernate 4.2.21 and first with JPA 2.0, I want to create a relationship OneToMany Unidirectional. I saw a lot examples in version of Hibernate 3 but not much in 4.2.21 This example works perfectly but I don't know if is a good practice, I want to know the Opinion from another members about that?

Relationship One To Many:

-Parent Template:

  @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  @JoinColumn(name = "template_id") 
  private Set<Variable> variables = new LinkedHashSet<Variable>();

-Child: Variable

@Column(name = "template_id", nullable = false)
Integer templateId;
java.nazif
  • 713
  • 1
  • 9
  • 18
Miofino
  • 85
  • 2
  • 10
  • The child should generally not have an ID to its parent. If it's necessary for the child to know its parent, then make it a bidirectional association instead. – JB Nizet Nov 25 '15 at 07:42
  • In my case it's no necessary the child must know the father, and then to implement a good practice, it's only necessary delete the reference in the child? – Miofino Nov 25 '15 at 07:58
  • Yes. The templateId field is unnecessary. Also note that, although you initialize the set with `new LinkedHashSet()`, Hibernate won't use that set implementation, and won't persist the insertion order anywhere. So don't expect the variables in the set to be in any particular order in an entity loaded by Hibernate. – JB Nizet Nov 25 '15 at 07:59
  • ok, thanks!, and the question of the Collections.. this was another point, some examples use Set in another List, what is the best practice? – Miofino Nov 25 '15 at 08:07
  • A List is necessary when you need to preserve order (using an index column). In the other cases, a Set is a better option (unless you have a badly-behaving equals()/hasshCode() implementation: the easiest way not to have problems is to avoid overriding these two methods). – JB Nizet Nov 25 '15 at 08:11
  • ok, then I will use Set, for me keep the Order it's not important – Miofino Nov 25 '15 at 08:16
  • @JB Nizet can you see my answer below – Miofino Nov 29 '15 at 16:18
  • You answered your own question by copy and pasting the Hibernate documentation. What do you want me to see? If you have another question, then ask it as a question. – JB Nizet Nov 29 '15 at 17:35
  • Your answer was use "@OneToMany " with "@JoinColumn", but according with the Hibernate Documentation this use It's not recommend, with unidirectional we must use the "@JoinTable", or change to bi-directional relationship. – Miofino Nov 29 '15 at 18:29
  • There is a big difference between "it's recommended", and "you must use". OneToMany with JoinColumn is correct and supported. That's what you had in your question, so I assumed that it's what you wanted. – JB Nizet Nov 29 '15 at 20:20
  • @JBNizet ok yes you are right thanks!! – Miofino Dec 02 '15 at 10:42

1 Answers1

0

According with this another post's.

Hibernate unidirectional one to many association - why is a join table better?

http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-mapping-association-collections

A unidirectional one to many using a foreign key column in the owned entity is not that common and not really recommended. We strongly advise you to use a join table for this kind of association (as explained in the next section). This kind of association is described through a @JoinColumn

@Entity
public class Customer implements Serializable {
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="CUST_ID")
    public Set<Ticket> getTickets() {
    ...
}

@Entity
public class Ticket implements Serializable {
    ... //no bidir
} 

Unidirectional with join table

A unidirectional one to many with join table is much preferred. This association is described through an @JoinTable.

@Entity
public class Trainer {
    @OneToMany
    @JoinTable(
            name="TrainedMonkeys",
            joinColumns = @JoinColumn( name="trainer_id"),
            inverseJoinColumns = @JoinColumn( name="monkey_id")
    )
    public Set<Monkey> getTrainedMonkeys() {
    ...
}

@Entity
public class Monkey {
    ... //no bidir
} 

Finally the only way it's implement the bidirectional method... yes or no?

Community
  • 1
  • 1
Miofino
  • 85
  • 2
  • 10