-->
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: Hibernate problem
PostPosted: Tue Feb 24, 2009 4:24 am 
Newbie

Joined: Sun Feb 08, 2009 10:41 am
Posts: 13
Hi, constantly I have a problem of throwing LazyInitializationException.

Either if I use a Spring or low-level comunication with database the mentioned exception is thrown whenever
I try to retrieve an object using -- load(Class klass, id) --.
If I try to retrieve using createQuery(...) method it's than ok, no exception occurs.
Here is the portion of code where I try to retrieve an object using Spring integration :

Code:
package boby;

import org.apache.commons.logging.*;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*;

import org.springframework.orm.hibernate3.*;
import org.springframework.context.support.*;

public class SpringEvent {

private static Log log = LogFactory.getLog(SpringEvent.class);
public static void main(String[] args) {

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

ImprovedEventDao eventDao = (ImprovedEventDao) ctx.getBean("eventDao", ImprovedEventDao.class);
Event event = new Event();
event.setName("TestUnit");
eventDao.create(event);

//Event obj = (Event) result.get(0);

Event obj = (Event) eventDao.findE(event.getId()); // addition, which requre Dao changing

log.warn("Using improved Dao-- first event : " +event.getName());
log.warn("Using improved Dao-- second event  : " +obj.getName());
eventDao.deleteD(event);
eventDao.deleteD(obj);
}
}


Where ImprovedEventDao extends abstract class AbstractSpringDao, which extends HibernateDaoSupport :

Code:
package boby;

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class AbstractSpringDao
extends HibernateDaoSupport{

public AbstractSpringDao() { }

protected void saveOrUpdate(Object obj) {
getHibernateTemplate().saveOrUpdate(obj);
}

protected void delete(Object obj) {
getHibernateTemplate().delete(obj);
}

protected Object find(Class clazz, Long id) {
return getHibernateTemplate().load(clazz, id);
}

protected List findAll(Class clazz) {
return getHibernateTemplate().find(
"from " + clazz.getName());
}
}


In low level db comunication, my method for storing and retrieving object is :



Code:

public void create(Event event)       
throws DataAccessLayerException {
try {
startOperation();
session.save(event);
session.flush();
tx.commit();

} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
}


public Event find(Long id) throws DataAccessLayerException { 
Event event = null;
try {
startOperation();
event = (Event) session.load(Event.class, id);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
log.warn("e.getMessage()");
handleException(e);
log.warn("Proba iz catcha");
} finally {
HibernateFactory.close(session);
}
return event;
}



where startoperation() mmethond perform obtaining SessionFactory, Sesion and Transaction. Also I get the mentioned
Exception. What is the problem? If I not close session after inserting data, and not commit second startoperation() and
commit() method, I do not get an exception, neither if I use createQuery() method.. But then, two distinct operation occurs during the same session!?
If I use Spring, there is no choice, and each operation will occurs in context of particular Session and Transaction.
so what is the problem!?!?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 5:03 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Can u post the mapping and the stack trace?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 6:05 am 
Newbie

Joined: Sun Feb 08, 2009 10:41 am
Posts: 13
here is the content of applocationContext.xml file:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/events_calendar</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>slobodan</value>
</property>
</bean>
<bean id="factory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>boby/Event.hbm.xml</value>
<value>boby/Location.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="eventDao"
class="boby.ImprovedEventDao">
<property name="sessionFactory">
<ref bean="factory" />
</property>
</bean>
</beans>



This is the content of Event.hbm.xml :

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="boby.Event"
        table="events"
    >

        <id
            name="id"
            column="uid"
            type="java.lang.Long"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Event.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="name"
                sql-type="TEXT"
            />
        </property>

        <property
            name="startDate"
            type="java.util.Date"
            update="true"
            insert="true"
            column="start_date"
        />

        <property
            name="duration"
            type="int"
            update="true"
            insert="true"
            column="duration"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Event.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


and Location.hbm.xml :

Code:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="boby.Location"
        table="locations"
    >

        <id
            name="id"
            column="uid"
            type="java.lang.Long"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Location.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

        <component
            name="address"
            class="boby.Address"
        >
        <property
            name="streetAddress"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="street_address"
            />
        </property>

        <property
            name="city"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="city"
            />
        </property>

        <property
            name="state"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="state"
            />
        </property>

        <property
            name="zipCode"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="zip_code"
            />
        </property>

        </component>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Location.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>




The stack trace is quite large, I'll supply to you just important part:

Code:


<hibernate.LazyInitializationException 19 ) could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
org.hibernate.proxy.AbstractLazyInitializer.initia lize(AbstractLazyInitializer.java:57)
org.hibernate.proxy.AbstractLazyInitializer.getImp lementation(AbstractLazyInitializer.java:111)
org.hibernate.proxy.pojo.cglib.CGLIBLazyInitialize r.invoke(CGLIBLazyInitializer.java:150)
.
.
.
.

