gpt4 book ai didi

java - 级联属性导致的分离实体错误

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

我是 JPA 和 Hibernate 的新手。我有两个实体:供应商和产品,如下所述。

@Entity
public class Vendor extends BaseEntity
{
private static final long serialVersionUID = 267250313080292374L;

@NotNull
private String name;

@NotNull
private String city;

@OneToMany(mappedBy = "vendor", cascade = CascadeType.ALL)
private List<Product> products = new ArrayList<Product>();

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public List<Product> getProducts() {
return products;
}

public void setProducts(List<Product> products) {
this.products = products;
}
}

@Entity
public class Product extends BaseEntity
{
private static final long serialVersionUID = -4676899182130380017L;

@NotNull
private String name;

@Column(nullable = false, precision = 10, scale = 2)
private double price;

@NotNull
private int quantity;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="VENDOR_ID", nullable = false)
private Vendor vendor;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public Vendor getVendor() {
return vendor;
}

public void setVendor(Vendor vendor) {
this.vendor = vendor;
}
}

我的问题与 @ManyToOne 注释中属性 nullable = false 的使用有关。我希望用 @ManyToOne 注释的相应列不为空。但是,当我使用以下测试来测试实体的持久性时:

public void testCreateEntity()
{

// create vendor1, set name and city
vendor1 = new Vendor();
vendor1.setName("MediaMarkt");
vendor1.setCity("Berlin");
vendor1 = service.createOrUpdateEntity(vendor1);

// create product1, set name, price and quantity
product1 = new Product();
product1.setName("Samsung Galaxy S7");
product1.setPrice(new Double("819.00"));
product1.setQuantity(1);

vendor1.getProducts().add(product1);
product1.setVendor(vendor1);

// create product at service
product1 = service.createOrUpdateEntity(product1);
...
}

其中 createOrUpdateEntity 方法是 DAO Bean 对象的一部分

 @PersistenceContext
private EntityManager em;

public <T extends BaseEntity> T createOrUpdateEntity(T entity)
{
// as we have no id yet, it must be freshly brewed
if (entity.getId() == 0)
{
log.debug("createOrUpdateEntity::create:: {}", entity);
this.em.persist(entity);
}
// if there is an id, we must have dealt with it before
else
{
log.debug("createOrUpdateEntity::update:: {}", entity);
this.em.merge(entity);
}
this.em.flush();

return entity;
}

我收到错误:

Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: de.brockhaus.stock.entity.Stock
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:124)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:765)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:758)
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener$1.cascade(JpaPersistEventListener.java:80)
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:398)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:162)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:111)
at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:425)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:249)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:775)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:753)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146)
... 138 more

编辑:实现此example ,它使用 @ManyToOne 列注释为 nullable = false,我意识到我的问题与此无关。如之前的服务器日志所示,问题是由 Cascade 类引起的。一些solutions因此建议将 CascadeType.ALL 更改为 CascadeType.MERGE。其他ones展示不同的策略。甚至一些ones告诉只有关联的 OneToMany 端才应该使用级联。坦率地说,我认为在关联双方都使用 CascadeType.ALL 是最好的选择。但看来我错了。虽然,正如我所指出的,我是 JPA 和 Hibernate 的新手,而且我不明白其中的原因。

最佳答案

您可以使用可选标签进行空控制:

@ManyToOne(cascade = CascadeType.ALL, optional = false)
@JoinColumn(name="VENDOR_ID")
private Vendor vendor;

关于java - 级联属性导致的分离实体错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38566417/

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