-->
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.  [ 7 posts ] 
Author Message
 Post subject: Load Id which don't exist
PostPosted: Fri May 16, 2008 9:14 am 
Beginner
Beginner

Joined: Fri Feb 29, 2008 9:36 am
Posts: 40
Hi,

When I code:

settings = session.Load(typeof(Settings), 1) as Settings;

If there's no Id with value 1, none error is thrown !

Is it a way to detect that there's a problem in load object ?

Thanks !

Papy (France)


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 9:40 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
Note that Load() will throw an unrecoverable exception if there is no matching database row. If the class is
mapped with a proxy, Load() returns an object that is an uninitialized proxy and does not actually hit the database
until you invoke a method of the object.


According to the docs, you should get an exception. Do you use lazy loading ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 9:58 am 
Beginner
Beginner

Joined: Fri Feb 29, 2008 9:36 am
Posts: 40
The real case is on user object which is linked to a profile table.

I use lazy=false in my hbm.xml

Code:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   default-lazy="false"
                   namespace="Cube_database.model"
                   assembly="Cube database">

there's also:

   <many-to-one name="_profile"  access="field.pascalcase-underscore" not-null="false" outer-join="auto" lazy="false" insert="true" update="true">
      <column name="id_profile"/>
    </many-to-one>



the profile loading throw a lazyinitialisationexception but the code to load user don't throw exception.

Code:
        internal static UsersEntity User(int Id, bool n, NHibernate.ISession session)
        {
            UsersEntity user = null;
            try
            {
                try
                {
                    user = session.Load(typeof(model.UsersEntity), Id) as model.UsersEntity;
                }
                catch (System.Exception)
                {
                    throw;
                }
            }
            finally
            {
                session.Close();
            }
            return user;
        }


The code jump from
user = session.Load
to
session.Close();


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 12:18 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
What do you mean with

Quote:
the profile loading throw a lazyinitialisationexception but the code to load user don't throw exception.


Quote:
The code jump from
user = session.Load
to
session.Close();


Isn't that the correct bahaviour for "finally" ? session.Close() should be executed before the exception is rethrown.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 19, 2008 3:52 am 
Beginner
Beginner

Joined: Fri Feb 29, 2008 9:36 am
Posts: 40
Quote:
the profile loading throw a lazyinitialisationexception but the code to load user don't throw exception.


I have added a line of code in catch

Code:
        catch (System.Exception)
        {
            user = null;
            throw;
        }


When I load the user, the code go in finally section and when I quit the function, my user is not null, so the line code "user = null" isn't executed.

But, if I try to use the user object after the function call, I get an ObjectNotFoundException.

I see that the user has several null values in properties (apart default values).
If I "spy" the profile property of the user, C# show me a lazyinitializationException.

To resolve the problem, I have added another line code:

model.ProfilesEntity profil = user._profile;

Code:
UsersEntity user = null;
try
{
    try
    {
        user = session.Load(typeof(model.UsersEntity), Id) as model.UsersEntity;
        model.ProfilesEntity profil = user._profile;
    }
    catch (NHibernate.ObjectNotFoundException)
    { user = null; }
    catch (System.Exception)
    { user = null; }
}
finally
{
    session.Close();
}
return user;


The of profile property throw the exception and my object is null after call the function.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 19, 2008 5:06 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
That souns like your user object gets lazily loaded despite of default-lazy="false". Have you tried to set lazy="false" on the class ?

Btw. I found this comment in the source code:

Quote:
Thrown when ISession.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 ISession.Get() to test if a row exists in the database.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 19, 2008 5:54 am 
Beginner
Beginner

Joined: Fri Feb 29, 2008 9:36 am
Posts: 40
Quote:
That souns like your user object gets lazily loaded despite of default-lazy="false". Have you tried to set lazy="false" on the class ?



Yes ! with lazy=false, it work properly. The ObjectNotFoundException is thrown.

Thanks a lot ![/code]


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