-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: hibernate and spring
PostPosted: Thu Mar 04, 2004 1:45 pm 
Newbie

Joined: Thu Mar 04, 2004 11:37 am
Posts: 9
hi all,
i try to use the spring framework to handle hibernate's session stuff.
i use hibernate 2.1.2, postgresql 7.2.1 beta (windows port), and tomcat 4.1.24.
when tomcat start and initialize my webapp, all is fine:
=> no exceptions are thrown.
but, when i want to use hibernate, with this first line:
Code:
session = SessionFactoryUtils.getSession(getSessionFactory(),false);

an illegalstateexception is thrown.
by the way, the reason is the sessionfactory is null.
i have configured my web.xml with the right listener to start spring:
Code:
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>


does spring initialize itself the sessionFactory?
what is the mistake?

my applicationContext.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>
   
                   <!--         DATASOURCES DEFINITIONS                -->
  <!-- forum Datasource Factory definition -->
  <bean id="forumDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
      <value>java:comp/env/jdbc/postgres</value>
    </property>
  </bean>
  <!-- registration Datasource Factory definition --> 
<bean id="regDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
      <value>java:comp/env/jdbc/postgres</value>
    </property>
  </bean>
 
 
                   <!--          SESSION FACTORIES DEFINITIONS           -->


  <!-- forum Session Factory definition -->
  <bean id="forumSessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    <property name="mappingResources">
      <list>
        <value>net/sf/jboard/model/forum/dto/ForumCategory.hbm.xml</value>
        <value>net/sf/jboard/model/forum/dto/ForumItem.hbm.xml</value>
        <value>net/sf/jboard/model/forum/dto/ForumThread.hbm.xml</value>
        <value>net/sf/jboard/model/forum/dto/ForumMessage.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">net.sf.hibernate.dialect.PostgreSQLDialect</prop>
        <prop key="hibernate.jdbc.batch_size">0</prop>       
      </props>
    </property>
    <property name="dataSource">
      <ref bean="forumDataSource"/>
    </property>
  </bean>
  <!-- registration Session Factory definition -->
  <bean id="regSessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    <property name="mappingResources">
      <list>
        <value>net/sf/jboard/model/reg/dto/User.hbm.xml</value>
        <value>net/sf/jboard/model/reg/dto/Role.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">net.sf.hibernate.dialect.PostgreSQLDialect</prop>
        <prop key="hibernate.jdbc.batch_size">0</prop>
      </props>
    </property>
    <property name="dataSource">
      <ref bean="regDataSource"/>
    </property>
  </bean>

                      <!--            AOP INTERCEPTORS                       -->
   
   <!-- forum interceptor -->
   <bean id="myForumHibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
    <property name="sessionFactory">
      <ref bean="forumSessionFactory"/>
    </property>
  </bean>

  <bean id="myForumDaoTarget" class="net.sf.jboard.model.forum.dao.ForumHibernateDAO">
    <property name="sessionFactory">
      <ref bean="forumSessionFactory"/>
    </property>
  </bean>

  <bean id="myForumDao" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
      <value>net.sf.jboard.model.forum.dao.ForumDAOInterface</value>
    </property>
    <property name="interceptorNames">
      <list>
        <value>myForumHibernateInterceptor</value>
        <value>myForumDaoTarget</value>
      </list>
    </property>
  </bean>
 
 
   <!-- registration interceptor --> 
  <bean id="myRegHibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
    <property name="sessionFactory">
      <ref bean="regSessionFactory"/>
    </property>
  </bean>

  <bean id="myRegDaoTarget" class="net.sf.jboard.model.reg.dao.RegHibernateDAO">
    <property name="sessionFactory">
      <ref bean="regSessionFactory"/>
    </property>
  </bean>

  <bean id="myRegDao" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
      <value>net.sf.jboard.model.reg.dao.RegDAOInterface</value>
    </property>
    <property name="interceptorNames">
      <list>
        <value>myRegHibernateInterceptor</value>
        <value>myRegDaoTarget</value>
      </list>
    </property>
  </bean>
</beans>


and my DAO method called:

