GenericDAO方法(如save)不能与Hibernate和Spring一起使用

时间:2014-01-02 21:56:39

标签: java spring hibernate dao

我使用的是Spring 3和Hibernate 4.我尝试在数据库中保存Users实体,但它不起作用。当我调用方法 finUsersByUsername(String username)时,它运行良好。但是当我使用GenericDao中定义的save方法时,它不起作用。我不明白。如果有人可以帮助我?

域类用户:

    @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实施:

@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();
    }


}

用户DAO界面:

public interface UserDao extends GenericDao<Users, Long> {

    Users finUsersByUsername(String username);

}

用户Dao实现

@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配置文件:

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

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

这种方法可能是根本原因:

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

Spring在事务开始时实例化会话。打开自己的会话时,它不会由Springs Transaction抽象管理。使用现有会话而不是创建新会话。可能改变方法就可以了:

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

另请参阅:http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/orm.html#orm-hibernate