我正在尝试从数据库中打印详细信息。在这里,我从多个表中获取数据,然后在最后一行的BillNewAction
主方法上打印它,显示异常org.hibernate.LazyInitializationException: could not initialize proxy - no Session
以解决此问题我在lazy=false
处设置了<many-to-one name="productDetails" class="iland.hbm.ProductDetails" update="false" insert="false" lazy="false" fetch="join">
BillProductDetails.hbm.xml
但这并不能解决这个问题。
我应该做些什么改变来正确运行它。
public Map fetchAll(int start, int pageSize) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
List<BillDetails> obj = null;
long count = 0;
try {
count = (Long) session.createQuery("select count(*) from BillDetails").uniqueResult();
String hql = "from BillDetails as bd "
+ "left join fetch bd.customerDetails as cd "
+ "left join fetch bd.billProductDetailses as bpd "
+ "left join fetch bpd.productDetails";
Query query = session.createQuery(hql).setFirstResult(start).setMaxResults(pageSize);
obj = query.list();
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
e.printStackTrace();
tx.rollback();
}
} finally {
session.close();
}
Map data = new HashMap();
data.put("list", obj);
data.put("count", count);
return data;
}
打印详细信息
public class BillNewAction{
public static void main(String[] s) {
BillDAO dao = new BillDAO();
Map m = dao.fetchAll(0, 10);
List<BillDetails> billList = (List<BillDetails>) m.get("list");
for (BillDetails d : billList) {
System.out.println("bill no "+d.getBillNo() + " paymentType " + d.getPaymentType());
System.out.println("name "+d.getCustomerDetails().getName() + " address " + d.getCustomerDetails().getAddress() + " dob "
+ d.getCustomerDetails().getDob() + " anni " + d.getCustomerDetails().getAnniversery());
List<BillProductDetails> bpd = d.getBillProductDetailses();
System.out.println("bpd size is " + bpd.size());
for (BillProductDetails cd : bpd) {
System.out.println("qty "+cd.getQty() + " sp " + cd.getSellingPrice() + " up " + cd.getUnitPrice());//Throwing nullPointer Exception
/*Line 152*/ System.out.println(" barcode "+cd.getProductDetails().getBarcode() + "prType " + cd.getProductDetails().getProductTypes());
}
}
}
}
POJO&#39; S
public class BillDetails implements java.io.Serializable {
private Long billNo;
private CustomerDetails customerDetails;
private Float subTotal;
private Float vat;
private Float total;
private String paymentType;
private String status;
private Date addDate;
private List billProductDetailses = new ArrayList();
//getter and setter
}
public class BillProductDetails implements java.io.Serializable {
private BillProductDetailsId id;
private ProductDetails productDetails;
private BillDetails billDetails;
private long qty;
private float unitPrice;
private float sellingPrice;
private Integer discountPercent;
//getter and setter
}
public class ProductDetails implements java.io.Serializable {
private Long barcode;
private ProductBrand productBrand;
private Sizes sizes;
private ProductModelDetails productModelDetails;
private SupplierDetails supplierDetails;
private Colors colors;
private ProductTypes productTypes;
private long quntity;
private float unitPrice;
private float sellingPrice;
private Integer discountPercent;
private Date addDate;
private String status;
private Set billProductDetailses = new HashSet(0);
//getter and setter
}
BillDetails.hbm.xml
<hibernate-mapping>
<class name="iland.hbm.BillDetails" table="bill_details" catalog="retail_shop">
<id name="billNo" type="java.lang.Long">
<column name="bill_no" />
<generator class="identity"></generator>
</id>
<many-to-one name="customerDetails" class="iland.hbm.CustomerDetails" fetch="join">
<column name="customer_id" not-null="true" />
</many-to-one>
<property name="subTotal" type="java.lang.Float">
<column name="sub_total" precision="10" />
</property>
<property name="vat" type="java.lang.Float">
<column name="vat" precision="10" />
</property>
<property name="total" type="java.lang.Float">
<column name="total" precision="10" />
</property>
<property name="paymentType" type="string">
<column name="payment_type" length="30" not-null="true" />
</property>
<property name="status" type="string">
<column name="status" length="20" />
</property>
<property name="addDate" type="timestamp">
<column name="add_date" length="19" not-null="true" />
</property>
<set name="billProductDetailses" table="bill_product_details" inverse="true" lazy="false" fetch="join">
<key>
<column name="bill_no" not-null="true" />
</key>
<one-to-many class="iland.hbm.BillProductDetails" />
</set>
</class>
</hibernate-mapping>
BillProductDetails.hbm.xml
hibernate-mapping>
<class name="iland.hbm.BillProductDetails" table="bill_product_details" catalog="retail_shop">
<composite-id name="id" class="iland.hbm.BillProductDetailsId">
<key-property name="productId" type="long">
<column name="product_id" />
</key-property>
<key-property name="billNo" type="long">
<column name="bill_no" />
</key-property>
</composite-id>
<many-to-one name="productDetails" class="iland.hbm.ProductDetails" update="false" insert="false" lazy="false" fetch="join">
<column name="product_id" not-null="true" />
</many-to-one>
<many-to-one name="billDetails" class="iland.hbm.BillDetails" update="false" insert="false" fetch="join">
<column name="bill_no" not-null="true" />
</many-to-one>
<property name="qty" type="long">
<column name="qty" not-null="true" />
</property>
<property name="unitPrice" type="float">
<column name="unit_price" precision="7" not-null="true" />
</property>
<property name="sellingPrice" type="float">
<column name="selling_price" precision="7" not-null="true" />
</property>
<property name="discountPercent" type="java.lang.Integer">
<column name="discount_percent" />
</property>
</class>
</hibernate-mapping>
输出
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at iland.hbm.ProductTypes_$$_javassist_7.toString(ProductTypes_$$_javassist_7.java)
at java.lang.String.valueOf(String.java:2979)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at iland.bill.BillNewAction.main(BillNewAction.java:152)
Java Result: 1