Code:
/**
     * checks if a user with the same username already exists.
     * @param User
     * @return result. true if a user with the same name exists, false otherwise.
     */
    public List userAlreadyExists(User user) throws DAOException {
       
       try{
       
       session = SessionFactoryUtils.getSession(getSessionFactory(),false);
       
       }catch(DataAccessResourceFailureException darfe){
           logger.error("erreur spring = "+darfe.getMessage());
           throw new DAOException(darfe.getMessage());
       }catch(IllegalStateException ise){
           logger.error("erreur spring = "+ise.getMessage());
           throw new DAOException(ise.getMessage());   
       }
       
       int rowNumber = 0;
       List list;
    try {
      
        Query q = session.createQuery("select user.id from net.sf.jboard.model.reg.dto.User as user " +
            "where user.username=:username and user.password=:password ");
        q.setParameter("username",user.getUsername());
        q.setParameter("password",user.getPassword());
       
        list = q.list();
   } catch (HibernateException e) {
     throw new DAOException(e.getMessage());
    }
       
        return list;
    }


you can note that i have configured two DAO which request to the same datasource (one of them will refers to another datasource later).


if someone has got an experience of Spring..........

sincerly yours,
charles.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 2:26 pm 
Newbie

Joined: Thu Mar 04, 2004 11:37 am
Posts: 9
just a precision.
here my trace at startup:
Code:
INFO [main] (Environment.java:462) - Hibernate 2.1.2
INFO [main] (Environment.java:491) - hibernate.properties not found
INFO [main] (Environment.java:519) - using CGLIB reflection optimizer
INFO [main] (Binder.java:229) - Mapping class: net.sf.jboard.model.forum.dto.ForumCategory -> forumcategory
INFO [main] (Binder.java:229) - Mapping class: net.sf.jboard.model.forum.dto.ForumItem -> forumitem
INFO [main] (Binder.java:229) - Mapping class: net.sf.jboard.model.forum.dto.ForumThread -> forumthread
INFO [main] (Binder.java:229) - Mapping class: net.sf.jboard.model.forum.dto.ForumMessage -> forummessage
INFO [main] (Configuration.java:595) - processing one-to-many association mappings
INFO [main] (Binder.java:1154) - Mapping collection: net.sf.jboard.model.forum.dto.ForumCategory.forumItems -> forumitem
INFO [main] (Binder.java:1154) - Mapping collection: net.sf.jboard.model.forum.dto.ForumItem.forumThreads -> forumthread
INFO [main] (Binder.java:1154) - Mapping collection: net.sf.jboard.model.forum.dto.ForumThread.forumMessages -> forummessage
INFO [main] (Configuration.java:604) - processing one-to-one association property references
INFO [main] (Configuration.java:629) - processing foreign key constraints
INFO [main] (Dialect.java:82) - Using dialect: net.sf.hibernate.dialect.PostgreSQLDialect
INFO [main] (SettingsFactory.java:62) - Use outer join fetching: true
INFO [main] (ConnectionProviderFactory.java:53) - Initializing connection provider: org.springframework.orm.hibernate.LocalDataSourceConnectionProvider
INFO [main] (TransactionManagerLookupFactory.java:33) - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
AbandonedObjectPool is used (org.apache.commons.dbcp.AbandonedObjectPool@178efd8)
   LogAbandoned: true
   RemoveAbandoned: true
   RemoveAbandonedTimeout: 40
INFO [main] (SettingsFactory.java:102) - Use scrollable result sets: true
INFO [main] (SettingsFactory.java:105) - Use JDBC3 getGeneratedKeys(): false
INFO [main] (SettingsFactory.java:108) - Optimize cache for minimal puts: false
INFO [main] (SettingsFactory.java:117) - Query language substitutions: {}
INFO [main] (SettingsFactory.java:128) - cache provider: net.sf.ehcache.hibernate.Provider
INFO [main] (Configuration.java:1080) - instantiating and configuring caches
INFO [main] (SessionFactoryImpl.java:119) - building session factory
INFO [main] (SessionFactoryObjectFactory.java:82) - no JNDI name configured
INFO [main] (Binder.java:229) - Mapping class: net.sf.jboard.model.reg.dto.User -> usertable
INFO [main] (Binder.java:547) - Mapping collection: net.sf.jboard.model.reg.dto.User.roles -> user_role
INFO [main] (Binder.java:229) - Mapping class: net.sf.jboard.model.reg.dto.Role -> role
INFO [main] (Binder.java:547) - Mapping collection: net.sf.jboard.model.reg.dto.Role.users -> user_role
INFO [main] (Configuration.java:595) - processing one-to-many association mappings
INFO [main] (Configuration.java:604) - processing one-to-one association property references
INFO [main] (Configuration.java:629) - processing foreign key constraints
INFO [main] (Dialect.java:82) - Using dialect: net.sf.hibernate.dialect.PostgreSQLDialect
INFO [main] (SettingsFactory.java:62) - Use outer join fetching: true
INFO [main] (ConnectionProviderFactory.java:53) - Initializing connection provider: org.springframework.orm.hibernate.LocalDataSourceConnectionProvider
INFO [main] (TransactionManagerLookupFactory.java:33) - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
INFO [main] (SettingsFactory.java:102) - Use scrollable result sets: true
INFO [main] (SettingsFactory.java:105) - Use JDBC3 getGeneratedKeys(): false
INFO [main] (SettingsFactory.java:108) - Optimize cache for minimal puts: false
INFO [main] (SettingsFactory.java:117) - Query language substitutions: {}
INFO [main] (SettingsFactory.java:128) - cache provider: net.sf.ehcache.hibernate.Provider
INFO [main] (Configuration.java:1080) - instantiating and configuring caches
INFO [main] (SessionFactoryImpl.java:119) - building session factory
INFO [main] (SessionFactoryObjectFactory.java:82) - no JNDI name configured
StandardWrapper[/jboard:default]: Chargement du conteneur (container) de servlet default
refreshTime=3500
log4jConfigFileName=log4j.properties


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 6:04 pm 
Regular
Regular

