zoukankan      html  css  js  c++  java
  • JPA实体关系映射

    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映射,会出现联合主键,即双主键。

  • 相关阅读:
    Acdream 1174 Sum 暴力
    Acdream 1114 Number theory 莫比乌斯反演
    Acdream 1007 快速幂,模乘法
    UVa 10023
    UVa 11027
    UVa 11029
    UVa 10820
    UVa 10791
    UVa 11121
    UVa 106
  • 原文地址:https://www.cnblogs.com/goodwin/p/1844673.html
Copyright © 2011-2022 走看看