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!!