JPAエンティティ関係マッピング

2037 ワード

OneToOne
 
public classEmployee implements Serializable {



         ……



    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)



    @JoinColumn(name = "ADDR_ID")



  private Address address;



… …



}


 
 
ManyToOne
 
public classEmployee implements Serializable {



         ……



    @ManyToOne(fetch = FetchType.LAZY)



    @JoinTable(name = "EMP_JOB", joinColumns = @JoinColumn(name = "EMP_ID"), inverseJoinColumns = @JoinColumn(name = "TITLE_ID"))



    //    joinColumns         ,inverseJoinColumns                 



  private JobTitle jobTitle;





    @ManyToOne(fetch = FetchType.LAZY)



    @JoinColumn(name = "MANAGER_ID")



  private Employee manager;



… …



}


 
 
OneToMany
 
public classEmployee implements Serializable {



         ……



    @OneToMany(mappedBy = "manager") //manager      



    private List<Employee> managedEmployees = newArrayList<Employee>();





    @OneToMany(mappedBy = "owner", cascade = CascadeType.ALL, orphanRemoval = true)



  private List<PhoneNumber> phoneNumbers = newArrayList<PhoneNumber>();





    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)



    @JoinColumn(name="EMP_ID")



    private List<Degree> degrees = newArrayList<Degree>();



         ……



}


 
 
ManyToMany
public classEmployee implements Serializable {



         ……



    @ManyToMany



    //   



    @JoinTable(name = "PROJ_EMP", joinColumns = @JoinColumn(name = "EMP_ID"), inverseJoinColumns = @JoinColumn(name = "PROJ_ID"))



   private List<Project> projects = newArrayList<Project>();



… …



}


 
 
統合プライマリ・キー・マッピング
ManyToOne OneToMany ManyToManyマッピング、JoinTableマッピングを使用すると、結合プライマリ・キー、すなわちデュアル・プライマリ・キーが表示されます.