0

I am working at a project and I want to update if from xml based configuration to annotation based. I successfully did this at service layer but having troubles with the DAO layer. I have DAO classes that extends BaseDAO, this BaseDAO extends HibernateDaoSupport. I want to annotate DAO classes with @Repository and add component scan to my context.xml. I think the problem is related to the inheritence from my DAO to the BaseDAO? this is my current code that doesn't work which explains the problem better

@Repository ("userDAO")
public class UserDAOImpl extends BaseDAO implements IUserDAO {


   @Override
   public Integer saveUser(UserVO user) {

       user.setDateCreated(MyDateUtils.getCurrentDate());
       user.setDateUpdated(MyDateUtils.getCurrentDate());

       Integer userId = performSave(user);
       return userId;
   }

and this is my BaseDAO

public class BaseDAO extends HibernateDaoSupport {



public Integer performSave(Object object) {
    Integer id = null;
    try {
        id = (Integer) getHibernateTemplate().save(object);
    } catch (DataAccessException e) {

    }
    return id;
}

and this is the xml bean for the BaseDAO

<bean id="baseDAO" class="com.my.dao.database.base.BaseDAO"
    abstract="true">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="MyDataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.showSQL}</prop>
        </props>
    </property>
    <property name="annotatedClasses">
        <list>
            <value>com.my.domain.dao.UserVO</value>
        </list>
    </property>
</bean>

and in my context.xml i use

-- and this is the old configuration for the UserDAO (which works)

    <bean id="userDAO" class="com.my.dao.database.impl.UserDAOImpl"
    parent="baseDAO" />

so to be clearer, i get rid of this xml bean definition for UserDAO and used the @Repository and but it stopped working and throw the java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required

Yasser Afifi
  • 207
  • 1
  • 9

2 Answers2

0

You must tag your sessionFactory with @Autowire in your DaoImpl class.

erolkaya84
  • 1,769
  • 21
  • 28
0

I think that this question is about the same problem:

Spring 3 Annotations - HibernateDaoSupport - Repository Requires Session Factory

I share the selected answer:

HibernateDaoSupport is supplied with SessionFactory via setSessionFactory(). However, setSessionFactory() is final, so you can't override it to add an @Autowired annotation. But you can apply @Autowired to the arbitrary method and call setSessionFactory() from it:

@Autowired
public void init(SessionFactory factory) {
    setSessionFactory(factory);
}

Hope it helps

Detailed explanation:

The original method that provided the SessionFactory to the DAO doesn't won't take de SessionFactory from the IoC container, because doesn't have @Autowired.

You could add it creating a child class that overrides that method, and have the annotation, BUT because that method is final, you can't.

So, because you can't automatically provide the dependency to the setter, your only way is doing it manually. The method above wraps the inyection of the SessionFactory. To make spring call it when the DAO is created, you can use @PostConstruct:

@PostConstruct
@Autowired
public void init(SessionFactory factory) {
    setSessionFactory(factory);
}
Community
  • 1
  • 1
  • It actually works, but i need to understand what you mean by "so you can't override it to add an Autowired annotation", does this mean "Autowired" can't be used with any final methods? – Yasser Afifi Jun 02 '16 at 13:30