and so on trace...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 6:06 am 
Newbie

Joined: Sun Feb 08, 2009 10:41 am
Posts: 13
here is the content of applocationContext.xml file:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/events_calendar</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>slobodan</value>
</property>
</bean>
<bean id="factory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>boby/Event.hbm.xml</value>
<value>boby/Location.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="eventDao"
class="boby.ImprovedEventDao">
<property name="sessionFactory">
<ref bean="factory" />
</property>
</bean>
</beans>



This is the content of Event.hbm.xml :

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="boby.Event"
        table="events"
    >

        <id
            name="id"
            column="uid"
            type="java.lang.Long"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Event.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="name"
                sql-type="TEXT"
            />
        </property>

        <property
            name="startDate"
            type="java.util.Date"
            update="true"
            insert="true"
            column="start_date"
        />

        <property
            name="duration"
            type="int"
            update="true"
            insert="true"
            column="duration"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Event.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


and Location.hbm.xml :

Code:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="boby.Location"
        table="locations"
    >

        <id
            name="id"
            column="uid"
            type="java.lang.Long"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Location.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

        <component
            name="address"
            class="boby.Address"
        >
        <property
            name="streetAddress"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="street_address"
            />
        </property>

        <property
            name="city"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="city"
            />
        </property>

        <property
            name="state"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="state"
            />
        </property>

        <property
            name="zipCode"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="zip_code"
            />
        </property>

        </component>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Location.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>




The stack trace is quite large, I'll supply to you just important part:

Code:


<hibernate.LazyInitializationException 19 ) could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
org.hibernate.proxy.AbstractLazyInitializer.initia lize(AbstractLazyInitializer.java:57)
org.hibernate.proxy.AbstractLazyInitializer.getImp lementation(AbstractLazyInitializer.java:111)
org.hibernate.proxy.pojo.cglib.CGLIBLazyInitialize r.invoke(CGLIBLazyInitializer.java:150)
.
.
.
.

and so on trace...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 6:07 am 
Newbie

Joined: Sun Feb 08, 2009 10:41 am
Posts: 13
here is the content of applocationContext.xml file:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/events_calendar</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>slobodan</value>
</property>
</bean>
<bean id="factory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>boby/Event.hbm.xml</value>
<value>boby/Location.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="eventDao"
class="boby.ImprovedEventDao">
<property name="sessionFactory">
<ref bean="factory" />
</property>
</bean>
</beans>



This is the content of Event.hbm.xml :

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="boby.Event"
        table="events"
    >

        <id
            name="id"
            column="uid"
            type="java.lang.Long"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Event.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="name"
                sql-type="TEXT"
            />
        </property>

        <property
            name="startDate"
            type="java.util.Date"
            update="true"
            insert="true"
            column="start_date"
        />

        <property
            name="duration"
            type="int"
            update="true"
            insert="true"
            column="duration"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Event.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


and Location.hbm.xml :

Code:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="boby.Location"
        table="locations"
    >

        <id
            name="id"
            column="uid"
            type="java.lang.Long"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Location.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

        <component
            name="address"
            class="boby.Address"
        >
        <property
            name="streetAddress"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="street_address"
            />
        </property>

        <property
            name="city"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="city"
            />
        </property>

        <property
            name="state"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="state"
            />
        </property>

        <property
            name="zipCode"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="zip_code"
            />
        </property>

        </component>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Location.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>




The stack trace is quite large, I'll supply to you just important part:

Code:


<hibernate.LazyInitializationException 19 ) could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
org.hibernate.proxy.AbstractLazyInitializer.initia lize(AbstractLazyInitializer.java:57)
org.hibernate.proxy.AbstractLazyInitializer.getImp lementation(AbstractLazyInitializer.java:111)
org.hibernate.proxy.pojo.cglib.CGLIBLazyInitialize r.invoke(CGLIBLazyInitializer.java:150)
.
.
.
.

and so on trace...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 6:08 am 
Newbie

Joined: Sun Feb 08, 2009 10:41 am
Posts: 13
here is the content of applocationContext.xml file:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/events_calendar</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>slobodan</value>
</property>
</bean>
<bean id="factory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>boby/Event.hbm.xml</value>
<value>boby/Location.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="eventDao"
class="boby.ImprovedEventDao">
<property name="sessionFactory">
<ref bean="factory" />
</property>
</bean>
</beans>



