JPA - 实体@OneToOne映射中的重复列

时间:2016-04-09 07:08:17

标签: jpa

我有两个简单的表:Users和User_Roles。 在两个表中我有一个字段email,我想从UserEntity加载User_Roles,查询应该是:

SELECT *
  FROM Users u, User_Roles ur
 WHERE u.email = ur.email;

当我尝试“链接”电子邮件字段时,我收到错误“实体映射中的重复列:UserEntity列:电子邮件”。

我尝试添加任何类型的组合(mappedBy,refColumn等),但错误甚至相同......

用户

@Entity
@Table(name = "users")
public class UserEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    private long id;
    private String email;
    private UserRolesEntity roles;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }

    @Basic
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    @OneToOne
    @JoinColumn(name = "email")
    public UserRolesEntity getRoles() {
        return roles;
    }
    public void setRoles(UserRolesEntity roles) {
        this.roles = roles;
    }
}

User_Roles

@Entity
@Table(name = "user_roles")
public class UserRolesEntity {

    private long id;
    private String email;
    private String role;

    @Id
    @Column(name = "user_role_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long getId() {
        return id;
    }

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

    @Basic
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    @Basic
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
}

1 个答案:

答案 0 :(得分:0)

如果要链接User_Roles和User,则应将User的主键保留在User_Roles中。因此,您最好像这样修改User_Roles表:

long user_role_id
long user
String role

然后你的User_Roles类将是:

@Entity
@Table(name = "user_roles")
public class UserRolesEntity {

private long id;
private User user;
private String role;

@Id
@Column(name = "user_role_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public long getId() {
    return id;
}

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

@OneToOne
public User getUser() {
    return user;
}
public void setUser(String user) {
    this.user = user;
}

@Basic
public String getRole() {
    return role;
}
public void setRole(String role) {
    this.role = role;
}
}

如果您想从user_role获取用户电子邮件,您可以这样做:userRole.getUser().getEmail()

然后User类将是:

@Entity
@Table(name = "users")
public class UserEntity implements Serializable {

private static final long serialVersionUID = 1L;

private long id;
private String email;
private UserRolesEntity roles;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}

@Basic
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}

@OneToOne(mappedBy="user")
public UserRolesEntity getRoles() {
    return roles;
}
public void setRoles(UserRolesEntity roles) {
    this.roles = roles;
}
}