[英]Hibernate oneToMany - object references an unsaved transient instance
I have a table with a oneToMany relation:我有一张具有 oneToMany 关系的表:
<hibernate-mapping>
<class name="com.al6.jtob.data.Lsell" table="LSELL" >
...
<many-to-one name="stock" class="com.al6.jtob.data.Stock" insert="false" update="false">
<column name="IDITEM" />
<column name="IDWAREHOUSE" />
</many-to-one>
...
</class>
</hibernate-mapping>
The StockID is composed by (IDITEM, IDWAREHOUSE). StockID 由 (IDITEM, IDWAREHOUSE) 组成。
It works well when I load existing LSELL objects.当我加载现有的 LSELL 对象时,它运行良好。 But when I create a new LSELL object, I have this error:但是当我创建一个新的 LSELL object 时,我有这个错误:
Caused by: javax.ejb.EJBException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.al6.jtob.data.Stock
I don't want to update or save any new "stock" into database, it's just a readonly data.我不想更新或将任何新的“库存”保存到数据库中,它只是一个只读数据。 All the stock data are complete and suffisant.所有的库存数据都是完整和充足的。 The goal is just to get the "stock" of the "Lsell" when I display a "Lsell".目标只是在我显示“Lsell”时获得“Lsell”的“库存”。
You forget to declare CascadeType
in relational mapping.您忘记在关系映射中声明CascadeType
。 you have to put cascade=all
in mapping tag because CascadeType
propagates entity transitions from Parent to Child.您必须将cascade=all
放在映射标记中,因为CascadeType
将实体转换从父级传播到子级。 For example, if you persist parent entity the referenced entities also get persisted before flushing.例如,如果您持久化父实体,则引用的实体也会在刷新之前持久化。
<one-to-many name="lsell" class="com.al6.jtob.data.Lsell" cascade="all">
...
</one-to-many>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.