gpt4 book ai didi

hibernate - JPA,Hibernate 我可以做复合主键,其中一个元素是外键@OneToMany?

转载 作者:行者123 更新时间:2023-12-01 12:49:23 27 4
gpt4 key购买 nike

我想在我的实体中包含由 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我一直在尝试类似于下面的解决方案,但无法通过 ServicePointPhotoId @IdClass 怎么写

extentions of previous example

最佳答案

去掉 providerId 字段及其对应的 getter 和 setter。向 getProvider() 添加一个 @Id 注释。像这样定义 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 中,属性类型必须与 ProviderId 属性类型匹配。

这在 JPA 2.1 规范的第 2.4.1 节中进行了讨论。

更新 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 属性的类型必须与引用的 EntityIdClass(即 ServicePointId)相匹配。

我使用了字段注释,但您可以将它们转换为属性注释。

同样:JPA 2.1 规范在第 2.4.1.3 节中有一个这种关系的示例。

关于hibernate - JPA,Hibernate 我可以做复合主键,其中一个元素是外键@OneToMany?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30718808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com