我正在尝试编写一个库存管理系统。但现在我有几个问题。请帮我解决这些问题。
我有 2 个实体。Item
和ItemPrice
。Item
有一个或多个 ItemPrice
当我尝试保存Item
对象时,它工作正常;但是当我尝试从数据库中检索详细信息时,它会给出以下Exception
堆栈跟踪。
org.hibernate.HibernateException:找到对集合的共享引用:com.pos.entities.ItemGroup.items Hibernate:选择 itemmodel0_.id 作为 id9_,itemmodel0_.item_model 作为 item2_9_,itemmodel0_.status 作为 status9_ from smartpos.item_model itemmodel0_ where (itemmodel0_.status=? ) 休眠:从 smartpos.item_size itemize0_ where (itemsize0_.status=? ) 中选择 itemize0_.id 作为 id3_,itemsize0_.item_size 作为 item2_3_,itemsize0_.status 作为 status3_ 在 org.hibernate.engine.Collections.processReachableCollection(Collections.java:163) 在 org.hibernate.event.def.FlushVisitor.processCollection(FlushVisitor.java:37) 在 org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101) 在 org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61) 在 org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55) 在 org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:138) 在 org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196) 在 org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76) 在 org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26) 在 org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) 在 org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) 在 org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) 在 com.pos.dao.ItemDaoImpl.getItem(ItemDaoImpl.java:761) 在 com.pos.manager.ItemManager.getItem(ItemManager.java:296) 在 com.pos.ui.ItemDefinitionForm$RefreshTask.doInBackground(ItemDefinitionForm.java:464) 在 com.pos.ui.ItemDefinitionForm$RefreshTask.doInBackground(ItemDefinitionForm.java:458) 在 javax.swing.SwingWorker$1.call(SwingWorker.java:277) 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) 在 java.util.concurrent.FutureTask.run(FutureTask.java:138) 在 javax.swing.SwingWorker.run(SwingWorker.java:316) 在 java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 在 java.lang.Thread.run(Thread.java:619) 2012 年 11 月 7 日 12:03:42 PM org.hibernate.LazyInitializationException 严重:未能延迟初始化角色集合:com.pos.entities.Item.itemPrices,没有会话或会话已关闭 org.hibernate.LazyInitializationException:未能延迟初始化角色集合:com.pos.entities.Item.itemPrices,没有会话或会话已关闭 在 org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358) 在 org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350) 在 org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97) 在 org.hibernate.collection.PersistentSet.size(PersistentSet.java:139) 在 com.pos.ui.ItemDefinitionForm$RefreshTask.doInBackground(ItemDefinitionForm.java:520) 在 com.pos.ui.ItemDefinitionForm$RefreshTask.doInBackground(ItemDefinitionForm.java:458) 在 javax.swing.SwingWorker$1.call(SwingWorker.java:277) 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) 在 java.util.concurrent.FutureTask.run(FutureTask.java:138) 在 javax.swing.SwingWorker.run(SwingWorker.java:316) 在 java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 在 java.lang.Thread.run(Thread.java:619) org.hibernate.LazyInitializationException:未能延迟初始化角色集合:com.pos.entities.Item.itemPrices,没有会话或会话已关闭 在 org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358) 在 org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350) 在 org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97) 在 org.hibernate.collection.PersistentSet.size(PersistentSet.java:139) 在 com.pos.ui.ItemDefinitionForm$RefreshTask.doInBackground(ItemDefinitionForm.java:520) 在 com.pos.ui.ItemDefinitionForm$RefreshTask.doInBackground(ItemDefinitionForm.java:458) 在 javax.swing.SwingWorker$1.call(SwingWorker.java:277) 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) 在 java.util.concurrent.FutureTask.run(FutureTask.java:138) 在 javax.swing.SwingWorker.run(SwingWorker.java:316) 在 java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 在 java.lang.Thread.run(Thread.java:619)
这是实体类文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 3, 2012 10:36:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class catalog="smartpos" name="com.pos.entities.ItemPrice" table="item_price" lazy="false">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="identity"/>
</id>
<many-to-one class="com.pos.entities.Item" fetch="select" name="item">
<column name="item_id" not-null="true"/>
</many-to-one>
<many-to-one class="com.pos.entities.PriceList" fetch="select" name="priceList">
<column name="price_list_id" not-null="true"/>
</many-to-one>
<property name="price" type="float">
<column name="price" not-null="true" precision="12" scale="0"/>
</property>
物品实体
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 3, 2012 10:36:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class catalog="smartpos" lazy="false" name="com.pos.entities.Item" table="item">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="identity"/>
</id>
<many-to-one class="com.pos.entities.ItemColor" fetch="select" name="itemColor">
<column name="item_color_id" not-null="true"/>
</many-to-one>
<many-to-one class="com.pos.entities.ItemModel" fetch="select" name="itemModel">
<column name="item_model_id" not-null="true"/>
</many-to-one>
<many-to-one class="com.pos.entities.ItemGrade" fetch="select" name="itemGrade">
<column name="item_grade_id" not-null="true"/>
</many-to-one>
<many-to-one class="com.pos.entities.BusinessPartner" fetch="select" name="businessPartner">
<column name="supplier_id" not-null="true"/>
</many-to-one>
<many-to-one class="com.pos.entities.ItemSize" fetch="select" name="itemSize">
<column name="item_size_id" not-null="true"/>
</many-to-one>
<many-to-one class="com.pos.entities.Login" fetch="select" name="login">
<column name="login_id" not-null="true"/>
</many-to-one>
<many-to-one class="com.pos.entities.ItemGroup" fetch="select" name="itemGroup">
<column name="item_group_id" not-null="true"/>
</many-to-one>
<many-to-one class="com.pos.entities.ItemBrand" fetch="select" name="itemBrand">
<column name="item_brand_id" not-null="true"/>
</many-to-one>
<property name="itemCode" type="string">
<column length="50" name="item_code" not-null="true" unique="true"/>
</property>
<property name="itemName" type="string">
<column length="200" name="item_name" not-null="true"/>
</property>
<property name="shortName" type="string">
<column length="100" name="short_name" not-null="true"/>
</property>
<property name="barcode" type="string">
<column length="50" name="barcode" not-null="true"/>
</property>
<property name="warrentyItem" type="byte">
<column name="warrenty_item" not-null="true"/>
</property>
<property name="taxableItem" type="byte">
<column name="taxable_item" not-null="true"/>
</property>
<property name="status" type="byte">
<column name="status" not-null="true"/>
</property>
<property name="createdDate" type="timestamp">
<column length="19" name="created_date" not-null="true"/>
</property>
<property name="batchSerial" type="byte">
<column name="batch_serial" not-null="true"/>
</property>
<property name="warrentyPeriod" type="int">
<column name="warrenty_period" not-null="true"/>
</property>
<set inverse="true" name="itemPrices" cascade="save-update">
<key>
<column name="item_id" not-null="true"/>
</key>
<one-to-many class="com.pos.entities.ItemPrice"/>
</set>
图形用户界面编码
headers = new SupportedMethod().getTableHeaderValues(tblItemPrice);
System.out.println("sdff" + item.getItemPrices().size());
itemPriceSet = item.getItemPrices();
Iterator it = itemPriceSet.iterator();
while (it.hasNext()) {
ItemPrice ip = (ItemPrice) it.next();
Vector<Object> oneRow = new Vector<Object>();
oneRow.add(ip.getId());
oneRow.add(ip.getPriceList().getPriceListName());
oneRow.add(Float.toString(ip.getPrice()));
tableData.add(oneRow);
}
tblItemPrice.setModel(new DefaultTableModel(tableData, headers));
cascade
没关系,我已经设置了lazy=false
,但它会抛出Exception
. 请任何人帮助我。