Hibernate mappedBy和ManyToMany

时间:2017-01-16 17:10:54

标签: java hibernate maven jpa

我从头开始一个新项目,我遇到了这个我无法解决的问题。我有三个实体,他们彼此之间有很多关系。集群:

@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",
          joinColumns=@JoinColumn(name="cluster_id", referencedColumnName="id"),
          inverseJoinColumns=@JoinColumn(name="user_id", referencedColumnName="id"))
    private List<User> users_cluster;

    @ManyToMany
      @JoinTable(name="cluster_sito",
          joinColumns=@JoinColumn(name="cluster_id", referencedColumnName="id"),
          inverseJoinColumns=@JoinColumn(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 +
                '}';
    }
}

这是Sito:

@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",
          joinColumns=@JoinColumn(name="sito_id", referencedColumnName="id"),
          inverseJoinColumns=@JoinColumn(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的以下错误,就好像它是在关系的两边写的,但实际上它只写在一边:

引起:org.hibernate.AnnotationException:在关系的两端非法使用mappedBy:User.users_cluster

任何人都知道我做错了什么?

1 个答案:

答案 0 :(得分:2)

Clustor

据我所知,Cluster实体的注释正确,除了一些命名约定。而不是

private List<User> users_cluster;

我建议使用

private List<User> users;

该列表包含用户,因此应将其命名以反映该用户;良好的命名是最好的文档,(imo)。

用户

这个实体似乎被正确注释,但引用是错误的,因为它们是自引用的。因此,如果要在thre实体之间创建多对多关系,则应按如下方式修改实体:

public class User {

    // ...

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

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

    // getters + setters


}

SITO

我在这里做了一个小修改如下:

public class Sito {

     // ...

     @ManyToMany
     @JoinTable(name="sito_user",
          joinColumns=@JoinColumn(name="sito_id", referencedColumnName="id"),
          inverseJoinColumns=@JoinColumn(name="user_id", referencedColumnName="id"))
    private List<User> users;

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


    // getters + setters
}

现在,您的三个实体应该根据需要相互关联。