休眠组合键:外键的列数错误 [英] Hibernate Composite Key: foreign key has wrong number of columns

查看:219
本文介绍了休眠组合键:外键的列数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hibernate和JPA的新手,难以设置如下定义的组合键:

  @Entity 
@Table(name = Entity.TABLE)
@IdClass(EntityPK.class)
public class MyEntity extends Entity {

@CollectionOfElements
@JoinTable name =items,
joinColumns = @JoinColumn(name =items))
private List< String>项目;

@Id
私人类型类型;

@Id
私有等级;
//加上标准的构造函数/ getters / setters
}


public class EntityPK implements Serializable {

private Type type;

私人级别;

//加上标准的构造函数/ getters / setters
}

当我运行我的dbunit测试时,我得到以下错误:


引起:org.hibernate.AnnotationException:引用com的外键。 com.tnt.sech.domain.management.ReasonList中的tnt.sech.domain.management.ReasonList的列数错误。应该是2


如果您能看到我要出错的地方,请发帖!

类型和 Level (不知道它们是什么)作为组合键的字段或属性。从JPA规范:

lockquote

2.1.4主键和实体标识

...

复合主键的主键(或字段或属性
)应该是
以下类型之一:任何Java
原始类型;任何原始包装
类型; java.lang.String中;
java.util.Date ; java.sql.Date
即可。在
通用中,不应在主键中使用近似数值
类型(例如,浮点类型)

主键使用类型
以外的实体将不可移植。
如果使用生成的主键,
只有整型将是可移植的。
如果使用 java.util.Date 作为
主键字段或属性,
临时类型应该被指定为
DATE


它们究竟是什么(枚举?)? / b>

除此之外,映射组合键本身的方法是OK的(请参阅下面的相关问题)。

相关的问题




I am new to Hibernate and JPA and am having difficulty setting up a composite key as defined below:

@Entity 
@Table(name = Entity.TABLE) 
@IdClass(EntityPK.class) 
public class MyEntity extends Entity {

        @CollectionOfElements
        @JoinTable(name="items",
            joinColumns = @JoinColumn(name="items"))
        private List<String> items;

        @Id
        private Type type;

        @Id
        private Level level;
   // plus standard constructors/getters/setters
}


public class EntityPK implements Serializable {

    private Type type;

    private Level level;

   // plus standard constructors/getters/setters
}

When I run my dbunit tests I get the following error:

Caused by: org.hibernate.AnnotationException: A Foreign key refering com.tnt.sech.domain.management.ReasonList from com.tnt.sech.domain.management.ReasonList has the wrong number of column. should be 2

Please post if you can see where I'm going wrong!

解决方案

In my opinion, the problem is that you can't use complex types such a Type and Level (not sure what they are) as field or property of your composite key. From the JPA specification:

2.1.4 Primary Keys and Entity Identity

...

The primary key (or field or property of a composite primary key) should be one of the following types: any Java primitive type; any primitive wrapper type; java.lang.String; java.util.Date; java.sql.Date. In general, however, approximate numeric types (e.g., floating point types) should never be used in primary keys. Entities whose primary keys use types other than these will not be portable. If generated primary keys are used, only integral types will be portable. If java.util.Date is used as a primary key field or property, the temporal type should be specified as DATE.

What are they exactly (enumerations?)?

Apart from that, the approach to map a composite key itself is OK (see related question below).

Related questions

这篇关于休眠组合键:外键的列数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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