Joined: Fri Jan 16, 2004 4:48 am
Posts: 56
Do you have the set/getSessionFactory def in your

net.sf.jboard.model.reg.dao.RegDAOInterface target proxy ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 6:14 pm 
Regular
Regular

Joined: Fri Jan 16, 2004 4:48 am
Posts: 56
Also, can you post your full trace, including the exception that is thrown


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 1:13 pm 
Newbie

Joined: Thu Mar 04, 2004 11:37 am
Posts: 9
shishirksingh wrote:
Also, can you post your full trace, including the exception that is thrown


the trace of my exception:
java.lang.IllegalStateException: Not allowed to create new Hibernate session

i've tried to create a sessionFactory getter/setter in my regDAOInterface, but this problem is here again.
and it's a non-sense to put these accessors in the interface, because they refers to a sessionFactory which is hibernate-dependant, isn't it?
=>the dao interface must be independant of which access medium you use.

so, i don't know again how the spring framework instantiate the hibernate SessionFactory......


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 1:26 pm 
Newbie

Joined: Thu Mar 04, 2004 11:37 am
Posts: 9
notice that i have put sessionFactory and session getter/setter in the implementation of the interface called regDAOInterface.
so, my problem persists......

:-(


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 1:47 pm 
Newbie

Joined: Thu Mar 04, 2004 11:37 am
Posts: 9
i've corrected my daoImplementation and now it inherits from HibernateDAOSupport, which have the suitable methods to handling sessionFactory.
but the exception is always here.....:
java.lang.NullPointerException

in fact, in mey initialization traces, i see the SessionFactoryImpl initializes:
Code:
INFO [main] (ConnectionProviderFactory.java:53) - Initializing connection provider: org.springframework.orm.hibernate.LocalDataSourceConnectionProvider
INFO [main] (TransactionManagerLookupFactory.java:33) - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
INFO [main] (SettingsFactory.java:102) - Use scrollable result sets: true
INFO [main] (SettingsFactory.java:105) - Use JDBC3 getGeneratedKeys(): false
INFO [main] (SettingsFactory.java:108) - Optimize cache for minimal puts: false
INFO [main] (SettingsFactory.java:117) - Query language substitutions: {}
INFO [main] (SettingsFactory.java:128) - cache provider: net.sf.ehcache.hibernate.Provider
INFO [main] (Configuration.java:1080) - instantiating and configuring caches
INFO [main] (SessionFactoryImpl.java:119) - building session factory
INFO [main] (SessionFactoryObjectFactory.java:82) - no JNDI name configured


but i suggest the Spring can't reach the SessionFactory.....
very strange....

can someone light my sight with this problem?

thanks in advance!
charles.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 06, 2004 7:56 am 
Newbie

Joined: Thu Mar 04, 2004 11:37 am
Posts: 9
some precisions:
there is probably, a problem with the session initialization:
i've tried to use the OpenSessionInViewFilter provided with Spring.
it is also unsuccessful.......
this filter doesn't throw any exception, but the getSessionFactory with SessionFactoryUtils.getSession method, return always a nullPointerException......

any ideas?
to resume:
does someone have successfully configured Spring with an AOP interceptor to handle hibernateSession stuff?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 06, 2004 11:24 am 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
Are you sure that the datasource is correctly configured? If your sessionFactory configuration fails because it couldn't find the JNDI name it probably means there's an error in the dataSource definition. You have to configure your dataSource in tomcat's server.xml as well as having a reference to that datasource in your web.xml.

About the OpenSessionInViewFilter, I noticed that it by default searches for a sessionFactory bean called "sessionFactory". Given that neither of your two sessionFactories are called that, you need to tell the filter to use another sessionFactory in some way. Probably in web.xml, but I don't really know how. You might also need two filters, one for each sessionFactory.

IMHO, it sounds like overkill to have two dataSources, considering that they both point to the same database and your app isn't that huge. I think your life would be simplified if you'd just combine your two sessionfactories into one. Later, if there really is a need, you can divide them again without touching any application code, only by editing the spring config file.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 5:31 pm 
Newbie

Joined: Thu Mar 04, 2004 11:37 am
Posts: 9
joib wrote:
Are you sure that the datasource is correctly configured? If your sessionFactory configuration fails because it couldn't find the JNDI name it probably means there's an error in the dataSource definition. You have to configure your dataSource in tomcat's server.xml as well as having a reference to that datasource in your web.xml.

About the OpenSessionInViewFilter, I noticed that it by default searches for a sessionFactory bean called "sessionFactory". Given that neither of your two sessionFactories are called that, you need to tell the filter to use another sessionFactory in some way. Probably in web.xml, but I don't really know how. You might also need two filters, one for each sessionFactory.

IMHO, it sounds like overkill to have two dataSources, considering that they both point to the same database and your app isn't that huge. I think your life would be simplified if you'd just combine your two sessionfactories into one. Later, if there really is a need, you can divide them again without touching any application code, only by editing the spring config file.

the problem seems to come from the read-only jndi tomcat feature.....
it can't associate a sessionFactory in jndi (like do spring)....
so i should code a repositoryvia static method.....(tips is published on hibernate website).

thanks for help!

charles.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 11:54 am 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
I'm also facing issues with AOP Interceptor in trying to get a HibernateSession.

I've got a BaseDaoHibernate class:
Code:
public class BaseDAOHibernate extends HibernateDaoSupport implements BaseDAO {


I'm using getHibernateTemplate() for most methods, I only need an AOP Interceptor to use the createQuery() method:

Code:
    public final List getLimitedList(Class clazz, String sql, int pageSize, int pageResults) throws DAOException {
        List ret = null;
        Session session = null;

        try {
            session = SessionFactoryUtils.getSession(getSessionFactory(), false);

        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new DAOException(e.getMessage());
         }

        try {
            Query q = getHibernateTemplate().createQuery(session, "from clazz in " + clazz + sql);
            q.setMaxResults(pageSize);
            q.setFirstResult(pageResults * pageSize);
            ret = q.list();
        } catch (HibernateException e) {
            throw new DAOException(e.getMessage());
        }
        return ret;
    }


My applicationContext look like this:
Code:
    <!-- base interceptor -->
    <bean id="baseHibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>

    <bean id="baseDaoHibernate" class="org.xx.persistence.BaseDAOHibernate">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>

    <bean id="baseDao" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces">
            <value>org.xx.persistence.BaseDAO</value>
        </property>
        <property name="interceptorNames">
            <list>
                <value>baseHibernateInterceptor</value>
                <value>baseDaoHibernate</value>
            </list>
        </property>
    </bean>


I got a NullPointerException when requesting getSessionFactory() from HibernateDaoSupport, hibernateTemplate being null.

??????

I don't understand it at all, I even start doubting it is possible to mix both style, i-e extending HibernateDaoSupport and using AOP within the same class.... I hope it is though, it must be!

I figured out I should probably implement SessionFactory getter/setter in my BaseDAO interface but there' no way sine it overrides HibernateDaoSupport ones...

i need your help guys! don't let me down ;-)
cheers


Last edited by nodje on Tue Jun 03, 2008 5:18 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 4:24 pm 
Newbie

Joined: Thu Mar 04, 2004 11:37 am
Posts: 9
if your DAO extends HibernateDaoSupport , sessionFactory getter/setter are already provided.

with my problem, it seems that tomcat is in charge (in fact me, who doesn't know exactly the tomcat features...).

tomcat appears to have a jndi support only in read-only mode.

so, IMHO, Spring cannot override JNDI to declare the sessionFactory....
and you get later, an nullPointerException like me, when you want to resolve the JNDI sessionFactory reference.........


this assertion only rules if your application server is tomcat, or another JNDI read-only application server.......

note that you can declare a JNDI reference in the server.xml file:
not helpful for Spring.....

hope this helps,
charles.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 6:05 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:09 pm
Posts: 58
I've been using hibernate+spring in tomcat for a while now with no problems.

I don't really think the problem has anything to do with JNDI. I'm not sure why you think that. You've configured the session factory as a bean in your spring application context, so it will load when the context loads. Spring is creating the session factory via the LocalSessionFactoryBean, not trying to do a JNDI lookup for it.

You have this line above:
Code:
session = SessionFactoryUtils.getSession(getSessionFactory(),false);


Are you calling this line yourself somewhere? Double check the getSessionFactory() call. Where is that function supposed to get the session factory from?

Also, the javadocs for SessionFactoryUtils.getSession states it will throw an IllegalStateException if there is no current thread-bound Session and allowCreate is false. You are passing false, so it expects that a session is already available.

Hope this helps...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 6:11 pm 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
nodje: As jhoeller said in another recent thread, HibernateTemplate.createQuery(...) is supposed to be used when using callbacks. OTOH, looking at the code (hey, it's open source, you can look too!), I don't see why it wouldn't work when using interceptors.

Anyway, my personal opinion is that callbacks are way more convenient than the interceptors. Ok, so you can't use checked exceptions in the callback code, but that's a small problem. If you want, you can wrap the callback block in a try-catch and then throw any exception you like. The big benefit is that you don't need to configure anything to use callbacks, and the code itself is shorter too!

diabolo: I wouldn't worry about that JNDI stuff. There is no need for spring to save the sessionFactory in JNDI. For a "typical" spring-hibernate-tomcat webapp, the only thing you need JNDI for is to get the JDBC datasource. That message in the log file about "no JNDI name configured" is just an informational message, it works just fine without it.

BTW, one of the really fantastic side-effects of using spring is that it's super-easy to set up a test environment where you can test your DB logic without tomcat. I.e. split the datasource definition from applicationcontext.xml into a separate file (say, dataSource-jndi.xml). For your test environment you then use another datasource definition (say, dataSource-local.xml), where you initialize the datasource without jndi (see the samples included with spring for examples on how to configure this). Total effort to initialize the context is about 5 lines in the setUp() method of your Junit TestCase (which you of course can avoid duplicating by extending this class for the actual testcases where you want to use the application context).

On a totally unrelated topic, while Java is far from my favourite language (my personal favourites being python for general stuff and C for high performance stuff), spring and hibernate are still the kind of stuff that make me jump with joy! ;-) Truly well designed and implemented stuff!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 5:24 am 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
I've absolutly no problem with JNDI, since i'm not using it at all. I don't need it, I just have a LocalSessionFactoryBean. It works great and avoids me lot of troubles.

Getter/setter are effectively already provided, so I shouldn't need them.

I've been looking at the code (especially HibernateDaoSupport) and in fact the get/set for SessionFactory would probably work if hibernateTemplate weren't null:

it crashes exactly here, since hibernateTemplate is null
Code:
   protected final SessionFactory getSessionFactory() {
      return hibernateTemplate.getSessionFactory();
   }


I would investigate further if all methods from my class BaseDAOHibernate were gving me this NPE.
But everythings fine except this one (the only one using AOP Interceptor OR CallBacks):
for instance:
Code:
    public final void removeObject(final Class clazz, final Serializable id) throws DAOException {
        try {
            getHibernateTemplate().delete(this.retrieveObject(clazz, id));
        } catch (Exception e) {
            logger.warn(e.getMessage());
            throw new DAOException(e);
        }
    }

works perfectly

Here's another version of the same method using CallBacks:
Code:
    public final List getLimitedList(final Class clazz, final String sql, final int pageSize, final int pageResults) throws DAOException {
        List ret = null;
        Session session = null;

        final String queryString = "from clazz in " + clazz + sql;
        return getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                Query query = session.createQuery(queryString);
                query.setMaxResults(pageSize);
                query.setFirstResult(pageResults * pageSize);
                return query.list();
            }
        });
   }

It crashes. I must say I don't really get what so different in the way HibernateDaoSupport is being used by CallBacks/Interceptor way and the std way.

I'll anyway have to understand the whole stuff if I can't make it to work since i relly need that createQuery method.

But I'm sure I shouldn't get this NPE here. I most probably miss something here.

I'll try a
Code:
session = SessionFactoryUtils.getSession(getSessionFactory(), true);
but i'm not really confident since the true/false just mean reusing/creating session. IMO it's not gonna create the hibernateTemplate...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.