JPA 1.0 使用带有 *nested* 复合主键的 @IdClass 的限制? [英] Limitation of JPA 1.0 using @IdClass with *nested* composite primary keys?

查看:31
本文介绍了JPA 1.0 使用带有 *nested* 复合主键的 @IdClass 的限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下示例(部门 - 项目):

Given the following example (departments - projects):

一个部门具有以下属性(复合主键):

A department has the following properties (composite primary key):

@Entity
@IdClass(DeptId.class)
public class Department
{
    @Id
    @Column(name="number")
    private Integer number;

    @Id
    @Column(name="country")
    private String country;

    @Column(name="name")
    private String name;

    @OneToMany(mappedBy="dept")
    private Collection<Project> projects;

    ...
}

这里是PK课:

public class DeptId implements Serializable
{
    private Integer number;
    private String country;

    ...
}

项目和部门之间是多对一的关系,即一个部门可以有多个项目.Project 类本身使用一个引用 Department 的复合键的复合键.重要提示:这只是关于@IdClass 的实现,而不是 @EmbeddedId.

The relationship between projects and departments is many-to-one, that is a deptartment can have many projects. The Project class is itself using a composite key referencing Department's composite key. Important note: it's only about the implementation with @IdClass not @EmbeddedId.

那么(有问题的)JPA 1.0 @IdClass 实现就必须看起来像这样(冗余的 deptNum 和 deptCtry 属性):-> 它只是一个部门内的唯一名称

Then the (problematic) JPA 1.0 @IdClass implementation would have to look something like that (redundant deptNum and deptCtry properties): -> it's just a unique name within a department

@Entity 
@IdClass(ProjectId.class)
public class Project
{
    @Id
    @Column(name="dept_number")
    private Integer deptNumber;

    @Id
    @Column(name="dept_country")
    private String deptCountry;

    @Id
    @Column(name="name")
    private String name;

    @ManyToOne 
    @JoinColumns({
       @JoinColumn(name="dept_number", referencedColumnName="number"),
       @JoinColumn(name="dept_country", referencedColumnName="country")
    })    
    private Department dept;

    ...
}

项目 ID 是:

public class ProjectId implements Serializable
{
    private String name;
    private DeptId dept;

    ...
}

这样做的问题是 Hibernate 和 EclipseLink 都不知道如何将 Project 中的两个冗余属性 deptNum 和 deptCtry 映射到 DeptId 中的 dept 属性(或其中的属性).-> 映射异常等

The problem with this is that neither Hibernate nor EclipseLink know how to map the two redundant properties deptNum and deptCtry in Project to the dept property in DeptId (or the properies within it). -> MappingException etc.

我的问题是:

这是 JPA 1.0 的一个限制吗,具有复合键的表引用其他具有 @IdClass 实现的复合键通常不起作用,因为 JPA 实现根本不知道如何映射这些字段?

Is this a limitation of JPA 1.0, that tables with composite keys referencing other composite keys with @IdClass implementations generally WON'T work, because the JPA implementation simply can't know how to map these fields?

作为一种解决方法,您必须对这些类使用@EmbeddedId 或使用JPA 2.0 语法来注释@XToX 与@Id 的关联.我只是想确保我对此的看法是正确的.

As a workaround, you'd have to use @EmbeddedId for these classes or use JPA 2.0 syntax to annotate the @XToX associations with @Id. I just want to make sure my view on this is right.

谢谢

推荐答案

所发布代码的问题是,JPA 1.0 确实不允许复合主键类的嵌套.此 ProjectId 无效:

The problem with the posted code is, that JPA 1.0 really doesn't allow nesting of composite primary key classes. This ProjectId is invalid:

public class ProjectId implements Serializable
{
    private String name;
    private DeptId dept;

    ...
}

DeptId 必须扁平化,例如:

DeptId has to be flattened, like:

public class ProjectId implements Serializable
{
    private Integer deptNumber;
    private String deptCountry;
    private String name;

    ...
}

我刚刚得到一个 EclipseLink 版本,但 Hibernate 有问题.我想知道如何告诉 Hibernate 假设是 JPA 1.0.

I just got an EclipseLink version to go, but Hibernate has problems with that. I wonder how to tell Hibernate that JPA 1.0 is assumed.

这篇关于JPA 1.0 使用带有 *nested* 复合主键的 @IdClass 的限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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