我想在我的实体中包含由2列(属性)组成的复合主键,并将其中之一同时作为外键。
我写这样的东西,但不知道它是否有效,因为在IntelliJ数据源中将外键标记为生成值
@Entity
@Table(name = "service_point")
@Access(AccessType.PROPERTY)
@IdClass(ServicePointId.class)
public class ServicePoint {
private Long providerId;
private Integer servicePointNumber;
private Provider provider;
@Id
@Basic(optional = false)
@Column(name = "provider_id", nullable = false, insertable = false,
updatable = false, columnDefinition = "BIGINT UNSIGNED")
public Long getProviderId() {
return providerId;
}
public void setProviderId(Long providerId) {
this.providerId = providerId;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "service_point_no", nullable = false, columnDefinition = "BIGINT UNSIGNED")
public Integer getServicePointNumber() {
return servicePointNumber;
}
public void setServicePointNumber(Integer servicePointNumber) {
this.servicePointNumber = servicePointNumber;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "provider_id")
public Provider getProvider() {
return provider;
}
public void setProvider(Provider provider) {
this.provider = provider;
}
}
更新:
我已经测试了Brian Vosburgh,它可以工作:
transaction.begin();
em.persist(provider);
ServicePoint servicePoint = new ServicePoint(provider, 1);
em.persist(servicePoint);
transaction.commit();
ServicePoint servicePoint2 = em.find(ServicePoint.class,
new ServicePointId(provider.getUserId(), servicePoint.getServicePointNumber()));
assertTrue("Service point provider id and Provider provider id should be the same.",
servicePoint2.getProvider().getUserId() == provider.getUserId());
assertNotNull("Service point number can not be null", servicePoint2.getServicePointNumber());
assertEquals(servicePoint2.getProvider(), provider);
transaction.begin();
em.remove(servicePoint);
em.remove(provider);
transaction.commit();
更新2-下一个关系复合PK中的新问题(3列),其中2个是复合FK ojit_rstrong
我一直在尝试类似于以下解决方案,但无法通过
如何编写ServicePointPhotoId @IdClass
最佳答案
摆脱providerId
字段及其对应的getter和setter。在@Id
中添加getProvider()
批注。像这样定义IdClass
:
public class ServicePointId {
private Long provider;
private Integer servicePointNumber;
public Integer getProvider() {
return provider;
}
public void setProvider(Integer provider) {
this.provider = provider;
}
public Integer getServicePointNumber() {
return servicePointNumber;
}
public void setServicePointNumber(Integer servicePointNumber) {
this.servicePointNumber = servicePointNumber;
}
}
请注意,
IdClass
中的属性名称与Entity
中的属性名称(即provider
)匹配,但是属性的类型不同。在IdClass
中,属性类型必须与Id
的Provider
属性的类型匹配。在JPA 2.1规范的2.4.1节中对此进行了讨论。
UPDATE 2的建议
public class ServicePointPhotoId {
public ServicePointId servicePoint;
public Long photoId;
}
@Entity
@IdClass(ServicePointPhotoId.class)
@Table(name="service_point_photo")
public class ServicePointPhoto {
@Id
@ManyToOne
@JoinColumns({
@JoinColumn(name="provider_id", referencedColumnName="provider_id"),
@JoinColumn(name="service_point_no", referencedColumnName="service_point_no")
})
private ServicePoint servicePoint;
@Id
@Column(name="photo_id")
private Long photoId;
}
请注意,属性名称必须匹配(即
servicePoint
);但IdClass
属性的类型必须与引用的Entity
的IdClass
(即ServicePointId
)匹配。我使用了字段注释,但是您可以将其转换为属性注释。
再次:JPA 2.1规范在2.4.1.3节中仅提供了这种关系的示例。