JPA:使用复合主键和单键的实体映射 [英] JPA: Entity mapping with composite primary key and single key

查看:190
本文介绍了JPA:使用复合主键和单键的实体映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JPA 2,Hibernate 4.

从数据库视图创建实体。我很难让这两个实体正确映射。我有一个带有组合键的父实体和一个带有单个值键的子实体。



两者之间的关系由Child实体定义父母的组合键。试图建立两者之间的关联。



复合关键类

  @Embeddable 
public Class ParentID实现Serializable {

private static final long serialVersionUID = 1L;

private long keyIdOne;
private String keyIdTwo;
....
}

父类

  @Entity 
public class Parent {
@EmbeddedId
protected ParentID id;
....

@OneToMany
私人列表< Child>的childList;

子类

 @Entity 
public class Child {
@Id
private long Id;

私钥long keyIdOne // FK到parentId组合键的一部分
.....


@ManyToOne
私有父父母;
}

我不完全确定如何让映射工作。
从子项到父项的关系是 keyIdOne 值。但是,如果这是父级组合键的一部分,我不确定如何让它们加入该值。



在子类上,我是无法使用:

  @ManyToOne 
@JoinColumn(name =keyIdOne,referencedColumnName =keyIdOne)
私人父母;

会抛出以下错误:


referenceColumnNames(keyIdOne)Child引用Parent未映射到单个属性。


非常感谢。

解决方案

我相信问题出在PK-FK(主键 - 外键)约束上。 / b>

总结:

 家长: 
| keyIdOne | keyIdTwo |
| ---------- | ---------- |
| 1 | a |
| 1 | b |
| 3 | a |

孩子:
| id | keyIdOne |
| ---- | ---------- |
| 2 | 1 |
| 3 | 3 |

外键必须引用表中引用的一行( http://en.wikipedia.org/wiki/Foreign_key )。如果Child具有 keyIdOne = 1

$ b $,则Parent中的哪一行会执行 Child.keyIdOne b

我认为通过使用@MapsId()注释有可能实现此功能。然而,这只会反过来。
示例:

 父母:
| keyIdOne |
| ---------- |
| 1 |
| 3 |

孩子:
| keyIdOne | keyIdTwo |
| ---------- | ---------- |
| 1 | a |
| 1 | b |
| 3 | a |

在这种情况下,您可以将其复合键的子映射部分映射到Parent: p>

  @MapsId(keyIdOne)
私有父母;

@MapsId仅适用于OnetoOne和ManyToOne( http://docs.oracle.com/javaee/6/api/javax/persistence/MapsId.html


JPA 2, Hibernate 4.

Creating entities from a database view. I'm having difficulty getting these two entities to map correctly. I have a parent Entity with a composite key, and a child Entity with a single value key.

The relationship between the two is defined by the Child entity having part of the Parent's composite key. Trying to make the association between the two.

Composite Key Class

@Embeddable
public Class ParentID implements Serializable {

    private static final long serialVersionUID = 1L;

    private long keyIdOne;
    private String keyIdTwo;
    ....
}

Parent Class

@Entity
public class Parent {
    @EmbeddedId
    protected ParentID id;
    ....

    @OneToMany
    private List<Child> childList;
}

Child Class

@Entity
public class Child {
    @Id
    private long Id;

    private long keyIdOne //FK to part of the parentId composite key
    .....


    @ManyToOne
    private Parent parent;
}

I'm not entirely sure how to get the mapping to work. The relationship from the child to the parent is the keyIdOne value. However, where this is part of a composite key in the parent I'm not sure how to get them to join on that value.

On the child class I'm unable to use:

@ManyToOne
@JoinColumn(name="keyIdOne", referencedColumnName="keyIdOne") 
private Parent parent;

as it throws an error of :

referencedColumnNames(keyIdOne) of Child is referencing Parent not mapped to a single property.

Any help is much appreciated.

解决方案

I believe the issue to this problem is in PK-FK(Primary Key - Foreign Key) constraints.

In summary:

Parent:
| keyIdOne | keyIdTwo |
|----------|----------|
|    1     |    a     |
|    1     |    b     |
|    3     |    a     |

Child:
| id | keyIdOne |
|----|----------|
| 2  |    1     |
| 3  |    3     |

A foreign key must reference a single row in the table that it's referencing (http://en.wikipedia.org/wiki/Foreign_key). Which row in Parent does does Child.keyIdOne reference if Child has keyIdOne = 1?

I thought there was a possibility of this working by using the @MapsId() annotation. However this will only work in inverse. Example:

Parent:
| keyIdOne |
|----------|
|    1     |
|    3     |

Child:
| keyIdOne | keyIdTwo |
|----------|----------|
|    1     |    a     |
|    1     |    b     |
|    3     |    a     |

In this case you can have the Child map part of its composite key to the Parent with:

@MapsId("keyIdOne")
private Parent parent;

@MapsId only works in a OnetoOne and ManyToOne (http://docs.oracle.com/javaee/6/api/javax/persistence/MapsId.html)

这篇关于JPA:使用复合主键和单键的实体映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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