Sorry everybody. Let me explain more detail. I want to practice the difference record retrieving by two methods -- load() & get().
I have a sample code as following:
Code:
private static void select8(Integer custno) {
Session session = ConnectionBean.getCurrentSession();
Customer customer = null;
try {
customer = (Customer)session.load(Customer.class, custno);
System.out.println(customer.getCompany() + " - By load().");
} catch (HibernateException e){
System.out.println("Record not found - From exception by load().");
}
customer = (Customer)session.get(Customer.class, custno);
if (customer != null){
System.out.println(customer.getCompany() + " - By get().");
} else {
System.out.println("Record not found - From return null by get()");
}
}
First to say, in Customer table, I have a record is primary key=1, but nothing record is primary key=2. Then:
After I call select8(1), it is very good for select record by both load() and get() methods. And then I call select8(2) to see how difference handling for record not found. I get the result from console:
Code:
Hibernate: select customer0_.custno as custno0_, customer0_.version as version0_0_, customer0_.company as company0_0_, customer0_.addr1 as addr4_0_0_, customer0_.addr2 as addr5_0_0_, customer0_.city as city0_0_, customer0_.state as state0_0_, customer0_.zip as zip0_0_, customer0_.country as country0_0_, customer0_.phone as phone0_0_, customer0_.fax as fax0_0_, customer0_.taxrate as taxrate0_0_, customer0_.contact as contact0_0_, customer0_.lastinvoicedate as lastinv14_0_0_ from customer customer0_ where customer0_.custno=?
16:42:56,967 INFO DefaultLoadEventListener : Error performing load command
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.pkg1.hibernate.Customer#12311]
at org.hibernate.ObjectNotFoundException.throwIfNull(ObjectNotFoundException.java:27)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:118)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:75)
at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:643)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:59)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
at com.pkg1.hibernate.Customer$$EnhancerByCGLIB$$1d5dd2dc.getCompany(<generated>)
at com.pkg1.client.ConsoleDemo.select8(ConsoleDemo.java:134)
at com.pkg1.client.ConsoleDemo.main(ConsoleDemo.java:305)
Record not found - From exception by load().
Hibernate: select customer0_.custno as custno0_, customer0_.version as version0_0_, customer0_.company as company0_0_, customer0_.addr1 as addr4_0_0_, customer0_.addr2 as addr5_0_0_, customer0_.city as city0_0_, customer0_.state as state0_0_, customer0_.zip as zip0_0_, customer0_.country as country0_0_, customer0_.phone as phone0_0_, customer0_.fax as fax0_0_, customer0_.taxrate as taxrate0_0_, customer0_.contact as contact0_0_, customer0_.lastinvoicedate as lastinv14_0_0_ from customer customer0_ where customer0_.custno=?
Hibernate: select customer0_.custno as custno0_, customer0_.version as version0_0_, customer0_.company as company0_0_, customer0_.addr1 as addr4_0_0_, customer0_.addr2 as addr5_0_0_, customer0_.city as city0_0_, customer0_.state as state0_0_, customer0_.zip as zip0_0_, customer0_.country as country0_0_, customer0_.phone as phone0_0_, customer0_.fax as fax0_0_, customer0_.taxrate as taxrate0_0_, customer0_.contact as contact0_0_, customer0_.lastinvoicedate as lastinv14_0_0_ from customer customer0_ where customer0_.custno=?
16:42:56,998 INFO DefaultLoadEventListener : Error performing load command
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.pkg1.hibernate.Customer#12311]
at org.hibernate.ObjectNotFoundException.throwIfNull(ObjectNotFoundException.java:27)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:118)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:75)
at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:643)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:59)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
at com.pkg1.hibernate.Customer$$EnhancerByCGLIB$$1d5dd2dc.getCompany(<generated>)
at com.pkg1.client.ConsoleDemo.select8(ConsoleDemo.java:141)
at com.pkg1.client.ConsoleDemo.main(ConsoleDemo.java:305)
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.pkg1.hibernate.Customer#12311]
at org.hibernate.ObjectNotFoundException.throwIfNull(ObjectNotFoundException.java:27)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:118)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:75)
at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:643)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:59)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
at com.pkg1.hibernate.Customer$$EnhancerByCGLIB$$1d5dd2dc.getCompany(<generated>)
at com.pkg1.client.ConsoleDemo.select8(ConsoleDemo.java:141)
at com.pkg1.client.ConsoleDemo.main(ConsoleDemo.java:305)
I found that the first exception is as expected ("Record not found - From exception by load().") But the subsequence exception is unexpected.
On the other hand, I try to remove a paragraph of code relation to load(), I found that if I just do try for get() only, it is ok.
Hope the information is enough to explain my problem.
Thank you very much.