gpt4 book ai didi

java - 刷新 JPA 实体时出错

转载 作者:搜寻专家 更新时间:2023-10-31 20:23:10 25 4
gpt4 key购买 nike

我有以下领域模型

Currency ----< Price >---- Product

或英文

A Product has one or more Prices. Each Price is denominated in a particular Currency.

Price 有一个复合主键(由下面的 PricePK 表示),它由 CurrencyProduct 的外键组成。 JPA 注释的 Java 类的相关部分如下(大部分省略了 getter 和 setter):

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Currency {

@Id
private Integer ix;

@Column
private String name;

@OneToMany(mappedBy = "pricePK.currency", cascade = CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
private Collection<Price> prices = new ArrayList<Price>();
}

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Product {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@OneToMany(mappedBy = "pricePK.product", cascade = CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
private Collection<Price> defaultPrices = new ArrayList<Price>();
}

@Embeddable
public class PricePK implements Serializable {

private Product product;
private Currency currency;

@ManyToOne(optional = false)
public Product getProduct() {
return product;
}

@ManyToOne(optional = false)
public Currency getCurrency() {
return currency;
}
}

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Price {

private PricePK pricePK = new PricePK();

private BigDecimal amount;

@Column(nullable = false)
public BigDecimal getAmount() {
return amount;
}

public void setAmount(BigDecimal amount) {
this.amount = amount;
}

@EmbeddedId
public PricePK getPricePK() {
return pricePK;
}

@Transient
public Product getProduct() {
return pricePK.getProduct();
}

public void setProduct(Product product) {
pricePK.setProduct(product);
}

@Transient
public Currency getCurrency() {
return pricePK.getCurrency();
}

public void setCurrency(Currency currency) {
pricePK.setCurrency(currency);
}
}

当我尝试 refresh Product 的一个实例,我得到一个 StackOverflowError,所以我怀疑上面的映射中存在某种循环(或其他错误),有人能发现吗?

最佳答案

我见过几次这个错误,但我不记得确切的解决方案。我认为您需要从 PricePK(@ManyToOne)中删除映射并将其替换为价格上的@AssociationOverrides。

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@AssociationOverrides({
@AssociationOverride(name = "pricePK.product",
joinColumns = @JoinColumn(name = "product_id")),
@AssociationOverride(name = "pricePK.currency",
joinColumns = @JoinColumn(name = "currency_id"))
})
public class Price extends VersionedEntity {
[...]
}

请检查列名是否正确,因为我在 Product 或 Currency 上看不到 id 列。

关于java - 刷新 JPA 实体时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6471380/

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