getHibernateTemplate()

getHibernateTemplate方法

前提条件:你的类必须继承HibernateDaoSupport 
一: 
回调函数: 
public List getList(){
   return (List ) getHibernateTemplate().execute(
     new HibernateCallback() {
      public List doInHibernate(final Session s)
        throws HibernateException, SQLException {

StringBuffer sb = new StringBuffer();
Query query = s.createSQLQuery(sb.toString());//<STRONG>执行普通sql</STRONG>
Query query = s.createQuery(sb.toString());// <STRONG>执行hql </STRONG>
List list = query.list();
return list ;
      }
     });   
}
采用这种做法的好处是:不用关心事务。session的创建和销毁,一切都在程序内部完成。看看名字doInHibernate就知道好处了。不好的是程序看起来比较乱!个人认为。 
二: 
getHibernateTemplate().find():

List list = getHibernateTemplate().find(String sql , Object[] values);
这种的前提是sql必须是hql ,原始sql 不能执行。

三: 
自己获取session处理。 
获取session的两种方式:this.getSession();和this.getHibernateTemplate().getSessionFactory().openSession(); 
获取session以后再createQuery操作就一样了。但是这种做法,是需要自己去手动关闭session的。所以你需要配置openSessioninview,个人认为不推荐使用!

其他: 
getHibernateTemplate.delete(Object);

getHibernateTemplate.save(Object); 
getHibernateTemplate.update(Object);

总结:推荐使用回调函数。

展开阅读全文

getHibernateTemplate().

05-23

public List selectAll(String hql,String TName) rnrntry rnreturn getHibernateTemplate().find(hql, TName);rnrn catch (RuntimeException re) rnrnthrow re;rnrnrnrnrnrnrnrnpublic List selectAll(String TName) rnrn System.out.println("21212121212121212121");rnrnreturn this.selectAll("From ?",TName);rnrnrnrnrnrnrnrnrn这样写法有问题吗?rn反正我这样写报错了 , 可以输出那些21212121 应该是查询出错了rnrnrnrnrnjava.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)Vrnorg.hibernate.hql.antlr.HqlBaseParser.fromRange(HqlBaseParser.java:1590)rnorg.hibernate.hql.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1408)rnorg.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1130)rnorg.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:702)rnorg.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)rnorg.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)rnorg.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:274)rnorg.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)rnorg.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)rnorg.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:101)rnorg.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:80)rnorg.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)rnorg.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)rnorg.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)rnorg.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)rnorg.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:914)rnorg.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:1)rnorg.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)rnorg.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)rnorg.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:912)rnorg.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:908)rncom.pro.dao.BaseDao.selectAll(BaseDao.java:65)rncom.pro.dao.OneDAO.selectAll(OneDAO.java:25)rncom.pro.serivce.OneSerivce.selectAll(OneSerivce.java:36)rncom.pro.action.FindAction.findty(FindAction.java:29) 论坛

没有更多推荐了,返回首页