2017-01-16 126 views
0

我從頭開始一個新的項目,我得到了這個問題,我無法解決。我有三個實體,他們彼此之間都有很多很多的關係。還有就是集羣:休眠mappedBy和ManyToMany

@Entity 
@Component 
@Table(name = "clusterEntity") 
public class Cluster { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "id", nullable = false, updatable = false) 
    private Long id; 

    @Column(name = "name") 
    private String name; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @ManyToMany 
     @JoinTable(name="cluster_user", 
      [email protected](name="cluster_id", referencedColumnName="id"), 
      [email protected](name="user_id", referencedColumnName="id")) 
    private List<User> users_cluster; 

    @ManyToMany 
     @JoinTable(name="cluster_sito", 
      [email protected](name="cluster_id", referencedColumnName="id"), 
      [email protected](name="sito_id", referencedColumnName="id")) 
    private List<Sito> sitos; 

    @Override 
    public String toString() { 
     return "Cluster{" + 
       "id=" + id + 
       ", name='" + name + 
       ", users='" + users_cluster.toString() + 
       '}'; 
    } 
} 

這是用戶:

@Entity 
@Component 
@Table(name = "userEntity") 
public class User { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "id", nullable = false, updatable = false) 
    private Long id; 

    @Column(name = "email", nullable = false, unique = true) 
    private String email; 

    @Column(name = "password_hash", nullable = false) 
    private String passwordHash; 

    @Column(name = "role", nullable = false) 
    @Enumerated(EnumType.STRING) 
    private Role role; 

    @Column(name = "G1", nullable = true) 
    private String G1; 

    @Column(name = "G2", nullable = true) 
    private String G2; 

    @Column(name = "G3", nullable = true) 
    private String G3; 

    @Column(name = "G4", nullable = true) 
    private String G4; 

    @Column(name = "G5", nullable = true) 
    private String G5; 

    @Column(name = "G6", nullable = true) 
    private String G6; 

    @Column (name = "access_token", nullable = true) 
    private String access_token; 

    @Column (name = "refresh_token", nullable = true) 
    private String refresh_token; 

    public Long getId() { 
     return id; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getPasswordHash() { 
     return passwordHash; 
    } 

    public void setPasswordHash(String passwordHash) { 
     this.passwordHash = passwordHash; 
    } 

    public Role getRole() { 
     return role; 
    } 

    public void setRole(Role role) { 
     this.role = role; 
    } 

    public String getG1() { 
     return G1; 
    } 

    public void setG1(String g1) { 
     G1 = g1; 
    } 

    public String getG2() { 
     return G2; 
    } 

    public void setG2(String g2) { 
     G2 = g2; 
    } 

    public String getG3() { 
     return G3; 
    } 

    public void setG3(String g3) { 
     G3 = g3; 
    } 

    public String getG4() { 
     return G4; 
    } 

    public void setG4(String g4) { 
     G4 = g4; 
    } 

    public String getG5() { 
     return G5; 
    } 

    public void setG5(String g5) { 
     G5 = g5; 
    } 

    public String getG6() { 
     return G6; 
    } 

    public void setG6(String g6) { 
     G6 = g6; 
    } 

    public String getAccess_token() { 
     return access_token; 
    } 

    public void setAccess_token(String access_token) { 
     this.access_token = access_token; 
    } 

    public String getRefresh_token() { 
     return refresh_token; 
    } 

    public void setRefresh_token(String refresh_token) { 
     this.refresh_token = refresh_token; 
    } 

    @ManyToMany(mappedBy="users_cluster") 
    private List<User> users_cluster; 

    @ManyToMany(mappedBy="users_sito") 
    private List<User> users_sito; 

    public User(){} 

    @Override 
    public String toString() { 
     return "User{" + 
       "id=" + id + 
       ", email='" + email.replaceFirst("@.*", "@***") + 
       ", passwordHash='" + passwordHash.substring(0, 10) + 
       ", role=" + role + 
       '}'; 
    } 
} 

這是鐸:

@Entity 
@Component 
@Table(name = "sitoEntity") 
public class Sito { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "id", nullable = false, updatable = false) 
    private Long id; 

    @Column(name = "name") 
    private String name; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @ManyToMany 
     @JoinTable(name="sito_user", 
      [email protected](name="sito_id", referencedColumnName="id"), 
      [email protected](name="user_id", referencedColumnName="id")) 
    private List<User> users_sito; 

    @Override 
    public String toString() { 
     return "Sito{" + 
       "id=" + id + 
       ", name='" + name+ 
       ", users='" + users_sito.toString()+ 
       '}'; 
    } 
} 

,當我嘗試使用Maven我得到的mappedBy以下錯誤編譯,就好像它寫在關係的兩邊,但實際上它只寫在一邊:

Caus編輯:org.hibernate.AnnotationException:非法使用mappedBy在關係兩邊:User.users_cluster

任何人都有任何想法我做錯了什麼?

+1

你已經告訴休眠,你從用戶到列表的關係被映射到類用戶(本身)。這會產生某種遞歸。我不認爲你可以在這種情況下使用mappedBy屬性。它通常在實體A和B之間存在關係時告訴哪一方是所有者。但是你有一個從實體用戶到自己的關係。用mappedBy定義一個所有者是沒有意義的(至少對我來說)。 –

+1

順便說一句:雖然我不太瞭解你的實體正在建模什麼,但你寫了3個實體具有彼此的ManyToMany關係,但在用戶中它與它自己只有2個ManyToMany關係。應該不是用戶列表和列表而不是兩個列表? –

回答

2

Clustor

至於我可以看到Cluster實體正確註釋除了命名約定的一些母牛。取而代之的

private List<User> users_cluster; 

我會建議使用

private List<User> users; 

列表中包含的用戶,以便它的名稱必須反映;好的命名是最好的文檔,(imo)。

用戶

這個實體似乎是正確的註釋,但引用是錯誤的,因爲它們是自參照。

public class User { 

    // ... 

    @ManyToMany(mappedBy="users") 
    private List<Cluster> clusters; 

    @ManyToMany(mappedBy="users") 
    private List<Sito> sitos; 

    // getters + setters 


} 

這裏也是我做了一個小的修改如下:所以,如果你想創建THRE實體之間的許多一對多的關係如下實體應修改

public class Sito { 

    // ... 

    @ManyToMany 
    @JoinTable(name="sito_user", 
      [email protected](name="sito_id", referencedColumnName="id"), 
      [email protected](name="user_id", referencedColumnName="id")) 
    private List<User> users; 

    @ManyToMany(mappedBy = "sitos") 
    private List<Cluster> clusters; 


    // getters + setters 
} 

現在你的三個實體應該如你所願相互關聯。