-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: session.get(Xxx.class, id) wouldn't return null
PostPosted: Wed Sep 07, 2005 3:33 am 
Newbie

Joined: Tue Sep 06, 2005 4:12 am
Posts: 16
I find that session.get(Xxx.class, id) wouldn't return null when record not found, snippet code here:
Code:
   customer = (Customer)session.get(Customer.class, custno);
   if (customer.getAddr1() != null){
      System.out.println(customer.getCompany() + " - By get().");
   } else {
      System.out.println("Record not found");
   }

What's wrong here? Thank you very much.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 3:35 am 
Newbie

Joined: Tue Sep 06, 2005 4:12 am
Posts: 16
Snippet has some change, see here again:
Code:
   customer = (Customer)session.get(Customer.class, custno);
   if ([b]customer != null[/b]){
      System.out.println(customer.getCompany() + " - By get().");
   } else {
      System.out.println("Record not found");
   }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 3:40 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:58 am
Posts: 40
Location: Singapore
What is wrong with your code???

Dont you get NULL?
Or any exception?

Could you state clearly what you you need to know? get method always returns null if it cannot find the object that you are looking for in DB, whereas load throws an exception!!!


Top
 Profile  
 
 Post subject: Criteria
PostPosted: Wed Sep 07, 2005 4:02 am 
Beginner
Beginner

Joined: Tue Jul 19, 2005 4:03 am
Posts: 34
Location: Aberdeen, UK
As far as I can see, you are not querying correctly. Thee session.get() takes in an identifier as the second argument, but I don’t know if that’s what you want to use. Have you tried using Criteria.

Then it would be something like this

Code:
Criterion criterion  = Restrictions.eq(Customer.ID, customer.getId())
Criteria criteria = session.createCriteria(clazz).add(criterion);
customer = (Customer) criteria.uniqueResult();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 4:57 am 
Newbie

Joined: Tue Sep 06, 2005 4:12 am
Posts: 16
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 5:09 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
Hi,

Just a quick shot response before I try this out: have you tried to reverse the order of the tests (first get, then load)?

The documenation says the following about get:
Quote:
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance. (If the instance, or a proxy for the instance, is already associated with the session, return that instance or proxy.)

I'm not 100% sure how to interpret this, but maybe a proxy was created by the load and is returned again in the get - which leads to unexpected behaviour.

What you are testing is certainly not a standard use case - but it is quite unexpected...

Erik


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 9:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
and you are keeping the session opened even though an exception has occurred.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 10:15 am 
Beginner
Beginner

Joined: Thu Aug 11, 2005 11:16 am
Posts: 20
i have almost the same problem ! but me when it's return a result don't found in db , it's return something strange not a string and not nul and i can't display that because it's do a classcastexception like that :

property = BasepropertyDAO.getInstance().getpropertyByName("rouge",false);

if (property.getParam_name().toString().equals("rouge")) { <--- error here


Exception in thread "main" java.lang.ClassCastException: java.lang.String
at org.hibernate.type.IntegerType.set(IntegerType.java:39)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:62)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:44)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1115)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1177)
at org.hibernate.loader.Loader.doQuery(Loader.java:390)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1345)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:116)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:101)
at org.hibernate.persister.entity.BasicEntityPersister.load(BasicEntityPersister.java:2471)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:351)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:332)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:113)
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 model.AC_param$$EnhancerByCGLIB$$d94ceb45.getParam_name(<generated>)

_________________
Cyril,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 11:40 pm 
Newbie

Joined: Tue Sep 06, 2005 4:12 am
Posts: 16
I have tried to do get() before invoke load(), no error is appeared. But if I do get() again after load() , that means get() --> load() --> get(), the last get() is mal-function again. It can't return null (all test is based on no record return circumstance).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 11:41 pm 
Newbie

Joined: Tue Sep 06, 2005 4:12 am
Posts: 16
Obviously, get() method can't be invoked after load() method in a session, right?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 15, 2005 4:26 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
get() will return the proxy created by load() in this case, as per the javadoc.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.