【问题标题】:JPA @OneToMany join on part of a multipart keyJPA @OneToMany 加入多部分密钥的一部分
【发布时间】:2010-12-28 20:03:07
【问题描述】:

配置文件表与特权表具有一对多关联。权限表有一个多部分键,一个 profile_id 和一个 privilege_id。我只想在 profile_id 上从 Profile 表加入 Privilege 表并取回权限集合。

在我的个人资料课程中

@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "PROF_ID")
public List<ProfilePrivilegeEO> getProfilePrivileges()
{
    return m_profilePrivileges;
}

我的特权班有

private ProfilePrivilegeId m_profileId;

@EmbeddedId
public ProfilePrivilegeId getProfileId()
{
    return m_profileId;
}

ProfilePrivilegeId 在哪里

@Embeddable
public class ProfilePrivilegeId
    implements Serializable
{
    private Integer m_profileId;
    private Integer m_privNumber;

    @Column(name = "PROF_ID")
    public Integer getProfileId()
    {
        return m_profileId;
    }

    @Column(name = "PRIV_NUM")
    public Integer getPrivNumber()
    {
        return m_privNumber;
    }

    .....
}

但是,当我这样做时,静态编织器会说:

The @JoinColumns on the annotated element [method getProfilePrivileges] from the entity     
class [class com.acme.ProfileEO] is incomplete. When the source entity class uses a 
composite primary key, a @JoinColumn must be specified for each join column using the 
@JoinColumns. Both the name and the referencedColumnName elements must be specified in 
each such @JoinColumn.

但是,Profile 表不知道 privilege_ids...我不明白为什么 JPA 应该要求我指定特权表的两个键,这只是 jpa 做出的任意决定,没有正当理由。 . 我需要做什么才能让它工作? (我正在使用 EclipseLInk。)

【问题讨论】:

    标签: java database jpa join


    【解决方案1】:

    创建一个封装 id 的 PrivilegeId 类。将该类设为@Embedded 并使用@EmbeddedId 将其放入Privilege。

    在 PrivilegeId 类中,将 @OneToMany 放入 Profile 和权限 id。

    【讨论】:

    • 第一部分是我所做的。配置文件对特权是 OneToMany。不是相反。加入仅在 profile_id 上。
    • 什么是 ProfilePrivilegeEO?放置@JoinColumns 注释并在那里使用两个@JoinColumn。这不会对生成的模式产生负面影响,因为在 @OneToMany 中,键位于外部类中,即 Privilege(它实际上在哪里并且应该在哪里)。
    • ProfilePrivilegeEO 是 Privilege 表的实体。我没有两个 @JoinColumns 可以从 Profile 类中指定,因为它不知道权限。这就是问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2015-09-27
    • 2012-12-01
    • 2014-05-14
    • 2021-08-11
    • 1970-01-01
    相关资源
    最近更新 更多