简体   繁体   English

正确使用sessionFactory的方法

[英]Correct way to use sessionFactory

Im trying to learn spring 3 and DAO and BO classes and how to autowire with it and I was wondering is this the correct way to wire the sessionFactory as i have read that it is better to use 我试图学习spring 3和DAO和BO类以及如何用它自动装配我想知道这是连接sessionFactory的正确方法,因为我已经读过它更好用

public void save(Customer customer) {
    sessionFactory.getCurrentSession().save(customer);
}

rather than 而不是

public void save(Customer customer){
    getHibernateTemplate().save(customer);
}

So is the following the correct way to wire the sessionFactory? 以下是连接sessionFactory的正确方法吗?

CustomHibernateDaoSupport class CustomHibernateDaoSupport类

package com.fexco.helloworld.web.util;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class CustomHibernateDaoSupport extends HibernateDaoSupport
{    
@Autowired
@Qualifier("sessionFactory")
public void seSessionFactory(SessionFactory sessionFactory) {

    this.setSessionFactory(sessionFactory);
}
}

CustomerDaoImpl class CustomerDaoImpl类

package com.fexco.helloworld.web.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.fexco.helloworld.web.model.Customer;
import com.fexco.helloworld.web.util.CustomHibernateDaoSupport;

@Repository("customerDao")
public class CustomerDaoImpl extends CustomHibernateDaoSupport implements CustomerDao{


@Autowired
private SessionFactory sessionFactory;

public void save(Customer customer) {
    sessionFactory.getCurrentSession().save(customer);
}

Is this correct or am i making a mistake somewhere because i cant get it to work? 这是正确的还是我在某个地方犯了错误,因为我无法让它发挥作用? Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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