Reputation: 1197
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 ?
Upvotes: 3
Views: 3035
Reputation: 61538
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>();
...
}
Upvotes: 1
Reputation: 153730
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.
Upvotes: 0