如下代码:
(注意该类继承自HibernateDaoSupport ,要在applicationContext.xml中将sessionFactory注入此类中)
public class DaoUtil extends HibernateDaoSupport {
public Object executeMySQL(final String sql){
System.out.println(sql);
return getHibernateTemplate().execute( new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Connection CurConn = session.connection();
PreparedStatement ps = CurConn.prepareStatement(sql);
ps.execute();
ps.close();
session.flush();
return null;
}
} );
}
public Object executeBetchSQL(final ArrayList sqlList){
return getHibernateTemplate().execute( new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Connection CurConn = session.connection();
int count = sqlList.size();
for(int i=0;i//System.out.println(sqlList.get(i));
PreparedStatement ps = CurConn.prepareStatement(sqlList.get(i));
ps.execute();
ps.close();
session.flush();
}
return null;
}
} );
}
public static DaoUtil getFromApplicationContext(
ApplicationContext ctx) {
return (DaoUtil) ctx.getBean("DaoUtil");
}
}
HibernateDaoSupport 是spring提供的对hibernate支持的dao层类,hibernate本身是没有的。select dateadd(month,6,getdate()) 就是一个取database server时间的再+6个月的查询语句,和你app本身的domain对象一点关系都没有,完全没有必要放在hibernate的session中执行。直接用jdbc执行就好了。
hibernate直接操纵的是对象,不是表,而你操作的是text的表
应该改成: session session = hibernatesessionfactory.currentsession();
transaction ts = session.begintransaction();
text user = (text)session.get(user.class, user.id); user.setname(name);
user.setpwd(pwd);
session.save(user);
ts.commit();
hibernatesessionfactory.closesession();
这种用法相对比较规范,也符合hibernate的开发规范。
楼上说的对啊 getSession.createSqlQuery(“select dateadd(month,6,getdate()) ”);
用户登录
还没有账号?立即注册
用户注册
投稿取消
| 文章分类: |
|
还能输入300字
上传中....
诸葛村妇主任