直接上問題: 一開始報出 HibernateException,這個問題原因是:struts2和hibernate整合時,有些jar包衝突原因,如Javassist.jar,保留高版本吧。解決jar報衝突問題後又給報錯誤了。 上代碼: 我們知道;對於一些比較複雜的業務,事務都放在業務層開啟和關閉,我 ...
直接上問題:
org.hibernate.HibernateException: HHH000142: Javassist Enhancement failed: cn.xxx.pojo.Customer at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:130) at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:666) at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4462) at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:359) at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:274) at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121) at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89) at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129) at org.hibernate.internal.SessionImpl.access$2600(SessionImpl.java:164) at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.getReference(SessionImpl.java:2669) at org.hibernate.internal.SessionImpl.load(SessionImpl.java:965) at cn.xxx.dao.impl.CustomerDaoImpl.findOrderByCst(CustomerDaoImpl.java:45) at cn.xxx.dao.impl.CustomerDaoImpl.demo1(CustomerDaoImpl.java:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) Caused by: java.lang.NoClassDefFoundError: javassist/util/proxy/Proxy at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:123) ... 35 more Caused by: java.lang.ClassNotFoundException: javassist.util.proxy.Proxy at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 36 more
一開始報出 HibernateException,這個問題原因是:struts2和hibernate整合時,有些jar包衝突原因,如Javassist.jar,保留高版本吧。解決jar報衝突問題後又給報錯誤了。
18:50:37.720 [main] ERROR org.hibernate.proxy.pojo.javassist.JavassistProxyFactory - HHH000142: Javassist Enhancement failed: cn.xxx.pojo.Customer java.lang.ClassCastException: cn.xxx.pojo.Customer_$$_javassist_3 cannot be cast to javassist.util.proxy.Proxy at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:123) [hibernate-core-5.0.7.Final.jar:5.0.7.Final] at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:666) [hibernate-core-5.0.7.Final.jar:5.0.7.Final] at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4462) [hibernate-core-5.0.7.Final.jar:5.0.7.Final] at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:359) [hibernate-core-5.0.7.Final.jar:5.0.7.Final] at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:274) [hibernate-core-5.0.7.Final.jar:5.0.7.Final] at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121) [hibernate-core-5.0.7.Final.jar:5.0.7.Final] at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89) [hibernate-core-5.0.7.Final.jar:5.0.7.Final] at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129) [hibernate-core-5.0.7.Final.jar:5.0.7.Final] at org.hibernate.internal.SessionImpl.access$2600(SessionImpl.java:164) [hibernate-core-5.0.7.Final.jar:5.0.7.Final] at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.getReference(SessionImpl.java:2669) [hibernate-core-5.0.7.Final.jar:5.0.7.Final] at org.hibernate.internal.SessionImpl.load(SessionImpl.java:965) [hibernate-core-5.0.7.Final.jar:5.0.7.Final] at cn.xxx.dao.impl.CustomerDaoImpl.findOrderByCst(CustomerDaoImpl.java:45) [classes/:?] at cn.xxx.dao.impl.CustomerDaoImpl.demo1(CustomerDaoImpl.java:53) [classes/:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_112] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_112] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_112] at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_112] at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) [junit.jar:4.12] at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit.jar:4.12] at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) [junit.jar:4.12] at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) [junit.jar:4.12] at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) [junit.jar:4.12] at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) [junit.jar:4.12] at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) [junit.jar:4.12] at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit.jar:4.12] at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit.jar:4.12] at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit.jar:4.12] at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit.jar:4.12] at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit.jar:4.12] at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit.jar:4.12] at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) [.cp/:?] at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:?] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:?] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) [.cp/:?] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:?] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:?]
上代碼:
@Override public Set<Order> findOrderByCst(Long cstId) {
//這裡面getSession方法直接獲取當前線程的session對象 Session session = HibernateSessionFactory.getSession(); Set<Order> orders = session.load(Customer.class, cstId).getOrders(); return orders; } @Test public void demo1(){ Session session = HibernateSessionFactory.getSession(); Transaction transaction = session.beginTransaction(); Set<Order> orders = findOrderByCst(1L); for (Order order : orders) { System.out.println(order); } transaction.commit(); }
我們知道;對於一些比較複雜的業務,事務都放在業務層開啟和關閉,我們經常需要保證連接對象的一致,不然可能導致某些邏輯操作成功另一些失敗,不符合原子性。這裡有一篇文章介紹的很詳細,概念和例子:https://blog.csdn.net/weixin_40657079/article/details/82055822。
那麼上面代碼就是基於dao層存放操作,service層存放事務。但是現在出現問題,查詢一對多的關聯對象時候,我用的時load方法,我們知道load是懶載入,他必須在有session的持久態情況才能使用,用的時候才會向資料庫發送數據,離開session進入脫管在查詢就會報錯。
解決辦法:
1:不用懶載入。
用get方法或者在配置映射文件配置lazy=false。這裡是一對多,一個顧客多個訂單,主控方為顧客,關聯方為訂單,所以在主控方set配置lazy=false(也就是配置關聯級別延遲載入)
2:離線條件查詢(QBC檢索):
指的是脫離session進行條件查詢,如果什麼條件都不設置就是查詢全部。
Web層或者Service層獲取DetachedCriteria對象,
DetachedCriteria dc = DetachedCriteria.forClass(cCustomer.class)
dao層:
Criteria criteria = dc.getExecutableCriteria(session);
criteria.list();
這樣照樣可以使用懶載入,降低記憶體消耗