0
votes

I have this maven project with its modules

Parent

|_____Model

|_____Persistence

     |_ persistence-context.xml

|_____Service

     |_ service-context.xml

|_____View

     |_ spring/app-config.xml

And in persistence-context.xml have the next:

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName">

<tx:annotation-driven transaction-manager="persistence.transactionManager"  proxy-target-class="true" />

<bean id="persistence.propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath*:META-INF/jdbc.properties</value>
            <value>classpath*:META-INF/hibernate.properties</value>             
        </list>
    </property>
</bean>

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

<bean name="persistence.jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
    <constructor-arg>
        <ref bean="persistence.dataSource" />
    </constructor-arg>
</bean>

<bean id="persistence.dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.db.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="defaultAutoCommit" value="false" />
    <property name="poolPreparedStatements" value="true" />
    <property name="initialSize" value="20" />
    <property name="maxActive" value="30" />
    <property name="maxIdle" value="25" />
</bean>

<bean id="persistence.sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="persistence.dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
            <prop key="hibernate.connection.autocommit">false</prop>
            <prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
        </props>
    </property>
    <property name="mappingLocations">
        <list>
            <value>classpath:mappings/items/servicio.hbm.xml</value>
            <value>classpath:mappings/items/stockable.hbm.xml</value>   
            <value>classpath:mappings/items/bigstockable.hbm.xml</value>        
        </list>
    </property>
</bean>

<!-- Daos beans -->

<bean id="servicioDao" class="daos.ServicioDao" >
    <property name="sessionFactory" ref="persistence.sessionFactory" />
</bean>
<bean id="stockableDao" class="daos.StockableDao" >
    <property name="sessionFactory" ref="persistence.sessionFactory" />
</bean>
<bean id="bigStockableDao" class="daos.BigStockableDao" >
    <property name="sessionFactory" ref="persistence.sessionFactory" />
</bean>

In that xml i make my daos with it sessionFactory, but when i startup the project i got java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required, because my hibernateTemplate is null. My daos extends from HibernateDaoSupport and i know that if you give to your dao a sessionFactory it will create automatically an hibernateTemplate, and idk what could be happening.

My daos have a @Repository (example @Repository(value="servicioDao"))

And the services the @Service with the @Autowired in the setter

and i am adding them in the contex

   <context:component-scan base-package="controllers" />
   <context:component-scan base-package="servicios" />
   <context:component-scan base-package="daos" />

I just add this in the persistence-context.xml

<!-- Daos beans -->

<bean id="servicioDao" class="daos.ServicioDao" >
    <property name="sessionFactory" ref="persistence.sessionFactory" />
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<bean id="stockableDao" class="daos.StockableDao" >
    <property name="sessionFactory" ref="persistence.sessionFactory" />
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<bean id="bigStockableDao" class="daos.BigStockableDao" >
    <property name="sessionFactory" ref="persistence.sessionFactory" />
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

I get the same error.

Some of my daos code:

@Repository(value="servicioDao")
@SuppressWarnings("serial")
public class ServicioDao extends GenericHome<Servicio>{
public ServicioDao(){}
@Override
protected Class<Servicio> getDomainClass() {
return Servicio.class;
}
}
public abstract class GenericHome<T> extends HibernateDaoSupport implements     Serializable{
protected Class<T> persistentClass = this.getDomainClass();
protected abstract Class<T> getDomainClass();
}
public class ServicioService {
private ServicioDao servicioDao;
public ServicioService(){}
public ServicioDao getServicioDao() {
return servicioDao;
}
@Autowired
public void setServicioDao(ServicioDao servicioDao) {
this.servicioDao = servicioDao;
}
}
2

2 Answers

1
votes

I noticed that when i use @Service and @Repository, beans arent created by the xml, so when it gave me the error "'sessionFactory' or 'hibernateTemplate' is required" was because the dao was created but never filled its sessionFactory, so to use my xml files i created the controller like a normal bean

0
votes

Try changing your bean definitions for this:

<bean id="servicioDao" class="daos.ServicioDao" >
        <constructor-arg>
            <ref bean="persistence.sessionFactory" />
        </constructor-arg>
</bean>

It means that you are passing the sessionFactory in the constructor of your DAO class.

You also have to write the full pass in your "class" parameter.

<bean id="servicioDao" class="full.package.path.to.ServicioDao" >

Next, in your DAO class write something like this:

@Repository
public class ServicioDao{
    private SessionFactory sessionFactory;

    public ServicioDao() {

    }

    /**
     * Constructor.
     *
     * @since 1.0
     */
    public ServicioDao(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    ...
    ...
 }

Finally, in your controllers, you can use the DAO class as follows:

...
@Autowired
ServicioDao servicioDao;
...
servicioDao.getServicioDao();

Notice that you don't need to make new ServicioDao();.

Do it for every DAO class.