-->
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.  [ 9 posts ] 
Author Message
 Post subject: ObjectNotFoundException not recoverable?
PostPosted: Thu Jul 14, 2005 5:26 pm 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Hi,

I read in the Hibernate JavaDoc of ObjectNotFoundException class:

Code:
public class ObjectNotFoundException
extends UnresolvableObjectException

Thrown when Session.load() fails to select a row with the given primary key (identifier value). This exception might not be thrown when load() is called, even if there was no row on the database, because load() returns a proxy if possible. Applications should use Session.get() to test if a row exists in the database.

Like all Hibernate exceptions, this exception is considered unrecoverable.


I am using Spring 1.2 and Hibernate 3.0.5 on a legacy DB (50 tables and many links between tables). I cannot garantee the integrity of the database, so it can happen that a foreign key is pointing to an inexisting record.

So I have a one-to-one relationship from table A to table B, and sometime the foreign key from A pointing to B is incoherent. Per example, it can be null (or "") or having corrupted data. So sometime, when I check on the one-to-one property (B) of A like:

Code:
if(a.getB()!=null)....


I get a ObjectNotFoundException.

Can I tell Hibernate to put a null value to the B property instead of throwing an unchecked exception when it cannot find the record? Is there another "easy way" to control this exception when occuring? Or finally should I put a try/catch ObjectNotFoundException everywhere in my code?

Thanks.

Etienne.
Montreal.

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 5:28 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
Use get() instead.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 5:35 pm 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
I read that in the Javadoc, but as you can see, I never use a session.load().

To retreive my object, I use a DAO under Spring. Per exemple this finder use a composite key:

Code:
   public Composante findComposanteByIds(String institution, String transit, short serviceCenter) {
      
      Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);
      Criteria criteria = session.createCriteria(Composante.class);
      criteria.add(Expression.eq("id.institution", institution));
      criteria.add(Expression.eq("id.transit", transit));
      criteria.add(Expression.eq("id.serviceCenter", new Short(serviceCenter)));
      return (Composante)criteria.uniqueResult();
   }


Having the "composante", i just do a

Code:
composante.getB();


in my code and doing that, i get the ObjectNotFoundException because the foreign key of my composante is equal to "" or any corrupted value...

So, I dont see how I could use a session.get() here.... instead of the session.load() probably made by the composante.getB();

Thanks.

Etienne.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 5:38 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Check out 'not-found' attribute in many-to-one:

http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-declaration-manytoone


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 6:14 pm 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Does this not-found="ignore" is a valide attribute?

I have this strange validation problem, neither Eclipse with the Hibernate Plugins (Tools) or WSAD with XML Validation seems to validate this XML attribute

I am using the good header:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


and this many-to-many tag:

Code:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field">
<!--
        Auto-generated mapping file from
        the hibernate.org cfg2hbm engine
-->
<class name="com.ComposanteExtention"
         table="ComposanteExtention"
           schema="dbo"
           catalog="as400_goodname"
           mutable="false">
    <composite-id name="id" class="com.ComposanteId">
      <key-property name="institution" type="com.TrimmedString">
        <column name="institution" scale="3" precision="0" not-null="true" sql-type="char" />
      </key-property>
      <key-property name="transit" type="com.TrimmedString">
        <column name="transit" scale="5" precision="0" not-null="true" sql-type="char" />
      </key-property>
      <key-property name="serviceCenter" type="short">
        <column name="serviceCenter" scale="3" precision="0" not-null="true" sql-type="numeric" />
      </key-property>
    </composite-id>
   
    <one-to-one name="composante"
            class="com.Composante">
   </one-to-one>
   <!-- PROBLEM HERE WITH NOT-FOUND TAG -->
    <many-to-one name="registeredRepIntegrate" not-found="ignore" insert="false" update="false" class="com.RegisteredRep">
      <column name="registeredRepIntegrateCode" scale="4" precision="0" not-null="false" />
    </many-to-one>

   etc...

   </class>



The error of both Eclipse 3.1 and WSAD 5.1.2 is :

Code:
Error         Attribute "not-found" is not declared for element "many-to-one".   ComposanteExtention.hbm.xml   persistence_NameOnly/src/com/ line 30



But the DTD of
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd
seems ok...


Code:
<!ELEMENT many-to-one (meta*,(column|formula)*)>
   <!ATTLIST many-to-one name CDATA #REQUIRED>
   <!ATTLIST many-to-one access CDATA #IMPLIED>
   <!ATTLIST many-to-one class CDATA #IMPLIED>
   <!ATTLIST many-to-one entity-name CDATA #IMPLIED>
   <!ATTLIST many-to-one column CDATA #IMPLIED>
   <!ATTLIST many-to-one not-null (true|false) #IMPLIED>
   <!ATTLIST many-to-one unique (true|false) "false">
   <!ATTLIST many-to-one unique-key CDATA #IMPLIED>
   <!ATTLIST many-to-one index CDATA #IMPLIED>
   <!ATTLIST many-to-one cascade CDATA #IMPLIED>
   <!ATTLIST many-to-one outer-join (true|false|auto) #IMPLIED>
   <!ATTLIST many-to-one fetch (join|select) #IMPLIED>
   <!ATTLIST many-to-one update (true|false) "true">
   <!ATTLIST many-to-one insert (true|false) "true">
   <!ATTLIST many-to-one optimistic-lock (true|false) "true">   <!-- only supported for properties of a class (not component) -->
   <!ATTLIST many-to-one foreign-key CDATA #IMPLIED>
   <!ATTLIST many-to-one property-ref CDATA #IMPLIED>
   <!ATTLIST many-to-one formula CDATA #IMPLIED>
   <!ATTLIST many-to-one lazy (true|false|proxy) #IMPLIED>
   <!ATTLIST many-to-one not-found (exception|ignore) "exception">
   <!ATTLIST many-to-one node CDATA #IMPLIED>
   <!ATTLIST many-to-one embed-xml (true|false) "true">


any idea?

Etienne.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 6:55 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
I haven't had that problem, and I'm using the same version of Hibernate (3.0.5). Sorry I can't help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 9:08 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Hi,

it works well, thanks a lot. I dont know about the xml validation, but it does'nt matter for now.

Thanks again!

Etienne.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 9:24 am 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
You may want to check to make sure all the libraries for hibernate and its dependencies are the correct versions.


Top
 Profile  
 
 Post subject: ObjectNotFoundExcepton -Solution using Hibernate Annotations
PostPosted: Thu May 24, 2007 8:20 am 
Newbie

Joined: Thu Apr 12, 2007 12:34 pm
Posts: 4
I was getting a similar problem : Dirty Data (correctly) causing a hibernate Exception. This was on an existing system, so 'cleaning up' the data was not an option.

This threw an ObjectNotFoundException when calling Job.getSalary , where the salaryId was -1 (marking it as invalid , as there was not an entry on the Salary table with that ID)

The solution (same as above, but using annotations) is to add a Hibernate annotation to the get method
@NotFound(action= NotFoundAction.IGNORE)

This causes the Salary object to be returned as Null, which can be handled as usual in your code.

Paul
http://www.firstpartners.net/blog


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.