java - 带 @EmbeddedId 的 JPA 复合键

标签 java jpa jpa-2.0 compound-key

在旧数据库中,我有三个表:Users、Workgroups 和 UsersWorkgroup。 UsersWorkgroup 存储用户在工作组中的角色。 以下是相关代码片段:

@Entity
@Table(name = "users_workgroup")
public class UsersWorkgroup implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected UsersWorkgroupPK usersWorkgroupPK;

    @JoinColumn(name = "idworkgroup", referencedColumnName = "idworkgroup")
    @ManyToOne(optional = false)
    private Workgroup workgroup;
    @JoinColumn(name = "user_name", referencedColumnName = "user_name")
    @ManyToOne(optional = false)
    private Users users;

    @Column(name = "role")
    private Integer role;


@Embeddable
public class UsersWorkgroupPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "idworkgroup", insertable=false, updatable=false)
    private int idworkgroup;
    @Basic(optional = false)
    @Column(name = "user_name", insertable=false, updatable=false)
    private String userName;


@Entity
@Table(name = "workgroup")
public class Workgroup implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idworkgroup")
    private Integer idworkgroup;
    @Column(name = "name")
    private String name;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idworkgroup")
    private Collection<UsersWorkgroup> usersWorkgroupCollection;

当然,问题是,它不起作用。 目前我得到这个异常:

Exception Description: An incompatible mapping has been encountered between [class entity.Workgroup] and [class entity.UsersWorkgroup]. This usually occurs when the cardinality of a mapping does not correspond with the cardinality of its backpointer.

我不明白,因为 OneToMany 应该匹配 ManyToOne...或者它是 ManyToMany 关系?如果我切换到@ManyToMany,我会得到这个:

Exception Description: The target entity of the relationship attribute [workgroup] on the class [class com.ericsson.rsg.ejb.entity.UsersWorkgroup] cannot be determined. When not using generics, ensure the target entity is defined on the relationship mapping.

我试图理解复合键(嵌入),但我能找到的所有示例都只有简单的列,而不是外键(但这就是复合键的全部要点,不是吗?)。 UsersWorkgroup 表可以 secret 地作为联接表吗?

我应该将 PK 类声明为严格的 POJO 类吗?或者我应该将@JoinColumn注释放在PK类中?如何从另一个表引用复合键中的列?我应该在引用类构造函数中初始化 PK 对象吗?还是没有必要?

我感觉完全被困住了。

最佳答案

首先,我认为您的关系是多对多,因为一个用户可以位于多个组中,并且一个组可以有多个用户(或者我会这样假设)。

其次,据我所知,您必须将 id_workgroup 和 user_name 都引用为 JoinColumns,因为它们是 PK 的一部分,也是一个单元,因此都应该引用。

此外,我发现您的嵌入式 PK 以及 getter/setter 中缺少“equals”和“hashCode”方法。我相信它们是强制性的。

关于java - 带 @EmbeddedId 的 JPA 复合键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3164207/

相关文章:

java - char buf[] = "test"是否等同于 String buf = new String ("test")?

java - 从 NumberFormatException 获取源字符串

java - 无法弄清楚如何解决 javax.persistence.PersistenceException

java - 对象无法使用JPA持久化到数据库中,因为它没有序列化

java - 复合键的 JPQL 异常

java - 正则表达式用新行数替换注释

Java + Groovy 联合项目的 JavaDoc

java - 使用 2 个互锁实体类进行 JPA 删除

java - JPA 2( hibernate )在保留父集合的同时插入子集合的重复项。到目前为止,解决方法是什么

validation - 在选择期间绕过 Bean 验证但不在插入/更新期间