具有@EmbeddedId的JPA复合键 [英] JPA Compound key with @EmbeddedId

查看:112
本文介绍了具有@EmbeddedId的JPA复合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a legacy database, I have three tables: Users, Workgroups, and UsersWorkgroup. UsersWorkgroup stores what role a user has in a workgroup. Here are the relevant code snippets:

@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;

当然,问题是,它不起作用. 目前,我收到此异常:

And of course, problem is, it doesn't work. Currently I get this exception:

异常描述:不兼容 之间已经遇到映射 [classentity.Workgroup]和[class 实体.UsersWorkgroup].这通常 发生在 映射与 它的反向指针的基数.

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,我会得到:

Which I don't understand since OneToMany should match ManyToOne... Or is it a ManyToMany relationship? If I switch to @ManyToMany, I get this:

异常描述:目标 关系属性的实体 班级[班级]上的[工作组] com.ericsson.rsg.ejb.entity.UsersWorkgroup] 无法确定.不使用时 泛型,确保目标实体是 在关系映射上定义.

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表可以秘密地成为联接表吗?

I'm trying to understand compound keys (embedded), but all the examples I could find have only simple columns that are not foreign keys (but that's the whole point of a compound key, isn't it?). Can the UsersWorkgroup table secretly be a join table?

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

Should I declare the PK class as a strict POJO class? Or should I put the @JoinColumn annotations in the PK class? How do I refer to the columns within the compound key from another table? Should I initialize the PK object in the refering class constructor, or is it not necessary?

我感觉完全卡住了.

推荐答案

首先,我认为您的关系是多对多",因为一个用户可以分为多个组,而一个组可以有多个用户(或者我可以会这样).

First of all, I think your relation is a Many To Many, as a user can be in many groups, and a group can have many users (or I would assume so).

第二,据我所知,您必须将id_workgroup和user_name都引用为JoinColumns,因为它们是PK和单元的一部分,因此应同时引用两者.

Second, as far as I know you have to reference both id_workgroup and user_name as JoinColumns, because they are part of the PK and a unit, so both should be referenced.

此外,我看到嵌入式PK以及getter/setter方法中缺少"equals"和"hashCode"方法.我相信它们是强制性的.

Also, I see the "equals" and "hashCode" methods missing from your embedded PK, as well as the getters/setters. I believe they are mandatory.

这篇关于具有@EmbeddedId的JPA复合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