JPA 2.0带有额外列的多对多 [英] JPA 2.0 many-to-many with extra column
问题描述
我试图在JPA 2.0(JBoss 7.1.1)中做一个ManyToMany关系,并且在关系中有一个额外的列(粗体,如下):
I'm trying to do a ManyToMany relationship in JPA 2.0 (JBoss 7.1.1) with an extra column (in bold, below) in the relationship, like:
Employer EmployerDeliveryAgent DeliveryAgent
(id,...) (employer_id, deliveryAgent_id, **ref**) (id,...)
我不想重复属性,所以我想应用 http:// giannigar .wordpress.com / 2009/09/04 / mapping-a-many-to-many-join-table-with-extra-column-using-jpa / 。但是我无法让它工作,我得到了几个错误,如:
I wouldn't like to have duplicate attributes, so I would like to apply the second solution presented in http://giannigar.wordpress.com/2009/09/04/mapping-a-many-to-many-join-table-with-extra-column-using-jpa/ . But I can't get it to work, I get several errors like:
- 嵌入式ID类不应包含关系映射(实际上spec中是这样说的);
- 在属性'employerDeliveryAgent'中,mapped by值'pk.deliveryAgent'不能被解析为目标实体上的属性;
- 在属性'employerDeliveryAgent'中,映射的值'pk.employer'无法解析为目标实体上的属性;
- 持续类型的覆盖属性
- 无法解析持续类型的覆盖属性pk.employer;
- Embedded ID class should not contain relationship mappings (in fact the spec says so);
- In attribute 'employerDeliveryAgent', the "mapped by" value 'pk.deliveryAgent' cannot be resolved to an attribute on the target entity;
- In attribute 'employerDeliveryAgent', the "mapped by" value 'pk.employer' cannot be resolved to an attribute on the target entity;
- Persistent type of override attribute "pk.deliveryAgent" cannot be resolved;
- Persistent type of override attribute "pk.employer" cannot be resolved;
$无法解析pk.deliveryAgent b $ b
该链接上的许多人都说它工作正常,所以我想我的环境中有些东西是不同的,可能是JPA或Hibernate版本。所以我的问题是:如何使用JPA 2.0(Jboss 7.1.1 /使用Hibernate作为JPA实现)来实现这种场景?为了补充这个问题:我应该避免使用组合键,而是使用纯生成的id和唯一约束吗?
Many people on that link said that it worked fine, so I suppose something is different in my environment, perhaps JPA or Hibernate version. So my question is: how do I achieve such scenario with JPA 2.0 (Jboss 7.1.1 / using Hibernate as JPA implementation)? And to complement that question: should I avoid using composite keys and instead use plain generated id and a unique constraint?
预先感谢。
Obs .:我没有在这里复制我的源代码,因为它基本上是上述链接中的一个副本,只是具有不同的类和属性名称,所以我想这不是必需的。 / p>
Obs.: I didn't copy my source code here because it is essentially a copy of the one at the link above, just with different classes and attributes names, so I guess it is not necessary.
推荐答案
首先,您需要生成 EmployerDeliveryAgentPK
类,因为它具有一个多重PK:
First of all You need to generate a EmployerDeliveryAgentPK
class because It has a multiple PK:
@Embeddable
public class EmployerDeliveryAgentPK implements Serializable {
@Column(name = "EMPLOYER_ID")
private Long employer_id;
@Column(name = "DELIVERY_AGENT_ID")
private Long deliveryAgent_id;
}
接下来,您需要创建一个 EmployerDeliveryAgent
class。这个类表示雇主
和 DeliveryAgent
的多对多表:
Next, You need to create a EmployerDeliveryAgent
class. This class represent many to many table of Employer
and DeliveryAgent
:
@Entity
@Table(name = " EmployerDeliveryAgent")
public class EmployerDeliveryAgent implements Serializable {
@EmbeddedId
private EmployerDeliveryAgentPK id;
@ManyToOne
@MapsId("employer_id") //This is the name of attr in EmployerDeliveryAgentPK class
@JoinColumn(name = "EMPLOYER_ID")
private Employer employer;
@ManyToOne
@MapsId("deliveryAgent_id")
@JoinColumn(name = "DELIVERY_AGENT_ID")
private DeliveryAgent deliveryAgent;
}
之后,在雇主$ c $您需要添加:
After that, in Employer
class You need to add:
@OneToMany(mappedBy = "deliveryAgent")
private Set<EmployerDeliveryAgent> employerDeliveryAgent = new HashSet<EmployerDeliveryAgent>();
并且在 DeliveryAgent
类中您需要添加:
And in DeliveryAgent
class You need to add:
@OneToMany(mappedBy = "employer")
private Set<EmployerDeliveryAgent> employer = new HashSet<EmployerDeliveryAgent>();
这就是全部!祝你好运!!
This is all! Good luck!!
这篇关于JPA 2.0带有额外列的多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!