gpt4 book ai didi

hibernate - org.hibernate.PersistentObjectException : detached entity passed to persist

转载 作者:行者123 更新时间:2023-12-03 05:18:28 35 4
gpt4 key购买 nike

我已经成功地用 hibernate 编写了我的第一个主子示例。几天后我又拿了它并升级了一些库。不知道我做了什么,但我再也无法让它运行了。有人可以帮我找出返回以下错误消息的代码有什么问题吗:

org.hibernate.PersistentObjectException: detached entity passed to persist: example.forms.InvoiceItem
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:127)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:799)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:791)
.... (truncated)

hibernate 映射:

<hibernate-mapping package="example.forms">
<class name="Invoice" table="Invoices">
<id name="id" type="long">
<generator class="native" />
</id>
<property name="invDate" type="timestamp" />
<property name="customerId" type="int" />
<set cascade="all" inverse="true" lazy="true" name="items" order-by="id">
<key column="invoiceId" />
<one-to-many class="InvoiceItem" />
</set>
</class>
<class name="InvoiceItem" table="InvoiceItems">
<id column="id" name="itemId" type="long">
<generator class="native" />
</id>
<property name="productId" type="long" />
<property name="packname" type="string" />
<property name="quantity" type="int" />
<property name="price" type="double" />
<many-to-one class="example.forms.Invoice" column="invoiceId" name="invoice" not-null="true" />
</class>
</hibernate-mapping>

编辑: InvoiceManager.java

class InvoiceManager {

public Long save(Invoice theInvoice) throws RemoteException {
Session session = HbmUtils.getSessionFactory().getCurrentSession();
Transaction tx = null;
Long id = null;
try {
tx = session.beginTransaction();
session.persist(theInvoice);
tx.commit();
id = theInvoice.getId();
} catch (RuntimeException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
throw new RemoteException("Invoice could not be saved");
} finally {
if (session.isOpen())
session.close();
}
return id;
}

public Invoice getInvoice(Long cid) throws RemoteException {
Session session = HbmUtils.getSessionFactory().getCurrentSession();
Transaction tx = null;
Invoice theInvoice = null;
try {
tx = session.beginTransaction();
Query q = session
.createQuery(
"from Invoice as invoice " +
"left join fetch invoice.items as invoiceItems " +
"where invoice.id = :id ")
.setReadOnly(true);
q.setParameter("id", cid);
theInvoice = (Invoice) q.uniqueResult();
tx.commit();
} catch (RuntimeException e) {
tx.rollback();
} finally {
if (session.isOpen())
session.close();
}
return theInvoice;
}
}

发票.java

public class Invoice implements java.io.Serializable {

private Long id;
private Date invDate;
private int customerId;
private Set<InvoiceItem> items;

public Long getId() {
return id;
}

public Date getInvDate() {
return invDate;
}

public int getCustomerId() {
return customerId;
}

public Set<InvoiceItem> getItems() {
return items;
}

void setId(Long id) {
this.id = id;
}

void setInvDate(Date invDate) {
this.invDate = invDate;
}

void setCustomerId(int customerId) {
this.customerId = customerId;
}

void setItems(Set<InvoiceItem> items) {
this.items = items;
}
}

InvoiceItem.java

public class InvoiceItem implements java.io.Serializable {

private Long itemId;
private long productId;
private String packname;
private int quantity;
private double price;
private Invoice invoice;

public Long getItemId() {
return itemId;
}

public long getProductId() {
return productId;
}

public String getPackname() {
return packname;
}

public int getQuantity() {
return quantity;
}

public double getPrice() {
return price;
}

public Invoice getInvoice() {
return invoice;
}

void setItemId(Long itemId) {
this.itemId = itemId;
}

void setProductId(long productId) {
this.productId = productId;
}

void setPackname(String packname) {
this.packname = packname;
}

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

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

void setInvoice(Invoice invoice) {
this.invoice = invoice;
}
}

编辑:从客户端发送的 JSON 对象:

{"id":null,"customerId":3,"invDate":"2005-06-07T04:00:00.000Z","items":[
{"itemId":1,"productId":1,"quantity":10,"price":100},
{"itemId":2,"productId":2,"quantity":20,"price":200},
{"itemId":3,"productId":3,"quantity":30,"price":300}]}

编辑:一些细节:
我尝试通过以下两种方式保存发票:

  1. 上述手动制作json 对象并将其传递给 fresh服务器的 session 。在这种情况下绝对调用之前未进行任何事件save 方法,因此不应有任何打开的 session 除了在 save 方法中打开的之外

  2. 使用以下方式加载现有数据getInvoice 方法和他们通过相同删除键值后的数据。这我也信应在另存为之前关闭 session 事务正在 getInvoice 方法中提交。

在这两种情况下,我都收到相同的错误消息,这迫使我相信 hibernate 配置文件或实体类或保存方法有问题。

如果我需要提供更多详细信息,请告诉我

最佳答案

您没有提供很多相关详细信息,因此我猜测您调用了 getInvoice,然后使用结果对象设置了一些值并调用 save 并假设您的对象更改将被保存。

但是,persist 操作适用于全新的 transient 对象,如果已分配 id,则操作会失败。在您的情况下,您可能想调用 saveOrUpdate 而不是 persist

您可以在此处找到一些讨论和引用"detached entity passed to persist error" with JPA/EJB code

关于hibernate - org.hibernate.PersistentObjectException : detached entity passed to persist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6378526/

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