This is the content of Event.hbm.xml :

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="boby.Event"
        table="events"
    >

        <id
            name="id"
            column="uid"
            type="java.lang.Long"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Event.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="name"
                sql-type="TEXT"
            />
        </property>

        <property
            name="startDate"
            type="java.util.Date"
            update="true"
            insert="true"
            column="start_date"
        />

        <property
            name="duration"
            type="int"
            update="true"
            insert="true"
            column="duration"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Event.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


and Location.hbm.xml :

Code:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="boby.Location"
        table="locations"
    >

        <id
            name="id"
            column="uid"
            type="java.lang.Long"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Location.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

        <component
            name="address"
            class="boby.Address"
        >
        <property
            name="streetAddress"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="street_address"
            />
        </property>

        <property
            name="city"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="city"
            />
        </property>

        <property
            name="state"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="state"
            />
        </property>

        <property
            name="zipCode"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="zip_code"
            />
        </property>

        </component>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Location.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>




The stack trace is quite large, I'll supply to you just important part:

Code:


<hibernate.LazyInitializationException 19 ) could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
org.hibernate.proxy.AbstractLazyInitializer.initia lize(AbstractLazyInitializer.java:57)
org.hibernate.proxy.AbstractLazyInitializer.getImp lementation(AbstractLazyInitializer.java:111)
org.hibernate.proxy.pojo.cglib.CGLIBLazyInitialize r.invoke(CGLIBLazyInitializer.java:150)
.
.
.
.

and so on trace...


Top
 Profile  
 
 Post subject: Re: Hibernate problem
PostPosted: Tue Feb 24, 2009 6:14 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 8:25 am
Posts: 46
Location: Saint Petersburg, Russian Federation
afrodom wrote:
...
where startoperation() mmethond perform obtaining SessionFactory, Sesion and Transaction. Also I get the mentioned
Exception. What is the problem? If I not close session after inserting data, and not commit second startoperation() and
commit() method, I do not get an exception, neither if I use createQuery() method.. But then, two distinct operation occurs during the same session!?
If I use Spring, there is no choice, and each operation will occurs in context of particular Session and Transaction.
so what is the problem!?!?


AbstractSpringDao.find() uses HibernateTemplate.load() that uses Session.load().

Session.load() does its best to use lazy-loading, i.e. no properties data are fetched from the database at the start. They are retrieved if necessary during the first request. I.e. when you call

Code:
Session session = ...;
MyClass c = session.load(MyClass.class, id);
c.getMyProperty();


'myProperty' property value is retrieved from the database only during c.getMyProperty() processing.

The simplest solution is to use HibernateTemplate.get() instead of HibernateTemplate.load() (Session.get() tries to load all necessary object data).

Feel free to read more about fetching strategies at the reference - 19.1. Fetching strategies


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 6:26 am 
Newbie

Joined: Sun Feb 08, 2009 10:41 am
Posts: 13
I've listened to you and refine
Code:
protected Object find(Class clazz, Long id) {
return getHibernateTemplate().get(clazz, id);
}
, so now, I use get instead of load.
The fetching occurs, but the second exception has been thrown - stacktrace :

Code:

org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
   at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:655)
   at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:378)
   at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:791)
   at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:787)
   at boby.AbstractSpringDao.delete(Unknown Source)
   at boby.ImprovedEventDao.deleteD(Unknown Source)
   at boby.SpringEvent.main(Unknown Source)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:217)
   at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:152)
   at org.apache.tools.ant.taskdefs.Java.run(Java.java:764)
   at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:218)
   at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:132)
   at org.apache.tools.ant.taskdefs.Java.execute(Java.java:105)
   at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
   at org.apache.tools.ant.Task.perform(Task.java:348)
   at org.apache.tools.ant.Target.execute(Target.java:357)
   at org.apache.tools.ant.Target.performTasks(Target.java:385)
   at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
   at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
   at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
   at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
   at org.apache.tools.ant.Main.runBuild(Main.java:758)
   at org.apache.tools.ant.Main.startAnt(Main.java:217)
   at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
   at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
   at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
   at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
   at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:146)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:374)
   ... 32 more



It's sort of previous, isn't it...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 6:33 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Both event and obj are pointing to the same row in DB.
Code:
eventDao.deleteD(event);
eventDao.deleteD(obj);

So the eventDao.deleteD has deleted that record and in the second line you are trying to delete that record again. Thats why you are getting this exception.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 6:46 am 
Newbie

Joined: Sun Feb 08, 2009 10:41 am
Posts: 13
Well, littypreethkr you really deserve this rating :)
It works. But tell me, again I try to use load(), and it throw again LazyInitializationException. Why?I read the article "Fetching strategies ", and I didn't find an explanations about such an occasion. So why load() method throw such an exception, and get() do not?
Thanks once again!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 6:53 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
See this:
http://jpa.ezhibernate.com/Javacode/lea ... bernateget

_________________
Regards,
Litty Preeth


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.