-->
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: How to disable autoload?
PostPosted: Sun Mar 15, 2009 8:16 am 
Newbie

Joined: Thu Sep 16, 2004 6:03 am
Posts: 15
Location: Moscow, Russia
Hello, I have a little problem. I use Spring and Hibernate. When I try to set a property, Hibernate try to SELECT from database. But it's no good becouse I can't set property without open session. For example:

Hibernate version:3.3.1

Mapping documents:
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>
    <class name="ru.gubber.partnership.model.SystemProperties" table="SYSTEM_PROPERTIES">
        <id name="propIdent" column="PROPERTY_IDENT">
            <generator class="assigned"/>
        </id>

        <property name="propValue" column="PROPERTY_VALUE" />
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
                logger.debug("1");
                SystemProperties props = (SystemProperties) session.load(SystemProperties.class, propIdent);
                logger.debug("2");
                if (props == null) props = new SystemProperties(propIdent);
                logger.debug("3");
                props.setPropValue(propValue);
                logger.debug("4");
                session.saveOrUpdate(propValue);
                logger.debug("5");


logtrace:
14:46:45,375 DEBUG ru.gubber.partnership.dao.PropertiesDAO :36 - 1
14:46:45,375 DEBUG ru.gubber.partnership.dao.PropertiesDAO :38 - 2
14:46:45,375 DEBUG ru.gubber.partnership.dao.PropertiesDAO :40 - 3
Hibernate: select systemprop0_.PROPERTY_IDENT as PROPERTY1_14_0_, systemprop0_.PROPERTY_VALUE as PROPERTY2_14_0_ from SYSTEM_PROPERTIES systemprop0_ where systemprop0_.PROPERTY_IDENT=?
[/code]
What should I do to disable auto-load?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 7:06 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Well, as soon as you access a property a proxy becomes initialized and there is know way to avoid it, as far as i know.

If the problem is, that you change the property outside a session, you have to use session.get instead of session.load:
Code:
SystemProperties props = (SystemProperties) session.get(SystemProperties.class, propIdent);

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 23, 2009 8:55 am 
Newbie

Joined: Thu Sep 16, 2004 6:03 am
Posts: 15
Location: Moscow, Russia
mmerder wrote:
Well, as soon as you access a property a proxy becomes initialized and there is know way to avoid it, as far as i know.

If the problem is, that you change the property outside a session, you have to use session.get instead of session.load:
Code:
SystemProperties props = (SystemProperties) session.get(SystemProperties.class, propIdent);

I can't do session.get becouse i want to create new instance of SystemProperties and in that moment I haven't any records in DB.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 23, 2009 3:18 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
I can't do session.get becouse i want to create new instance of SystemProperties and in that moment I haven't any records in DB.


Then you should of course use neither Session.get() nor Session.load(). Instead you should simply do:

Code:
SystemProperties props = new SystemProperties();


and when you are done setting all properties:

Code:
session.save(props);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 23, 2009 3:26 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Hmmm... maybe I should have read the code example from the first post more carefully. It should work as expected if you replace session.load() with session.get().

Session.get() returns null if an entity doesn't exists in the database, but session.load() simply assumes that an instance exists (it doesn't check!) and returns an uninitialized proxy.


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.