-->
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.  [ 5 posts ] 
Author Message
 Post subject: about session.get and session.load method
PostPosted: Thu Nov 17, 2005 4:21 am 
Newbie

Joined: Tue Nov 15, 2005 8:00 am
Posts: 5
Location: china
my hibernate version is: 3.1.rc1

Hibernate version:
Hibernate-Version: 3.1.rc1

Code between sessionFactory.openSession() and session.close():
I use follow codes;

Session s = HibernateUtil.currentSession();

Transaction tx = s.beginTransaction();
//Person p = (Person) s.load(Person.class, new Integer(1)); // (1)
Person p = (Person) s.get(Person.class, new Integer(1));
tx.commit(); //why ?
System.out.println(p.getName());

HibernateUtil.closeSession();


i can use the method s.get() and there gos well
but if i use the method s.load and there will have something is wrong!
the full stack trace as fellows;
Full stack trace of any exception that occurs:
16:11:19,984 INFO SessionFactoryImpl:432 - Checking 0 named queries
16:11:20,093 ERROR LazyInitializationException:19 - could not initialize proxy - the owning Session is disconnected
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session is disconnected
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:59)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:133)
at test.hibernate.Person$$EnhancerByCGLIB$$775d807d.getName(<generated>)
at test.hibernate.Test.main(Test.java:34)

who can tell me why can use s.get() but can not use s.load().

by the way.
my .cfg.xml file as follows
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">329</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.transaction.auto_close_session">false</property>

<mapping resource="test/hibernate/Person.hbm.xml"/>

</session-factory>
</hibernate-configuration>

_________________
Hibernate is good


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 1:46 pm 
Regular
Regular

Joined: Mon Jun 14, 2004 1:42 pm
Posts: 80
Location: Brazil
That's becouse load is only to objects that you already got:

Quote:
load

Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists.

You should not use this method to determine if an instance exists (use get() instead). Use this only to retrieve an instance that you assume exists, where non-existence would be an actual error.



Quote:
get

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.)


[/quote]

_________________
Alexandre Torres
--------------------


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 1:48 pm 
Regular
Regular

Joined: Mon Jun 14, 2004 1:42 pm
Posts: 80
Location: Brazil
with "got" I mean already loaded /in session. load does not issue sql queries.

_________________
Alexandre Torres
--------------------


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 5:07 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
atorres wrote:
with "got" I mean already loaded /in session. load does not issue sql queries.


not true.

ice - it appears as though the session is closing right after the transaction has been committed. Make sure you do not actually have the application configured to do this - and just out of curiosity, what happens when both transaction method lines are commented out?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 6:33 am 
Regular
Regular

Joined: Mon Jun 14, 2004 1:42 pm
Posts: 80
Location: Brazil
Yes, Dennis is right. But let me explain better.
Load method does issue SQLs but it returns Proxy instances of your object.
It means that the attributes will not be loaded from database to your object when you issue load, just when you acess the attribute with get method.
Your session should be seted to close when closing transaction. After you close the session you can't fire an acessor method in a unloaded proxy, becouse it needs a session to load itself.
When you get an object it's ready to be disconnected from session. At least if it don't try to access lazy collections.

_________________
Alexandre Torres
--------------------


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