Sessionfactory.getCurrentSession与 openSession() 的区别

1. getCurrentSession创建的session会和绑定到当前线程,而openSession不会。

2. getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭。

这里getCurrentSession本地事务(本地事务:jdbc)时要在配置文件里进行如下设置

 

  * 如果使用的是本地事务(jdbc事务)
 thread
 * 如果使用的是全局事务(jta事务) 

jta 

 

继承Session 方法及说明

1 TransactionbeginTransaction()

开始工作单位,并返回关联事务对象。

2 voidcancelQuery()

取消当前的查询执行。

3 void clear()

完全清除该会话。

4 Connectionclose()

通过释放和清理 JDBC 连接以结束该会话。

5 Criteria createCriteria(ClasspersistentClass)

为给定的实体类或实体类的超类创建一个新的 Criteria 实例。

6 CriteriacreateCriteria(String entityName)

为给定的实体名称创建一个新的 Criteria 实例。

7 SerializablegetIdentifier(Objectobject)

返回与给定实体相关联的会话的标识符值。

8 QuerycreateFilter(Object collection, String queryString)

为给定的集合和过滤字符创建查询的新实例。

9 QuerycreateQuery(String queryString)

为给定的 HQL 查询字符创建查询的新实例。

10 SQLQuerycreateSQLQuery(String queryString)

为给定的 SQL 查询字符串创建 SQLQuery 的新实例。

11 void delete(Objectobject)

从数据存储中删除持久化实例。

12 void delete(String entityName, Objectobject)

从数据存储中删除持久化实例。

13 Session get(String entityName,Serializable id)

返回给定命名的且带有给定标识符或 null 的持久化实例(若无该种持久化实例)。

14SessionFactory getSessionFactory()

获取创建该会话的 session 工厂。

15 voidrefresh(Objectobject)

从基本数据库中重新读取给定实例的状态。

16 TransactiongetTransaction()

获取与该 session 关联的事务实例。

17booleanisConnected()

检查当前 session 是否连接。

18boolean isDirty()

该 session 中是否包含必须与数据库同步的变化?

19boolean isOpen()

检查该 session 是否仍处于开启状态。

20 Serializablesave(Objectobject)

先分配一个生成的标识,以保持给定的瞬时状态实例。

21 voidsaveOrUpdate(Objectobject)

保存(对象)或更新(对象)给定的实例。

22 void update(Objectobject)

更新带有标识符且是给定的处于脱管状态的实例的持久化实例。

23 void update(String entityName, Objectobject)

更新带有标识符且是给定的处于脱管状态的实例的持久化实例。

 

 

实例


import java.text.SimpleDateFormat;  
import java.util.Date;  
  
import org.hibernate.SessionFactory;  
import org.springframework.stereotype.Repository;  
import com.ztkj.oes.dao.OneChoiceDao;  
import com.ztkj.oes.model.OneChoiceQuestions;  
import com.ztkj.oes.utils.AbstractManager;  
import com.ztkj.oes.utils.PageUtil;  
  
  
@Repository  
public class OneChoiceDaoImpl extends AbstractManager implements OneChoiceDao {  
  
    protected SessionFactory sessionFactory;  
      
    public void setSessionFactory(SessionFactory sessionFactory) {  
        this.sessionFactory = sessionFactory;  
    }  
      
    public PageUtil conditionFind(Date beginDate, Date endDate, String content,  
            int belongsChapter, int category) {  
        StringBuffer hql = new StringBuffer();  
        hql.append("select p from OneChoiceQuestions p where 1=1");  
        if(beginDate != null && endDate != null){  
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
            String beginDate1 = sdf.format(beginDate);  
            String endDate1 = sdf.format(endDate);  
            hql.append(" and (p.inputTime between '").append(beginDate1).append("' and '").append(endDate1).append("')");  
        }  
        if(content != null){  
            //模糊查询  
            hql.append(" and p.content like '%").append(content).append("%'");  
        }  
        if(belongsChapter != -1){  
            hql.append(" and p.belongsChapter = ").append(belongsChapter);  
        }  
        if(category != -1){  
            hql.append(" and p.category = ").append(category);  
        }  
        return searchPaginate(hql.toString());  
    }  
  
      
    public long conditionTotal(Date beginDate, Date endDate,  
            String content, int belongsChapter, int category) {  
        StringBuffer hql = new StringBuffer();  
        hql.append("select count(*) from OneChoiceQuestions u where 1=1");  
        if(beginDate != null && endDate != null){  
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
            String beginDate1 = sdf.format(beginDate);  
            String endDate1 = sdf.format(endDate);  
            hql.append(" and (u.inputTime between '").append(beginDate1).append("' and '").append(endDate1).append("')");  
        }  
        if(content != null){  
            hql.append(" and u.content like '%").append(content).append("%'");  
        }  
        if(belongsChapter != -1){  
            hql.append(" and u.belongsChapter = ").append(belongsChapter);  
        }  
        if(category != -1){  
            hql.append(" and u.category = ").append(category);  
        }  
        return ((Long)sessionFactory.getCurrentSession().createQuery(hql.toString()).uniqueResult()).intValue();  
    }  
  
      
    public void deleteOneChoiceQuestions(long[] idArray) {  
        OneChoiceQuestions oneChoiceQuestions = new OneChoiceQuestions();  
        for(int i=0;i
            oneChoiceQuestions = (OneChoiceQuestions)sessionFactory.getCurrentSession().get(OneChoiceQuestions.class, idArray[i]);  
            sessionFactory.getCurrentSession().delete(oneChoiceQuestions);  
        }  
    }  
  
      
    public void deleteOneChoiceQuestions(long id) {  
        sessionFactory.getCurrentSession().delete(  
                (OneChoiceQuestions)sessionFactory.getCurrentSession().get(OneChoiceQuestions.class, id)  
                );  
    }  
  
      
    public OneChoiceQuestions findById(long id) {  
        return (OneChoiceQuestions)sessionFactory.getCurrentSession().get(OneChoiceQuestions.class, id);  
    }  
  
      
    public void saveOneChoiceQuerstion(OneChoiceQuestions oneChoiceQuestions) {  
        sessionFactory.getCurrentSession().save(oneChoiceQuestions);  
    }  
  
      
    public void updateOneChoiceQuestions(OneChoiceQuestions oneChoiceQuestions) {  
        sessionFactory.getCurrentSession().update(oneChoiceQuestions);  
    }  
      
}  

根据String 查询拿到一个对象,代码如下

 

  
public User findUserByIdCarNo(String idCardNo){  
    StringBuffer hql = new StringBuffer();  
    hql.append("select u from User u where u.idCardNo = '").append(idCardNo).append("'");  
    User user= (User)sessionFactory.getCurrentSession().createQuery(hql.toString()).uniqueResult();  
    return user;  
}  

配置文件.xml,及配置的依赖关系

applicationContext-dao.xml

<</span>bean id="oneChoiceDao" class="com.ztkj.oes.dao.impl.OneChoiceDaoImpl">  
     <</span>property name="sessionFactory" ref="sessionFactory"></</span>property>  
   </</span>bean>  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值