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

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

问题描述

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

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

  @Entity 
@IdClass(DeptId.class)
公共类部门
{
@Id
@列(name =number)
私人整数;

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

@Column(name =name)
私有字符串名称;

@OneToMany(mappedBy =dept)
私人收藏< Project>项目;

...
}

这里PK类:

  public class DeptId implements Serializable 
{
private Integer number;
私人字符串国家;

...
}

项目与部门是多对一的,这是一个部门可以有很多项目。 Project类本身使用引用Department组合键的组合键。重要提示:这只是关于@IdClass的实现而不是@EmbeddedId。

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

  @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)
私有字符串名称;
$ b @ManyToOne
@JoinColumns({
@JoinColumn(name =dept_number,referencedColumnName =number),
@JoinColumn(name =dept_country ,referencedColumnName =country)
})
private Department dept;


}

ProjectId是:

  public class ProjectId实现Serializable 
{
private String name;
私人部门;

...
}

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



我的问题是:

这是JPA 1.0的限制,使用@IdClass实现引用其他组合键的复合键通常不会工作,因为JPA实现根本无法知道如何映射这些字段?

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



谢谢

解决方案
  public class ProjectId implements Serializable 
{
private String name;
私人部门;

...
}

DeptId必须变平,如:

  public class ProjectId implements Serializable 
{
private Integer deptNumber;
private String deptCountry;
私人字符串名称;

...
}

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


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;

    ...
}

Here the PK class:

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

    ...
}

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.

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;

    ...
}

The ProjectId is:

public class ProjectId implements Serializable
{
    private String name;
    private 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.

My question is:

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?

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.

Thanks

解决方案

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 has to be flattened, like:

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

    ...
}

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.

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

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