4

I'm a student and I'm new in Hibernate framework, I have a association class and I want to know how to map it the Situation :

         Entity_1[0..\*]-------------------------[0..\*]Entity_2

                                 |
                                 |
                                 |
                                 |
                                 |
                          Association class
                           Date_affectation
                           Date_expiration

What the association rule should I use ?

slavoo
  • 5,798
  • 64
  • 37
  • 39
K.Ariche
  • 1,188
  • 3
  • 14
  • 29

2 Answers2

1

You can model the association as a separate Association entity. The temporal data can be modelled as regular Date attributes (as @Temporal(TemporalType.TIMESTAMP)).

You can model the PK of an association as the composition of the foreign keys of Entity_1 and Entity_2 - this would make the association a dependent entity. Or you can assign it an own id and connect to Entity_1 and Entity_2 via ManyToOne relationships.

EDIT: The bulk of my example implementation is shamelessly stolen from @Vlad's answer, since the main difference is that an entity has an id and is queryable while an embeddable is not.

I have removed @Column(updatable=false) from the Date columns, because I am not sure that this is required and added @Temporal(TemporalType.TIMESTAMP) because it is required by the JPA spec and thus increases portability.

Since this is a bidirectional mapping, I use mappedBy on the inverse side (the entities) instead of JoinColumn on the owning side (the association).

@Entity
public class Association {

  @Id
  @GeneratedValue(...)
  private Long id;

  @Column
  @Temporal(TemporalType.TIMESTAMP)
  private Date affectation;

  @Column
  @Temporal(TemporalType.TIMESTAMP)
  private Date expiration;

  @ManyToOne(fetch=FetchType.LAZY)
  private Entity1 entity1;

  @ManyToOne(fetch=FetchType.LAZY)
  private Entity2 entity2;
}

Entity1 and Entity2 look the same, just replace the digit. I am not sure about the added value of using the Set interface, so I replaced it with List, but perhaps I am missing something.

public class Entity1 {

  @OneToMany(mappedBy="entity1")
  private List<Association> associations = new ArrayList<Association>();
  ...
}
Community
  • 1
  • 1
kostja
  • 60,521
  • 48
  • 179
  • 224
  • I understand that I will have a table with two keys and date(expiration and affectation) for Association and a table for each Entity,Could you clarify who to implement you answer – K.Ariche May 21 '14 at 14:38
  • What I understands is that you had transformed the class association to two associations each one with Entity[1]-----[*]Association_class isn't it ? – K.Ariche May 21 '14 at 16:12
  • Exactly. You could also map the relationship table as a map in both entities, but I find the entity/embeddable approach more clean and simple. – kostja May 21 '14 at 17:27
0

Unless you ever want to fetch the Association by its id, you should choose to have an Embeddable Association which will define those two Date fields.

@Embeddable
public class Association {

    @Column(updatable=false)
    private Date affectation;

    @Column(updatable=false)
    private Date expiration;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="ENTITY_1_ID")
    private Entity1 entity1;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="ENTITY_2_ID")
    private Entity2 entity2;

}

public class Entity1 {

    @CollectionOfElements
    @JoinTable(
        table=@Table(name="ASSOCIATION"),
        joinColumns=@JoinColumn(name="ENTITY_1_ID")
    )
    private Set<Association> associations = new HashSet<Association>();
}

public class Entity2 {

    @CollectionOfElements
    @JoinTable(
        table=@Table(name="ASSOCIATION"),
        joinColumns=@JoinColumn(name="ENTITY_2_ID")
    )
    private Set<Association> associations = new HashSet<Association>();
}

This was previously explained here.

Community
  • 1
  • 1
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • is it necessary to add navigability in the Entity1 and Entity2 ? – K.Ariche May 21 '14 at 12:07
  • Because the Association is embeddable its life-cycle is bound to an Entity. You can't query embeddable objects, just Entities, so that's why you need the "associations" relationship in Entity1 and Entity2. If you need to query for Association or you'd like to have it independent of Entity1 or Entity2 then you should consider @kostja's example – Vlad Mihalcea May 21 '14 at 12:12