简体   繁体   中英

Hibernate 4 + Spring + getHibernateTemplate().getSessionFactory().getCurrentSession()

Want to use Spring to automatically work with Hibernate 4 sessions Also I like to extends DAO, for not create many DAO for many entities But there is problem:

SessionDAO.java

public abstract class SessionDAO extends HibernateDaoSupport{

    public void startSession() {

        getSession().beginTransaction();
    }

    public void closeSession() {
        getSession().getTransaction().commit();
    }

    public void addObject() {
        startSession();
        getSession().save(this);
        closeSession();
    }

    public Session getSession()
    {
        return getHibernateTemplate().getSessionFactory().getCurrentSession();

    }
}

Values.java

@Entity
@Table
public class Values extends SessionDAO {
private int valuesId;
private double amount;
private Date date;
//...getters, setters, etc
}

dispatcher-servlet.xml

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>entity.Crypto</value>
            <value>entity.Values</value>
        </list>
    </property>
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
         </props>
      </property>

   </bean>

   <bean id="dataSource"
    class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
      <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
      <property name="url" value="jdbc:oracle:thin:@my.****.us-east-1.rds.amazonaws.com:1521:ORCL" />
      <property name="username" value="****" />
      <property name="password" value="****" />
   </bean>

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

   <bean id="persistenceExceptionTranslationPostProcessor"
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

When start server, receive:

INFO: HHH000206: hibernate.properties not found

When try to load page receive:

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException

on line

return getHibernateTemplate().getSessionFactory().getCurrentSession();

What's wrong?

Don't get the session by extend HibernateDaoSupport , just inject SessionFactory in DAO .

private HibernateTemplate hibernateTemplate;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM