Matching class classes in sleep mode

I am a student and I am new to the Hibernate structure, I have an association class and I want to know how to map it Situation:

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

                                 |
                                 |
                                 |
                                 |
                                 |
                          Association class
                           Date_affectation
                           Date_expiration

Which association rule should be used?

+4
source share
2 answers

You can model the association as a separate entity Association. Temporary data can be modeled as regular attributes Date(like @Temporal(TemporalType.TIMESTAMP)).

You can model PK associations as part of foreign keys Entity_1and Entity_2- this will make the association a dependent object. Or you can assign it your own identifier and connect to Entity_1and Entity_2through the ManyToOne relationship.

. @Vlad, , , .

@Column(updatable=false) Date, , @Temporal(TemporalType.TIMESTAMP), JPA , , .

, mappedBy () JoinColumn ().

@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 Entity2 , . Set, List, , , - .

public class Entity1 {

  @OneToMany(mappedBy="entity1")
  private List<Association> associations = new ArrayList<Association>();
  ...
}
+1

, " ", "".

@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>();
}

.

0

Source: https://habr.com/ru/post/1541401/


All Articles