Java JPA:如何使用@ElementCollection 注释?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18406516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:19:57  来源:igfitidea点击:

JPA: how to use @ElementCollection annotation?

javasqlhibernatejpa

提问by gpezzini

I need your help to map the relation in Hibernate of two tables using the @ElementCollection annotation.

我需要您的帮助来使用 @ElementCollection 注释在 Hibernate 中映射两个表的关系。

The first one is the parent table
Table name: Parent
DB Columns

第一个是父
表表名:Parent
DB Columns

KEY1         Char      (first primary key field)
KEY2         Char      (second primary key field)
DESCRIPTION  Char
DEPENDENTID  BigInt

The second one is the dependent table
TableName: Dependent
DB Columns

第二个是依赖表
TableName: Dependent
DB Columns

PARENTID     BigInt    (first primary key field)
CODE         Char      (second primary key field)
FIELD1       Char
FIELD2       Char

I need to define the PK for both tables using @EmbeddedId annotation, so I have created the two classes:

我需要使用 @EmbeddedId 注释为两个表定义 PK,因此我创建了两个类:

@Embeddable
public class ParentPK implements Serializable
{
   @Column(name="K1")
   private String iK1;
   @Column(name="K2")
   private String iK2;
  // I omit the constructor, getter, setter, equals, hashcode method 
}

@Embeddable
public class DependentPK implements Serializable
{
  @Column(name="PARENTID")
  private String iParentId;
  @Column(name="CODE")
  private String iCode;
  // I omit the constructor, getter, setter, equals, hashcode method 
}

and then I've created the two beans:
The class for the DEPENDENT table.
Note that in this class I do not want to have any relational annotation

然后我创建了两个 bean:
DEPENDENT 表的类。
请注意,在这个类中我不想有任何关系注释

@Entity
@Table(name = "DEPENDENT")
public class DependentBean implements Serializable
{
   @EmbeddedId
   private DependentPK iDependentPK;
   @Column(name = "FIELD1")
   private String iField1;
   @Column(name = "FIELD2")
   private String iField2;
   // I omit the constructor, getter, setter methods
}

And the class for the PARENT table

和 PARENT 表的类

@Entity
@Table(name = "PARENT")
public class ParentBean implements Serializable
{
   @EmbeddedId
   ParentPK iParentPK;
   @Column(name = "DESCRIPTION")
   private String iDescription;
   @Column(name = "DEPENDENTID")
   private long iDependentId;
   @ElementCollection
   @CollectionTable(name="DEPENDENT", joinColumns={@JoinColumn(name="PARENTID",  referencedColumnName="DEPENDENTID")})
   private Set<DependentBean> iDependentBeans = new HashSet<DependentBean>();
   // I omit the constructor, getter, setter methods
}

When I try to deploy I got the error:

当我尝试部署时出现错误:

Caused by: org.hibernate.MappingException: Foreign key (FK9619C2A17B05CB2:DEPENDENT [iDependentBeans_PARENTID,iDependentBeans_CODE])) must have same number of columns as the referenced primary key (DEPENDENT [PARENTID,iDependentBeans_PARENTID,iDependentBeans_CODE])

引起:org.hibernate.MappingException:外键 (FK9619C2A17B05CB2:DEPENDENT [iDependentBeans_PARENTID,iDependentBeans_CODE])) 必须与引用的主键 (DEPENDENT [PARENTID,iDependentBeans_PARENTID,iCodeDepen]) 具有相同的列数

So I'm doing something wrong, but I can not imagine what. Could anyone wants to help me, please?

所以我做错了什么,但我无法想象是什么。请问有人愿意帮助我吗?

回答by study

@ElementCollection should be used with basic type or embeddable class, not for entity.

@ElementCollection 应该与基本类型或可嵌入类一起使用,而不是用于实体。

The DependentBean is an Entity.

DependentBean 是一个实体。

Try to use One To Many Mapping and modify your schema

尝试使用一对多映射并修改您的架构

PARENT schema

父模式

KEY1         Char      (PK)
KEY2         Char      (PK)
DEPENDENTID  BigInt    (PK)
DESCRIPTION  Char

DEPENDENT schema

依赖模式

CODE         Char      (PK)
PARENTID     BigInt    (FK)
KEY1         Char      (FK)
KEY2         Char      (FK)
FIELD1       Char
FIELD2       Char

One To Many mapping

一对多映射

ParentPK

父PK

@Embeddable
public class ParentPK {
    @Column(name = "K1")
    private String iK1;
    @Column(name = "K2")
    private String iK2;
    @Column(name = "DEPENDENTID")
    private long iDependentId;
}

ParentBean

父Bean

@Entity
@Table(name = "PARENT")
public class ParentBean {
    @EmbeddedId
    ParentPK iParentPK;

    @OneToMany(mappedBy = "parent")
    List<DependentBean> iDependentBeans;
}

DependentBean

依赖Bean

@Entity
@Table(name = "DEPENDENT")
public class DependentBean {
    @Id
    @Column(name = "CODE")
    private String iCode;

    @ManyToOne
    @JoinColumns({ 
      @JoinColumn(name = "PARENTID", referencedColumnName = "iDependentId"),  
      @JoinColumn(name = "K1", referencedColumnName = "iK1"),
      @JoinColumn(name = "K2", referencedColumnName = "iK2") })
    ParentBean parent;
}