为什么这个JPA 2.0映射在Eclipse / JBoss Tools中给我一个错误? [英] Why is this JPA 2.0 mapping giving me an error in Eclipse/JBoss Tools?

查看:103
本文介绍了为什么这个JPA 2.0映射在Eclipse / JBoss Tools中给我一个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:
有效的XHTML http:/ /www.kawoolutions.com/media/zips-countries-geoareas.png



JPA 2.0映射(可能只需考虑Zip和ZipId类,这是错误似乎来自):

  @Entity 
@Table(name =GeoAreas)
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name =discriminator,discriminatorType = DiscriminatorType.STRING)
public abstract class GeoArea implements可序列化
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =id)
protected Integer id;

@Column(name =name)
protected String name;

...
}

@Entity
@Table(name =Countries)
@DiscriminatorValue(value =country )
public class Country extends GeoArea
{
@Column(name =iso_code)
private String isoCode;

@Column(name =iso_nbr)
private String isoNbr;

@Column(name =dial_code)
private整数dialCode = null;

...
}

@Entity
@Table(name =Zips)
@IdClass(value = ZipId。 class)
public class Zip implements Serializable
{
@Id
@Column(name =code)
private String code;

@Id
@ManyToOne
@JoinColumn(name =country_code,referencedColumnName =iso_code)
private国家/地区country = null;

...
}

public class ZipId implements Serializable
{
private String country;

私人字符串代码;

...
}

相当简单的设计: / p>

一个国家是一个地理区域,并从根类继承ID。邮政编码在其国内是唯一的,所以它结合了ISO代码加上实际的邮政编码作为PK。因此,Zips引用Countries.iso_code,它具有一个替代的唯一的非空键(引用非主键键列!)。 Zip.country关联获取一个@Id注释,其变量名与其ID类ZipId中的变量名相同。



但是,我从Eclipse中获取此错误消息(也使用JBoss工具):



验证消息:
与ID类属性国家匹配的属性没有正确的类型java.lang.String


  1. 为什么在 JPA 2.0 语法中出错(请参阅Zip.country上的@Id注释)?我不认为是这样。所有类型的Zip.country和ZipId.country 不能对于JPA 2是相同的,因为@ManyToOne上的@Id注释,而PK是一个简单的整数,成为ID班级对应有人可以检查/确认吗?

  2. 这可能是一个Bug,可能是在JBoss Tools中? (哪个软件组件报告上述错误?当将3个表和实体类放入新的JavaSE项目时,确切的代码没有显示错误...)


解决方案

回答自己的问题...



我建模引用的方式,我用一个String因为FK指向国家表中的iso_code列,它是一个CHAR(2),所以基本上我的映射是正确的。但是,问题是JPA 2.0不允许任何东西,除了引用主键列。这是Eclipse Dali JPA验证器显示的。



从Keith / Schincariol p.283顶级的Pro JPA 2.0中,导出标识符的基本规则(规则#6):如果一个实体的id属性是一个关系,那么id类中的匹配属性的类型与关系中的目标实体的主键类型是一样的(无论是主键类型是一个简单的类型,id类或嵌入式id类)



个人附录:



我不同意JPA 2.0有这个限制。 JPA 1.0映射允许引用非PK列。请注意,使用JPA 1.0映射不是我正在寻找的。我更希望对JPA 2.0施加这种限制的原因感兴趣。 JPA 2.0绝对是有限的。


I have the following situation: Valid XHTML http://www.kawoolutions.com/media/zips-countries-geoareas.png

JPA 2.0 mappings (It might probably suffice to consider only the Zip and ZipId classes as this is where the error seems to come from):

@Entity
@Table(name = "GeoAreas")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
public abstract class GeoArea implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    protected Integer id;

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

    ...
}

@Entity
@Table(name = "Countries")
@DiscriminatorValue(value = "country")
public class Country extends GeoArea
{
    @Column(name = "iso_code")
    private String isoCode;

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

    @Column(name = "dial_code")
    private Integer dialCode = null;

    ...
}

@Entity
@Table(name = "Zips")
@IdClass(value = ZipId.class)
public class Zip implements Serializable
{
    @Id
    @Column(name = "code")
    private String code;

    @Id
    @ManyToOne
    @JoinColumn(name = "country_code", referencedColumnName = "iso_code")
    private Country country = null;

    ...
}

public class ZipId implements Serializable
{
    private String country;

    private String code;

    ...
}

Pretty simple design:

A country is a geo area and inherits the ID from the root class. A ZIP code is unique within its country so it combines an ISO code plus the actual ZIP code as PK. Thus Zips references Countries.iso_code, which has an alternative unique, not-null key on it (reference to non-primary key column!). The Zip.country association gets an @Id annotation and its variable name is the same as the one in its ID class ZipId.

However I get this error message from within Eclipse (also using JBoss Tools):

Validation Message: "The attribute matching the ID class attribute country does not have the correct type java.lang.String"

  1. Why is this wrong in JPA 2.0 syntax (see @Id annotation on Zip.country)? I don't think it is. After all the types of Zip.country and ZipId.country can't be the same for JPA 2 because of the @Id annotation on the @ManyToOne and the PK being a simple integer, which becomes the ID class counterpart. Can anyone check/confirm this please?
  2. Could this be a bug, probably in JBoss Tools? (Which software component is reporting the above bug? When putting the 3 tables and entity classes into a new JavaSE project there's no error shown with the exact code...)

解决方案

Answering own question...

The way I modeled the reference, I use a String because the FK points to the iso_code column in the Countries table which is a CHAR(2), so basically my mapping is right. However, the problem is that JPA 2.0 doesn't allow anything but references to primary key columns. This is what the Eclipse Dali JPA validator shows.

Taken from "Pro JPA 2.0" by Keith/Schincariol p.283 top, "Basic Rules for Derived Identifiers" (rule #6): "If an id attribute in an entity is a relationship, then the type of the matching attribute in the id class is of the same type as the primary key type of the target entity in the relationship (whether the primary key type is a simple type, an id class, or an embedded id class)."

Personal addendum:

I disagree with JPA 2.0 having this limitation. JPA 1.0 mappings allow references to non-PK columns. Note, that using JPA 1.0 mappings instead isn't what I'm looking for. I'd rather be interested in the reason why this restriction was imposed on JPA 2.0. The JPA 2.0 is definitely limiting.

这篇关于为什么这个JPA 2.0映射在Eclipse / JBoss Tools中给我一个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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