-->
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.  [ 4 posts ] 
Author Message
 Post subject: HibernateTemplate (NullPointerException)
PostPosted: Thu Dec 23, 2010 5:14 am 
Newbie

Joined: Thu Dec 23, 2010 4:51 am
Posts: 3
Location: Atlanta (USA)
Basically, I'm doing a very simple web application using Hibernate, Spring and Struts (using annotations).

I have created one table into my DataBase where I would like to put and request the information.

I have configured all the XML files like applicationContext.xml, hibernate.cfg.xml, hibernate.properties and also, I have programmed java classes.

I have this classes:

Alumno.java (this is an entity class)
AlumnoDao.java (this is an interface)
AlumnoDaoHibernate (this is the interface implementation)

As you can see in the code, I have problems with hibernateTemplation because it seems not initialized and I don't know why because when I use my test classes (JUNIT) it works nice! I only would like to get a list with the students but I can't, I get a NullPointerException at this line: return hibernateTemplate.loadAll(Alumno.class);

Code:
package escuela.dao.impl;

all the imports.....

@Repository("alumnoDaoHibernate")
@Transactional
public class AlumnoDaoHibernate implements AlumnoDao {
    private static final Log log = LogFactory.getLog(AlumnoDaoHibernate.class);

    private HibernateTemplate hibernateTemplate;
   
    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
       this.hibernateTemplate = new HibernateTemplate(sessionFactory);

    }

    @Transactional(readOnly=true)
    @Override
    public List<Alumno> lista() {
       
        log.debug("Buscando lista de alumnos con hibernate");
        [b]return hibernateTemplate.loadAll(Alumno.class);    [/b]     
    }

    @Transactional(readOnly=true)
    @Override
    public Alumno obtiene(Long id) {
        log.debug("Buscando alumno con hibernate");
        Alumno alumno = (Alumno)hibernateTemplate.get(Alumno.class, id);
        if (alumno == null) {
            throw new RuntimeException("No se encontro el alumno");
        }
        return alumno;
    }
}


I hope somebody can help me because I don't know what to do... :(

Also, this is my applicationContext.xml

Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/springhiber" />
        <property name="username" value="root" />
        <property name="password" value="samu@31" />
    </bean>

    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean>
   

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="dataSource" ref="dataSource"/>
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>


    <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven />


    <!-- Indicamos sobre que paquetes se hace el escaneo de las anotaciones cuando creemos los beans -->
    <context:component-scan base-package="escuela" />

</beans>



Thank you so much!!


Last edited by Samuel_CR on Thu Dec 23, 2010 6:20 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: HibernateTemplate (NullPointerException)
PostPosted: Thu Dec 23, 2010 6:00 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
obviously the @Autowired isn't working. that's a Spring issue, unless you have an Hibernate related exception to show.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: HibernateTemplate (NullPointerException)
PostPosted: Thu Dec 23, 2010 6:19 am 
Newbie

Joined: Thu Dec 23, 2010 4:51 am
Posts: 3
Location: Atlanta (USA)
s.grinovero wrote:
obviously the @Autowired isn't working. that's a Spring issue, unless you have an Hibernate related exception to show.



and... How can I resolve this issue with the annotation?

I have some JUNIT that are working good and they use AlumnoDaoHibernate.... so I don't understand nothing... :(

Anyway, the error is the following...



Code:
Messages:    
File:    escuela/dao/impl/AlumnoDaoHibernate.java
Line number:    70
Stacktraces
java.lang.NullPointerException

    escuela.dao.impl.AlumnoDaoHibernate.lista(AlumnoDaoHibernate.java:70)
    escuela.web.AlumnoAction.lista(AlumnoAction.java:41)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
...
...


Could be Tomcat the problem?

Thank you!


Top
 Profile  
 
 Post subject: Re: HibernateTemplate (NullPointerException)
PostPosted: Thu Dec 23, 2010 6:24 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
and... How can I resolve this issue with the annotation?

I have no clue, you should ask to Spring forums. While Spring is widely used, Hibernate is used even more without Spring and I don't know how Spring works.

_________________
Sanne
http://in.relation.to/


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