Question

I use Spring 3 and Hibernate 4. I try to save a Users entity in database but it's not working. When i call the method finUsersByUsername(String username) it's work well. But when i use the save method defined in GenericDao it's does not work. I don't understand it. If someone could help me ?

Domain class users:

    @Entity
    @Table(name="USERS")
    public class Users{ 

        @Id
        @Column(name="USERNAME", length=50,nullable=false )
        private String username;

        @Column(name="PASSWORD",length=50, nullable=false)
        private String password;

        @Column(name="ENABLED", nullable=false)
        private Boolean enabled;

        @Column(name="LASTNAME",length=50)
        private String lastName;

        @Column(name="FIRTSNAME",length=50)
        private String firstName;

        @Column(name="DATEOFBIRTH")
        private Date dateOfBirth;

        @Column(name="EMAIL",length=50, nullable=false)
        private String email;

        @Column(name="PHONENUMBER",length=50)
        private String phoneNumber;

        ... getters and setters


    }

Generic DAO interface :

    public interface GenericDao<T, PK extends Serializable> {

        PK create(T newInstance);

        T read(PK id);

        List<T> readAll();

        List<T> readByCriteria(Criterion criterion);

            void update(T transientObject);

        void delete(T persistentObject);
    }

DAO implementation:

@Repository
public class GenericDaoImpl<T, PK extends Serializable> implements GenericDao<T, PK> {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    private Class<T> type;

    public GenericDaoImpl() {
        super();
    }


    public GenericDaoImpl(Class<T> type) {
        this.type = type;
    }


    @Transactional
    @SuppressWarnings("unchecked")
    public PK create(T object) {
        return (PK) getSession().save(object);
    }


    @SuppressWarnings("unchecked")
    public T read(PK id) {
        return (T) getSession().get(type, id);
    }


    public List<T> readAll() {
        return readByCriteria();
    }


    @SuppressWarnings("unchecked")
    public List<T> readByCriteria(Criterion... criterion) {
        Criteria crit = getSession().createCriteria(type);
        for (Criterion c : criterion) {
            crit.add(c);
        }
        return crit.list();
    }

    @Transactional
    public void update(T object) {
        getSession().update(object);
    }

    @Transactional
    public void delete(T object) {
        getSession().delete(object);
    }


    public Session getSession() {
        return sessionFactory.openSession();
    }


}

Users DAO Interface:

public interface UserDao extends GenericDao<Users, Long> {

    Users finUsersByUsername(String username);

}

Users Dao implementations

@Repository
@Transactional
public class UserDaoImpl extends GenericDaoImpl<Users, Long>  implements UserDao{

    @Autowired
    private SessionFactory sessionFactory;

    public UserDaoImpl(){}

    @SuppressWarnings("unused")
    private Class<Users> type;
    public UserDaoImpl(Class<Users> type) {
        super(type);

    }
    @Override
    public Users finUsersByUsername(String username) {

        return (Users) sessionFactory.getCurrentSession().load(Users.class,username);
    }

}

Spring configuration file:

?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

     <!-- Configuration des annotations -->
    <context:annotation-config />
    <!-- Activation du scan des annotations Spring MVC -->
    <context:component-scan base-package="com.nexthome.app"/>

    <bean id="userDao" class="com.nexthome.app.dao.hibernate.UserDaoImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://localhost:5432/home"/>
        <property name="username" value="postgres"/>
        <property name="password" value="database"/>
    </bean>

 <tx:annotation-driven />  
 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
 <bean id="sessionFactory"  
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  <property name="dataSource" ref="dataSource" />  
  <property name="annotatedClasses">
<list>
<value>com.nexthome.app.dao.entities.Users</value>
</list>
</property>
  <property name="hibernateProperties">  
   <props>  
    <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>  
    <prop key="hibernate.show_sql">true</prop>  
    <prop key="hibernate.hbm2ddl.auto">update</prop>      
   </props>  
  </property>  
 </bean>    

    <!-- Resolveur de vues -->      
    <bean id="jspViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsps/"
          p:suffix=".jsp" />    
</beans>

Thanks for your help

Was it helpful?

Solution

This method is probably the root cause:

public Session getSession() {
    return sessionFactory.openSession();
}

Spring instantiates a session at start of the Transaction. When opening your own session, it won't be managed by Springs Transaction abstraction. Use the existing session instead of creating a new one. Probably changing method to this does the trick:

public Session getSession() {
    return sessionFactory.getCurrentSession();
}

See also: http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/orm.html#orm-hibernate

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top