Hibernate 调用原生态Sql语句和存储过程
2015-10-06 15:47
447 查看
调用原生态Sql语句:
1.
String sqlStr = "select distinct name from mytable where isDeleted = :isDeleteFlag order by name asc";
List<T> = this.getSessionFactory().getCurrentSession().createSQLQuery(sqlStr).
addScalar("name", StandardBasicTypes.STRING).setParameter("isDeleteFlag", 0).list();
Double max = (Double) session.createSQLQuery("select max(cat.weight) as maxWeight from cats cat")
.addScalar("maxWeight", Hibernate.DOUBLE).uniqueResult();
用 addScalar 指明输出类型,否则会报错。
调用存储过程:
1.
Connection conn = ((SessionImplementor)getSessionFactory().getCurrentSession()).connection();
String procedure = "{call test_proc(?,?,?)}";
CallableStatement cstmt = conn.prepareCall(procedure);
cstmt.setString(1, value1);
cstmt.setString(2, value2);
cstmt.registerOutParameter(3, OracleTypes.CURSOR);//这里的输出是oracle的游标
cstmt.executeUpdate();
ResultSet rs = (ResultSet)cstmt.getObject(5);
2. 用doWork
session.doWork(new Work(){
@Override
public void execute(Connection conn) throws SQLException {
CallableStatement cs = conn.prepareCall("{call test_proc(?,?,?)}");
cs.registerOutParameter(1, OracleTypes.NUMBER);
cs.setInt(2, valueint);
cs.setDate(3, valuedate);
try{
cs.executeQuery();
int returnValue = cs.getInt(1);
}catch(Exception e){
e.printStackTrace();
}finally{
cs.close();
}
}
});
3.
session.getConnetion().createQuery("call XXX");
4.
session.createSQLQuery("{Call 存储过程名(参数)}");
1.
String sqlStr = "select distinct name from mytable where isDeleted = :isDeleteFlag order by name asc";
List<T> = this.getSessionFactory().getCurrentSession().createSQLQuery(sqlStr).
addScalar("name", StandardBasicTypes.STRING).setParameter("isDeleteFlag", 0).list();
Double max = (Double) session.createSQLQuery("select max(cat.weight) as maxWeight from cats cat")
.addScalar("maxWeight", Hibernate.DOUBLE).uniqueResult();
用 addScalar 指明输出类型,否则会报错。
调用存储过程:
1.
Connection conn = ((SessionImplementor)getSessionFactory().getCurrentSession()).connection();
String procedure = "{call test_proc(?,?,?)}";
CallableStatement cstmt = conn.prepareCall(procedure);
cstmt.setString(1, value1);
cstmt.setString(2, value2);
cstmt.registerOutParameter(3, OracleTypes.CURSOR);//这里的输出是oracle的游标
cstmt.executeUpdate();
ResultSet rs = (ResultSet)cstmt.getObject(5);
2. 用doWork
session.doWork(new Work(){
@Override
public void execute(Connection conn) throws SQLException {
CallableStatement cs = conn.prepareCall("{call test_proc(?,?,?)}");
cs.registerOutParameter(1, OracleTypes.NUMBER);
cs.setInt(2, valueint);
cs.setDate(3, valuedate);
try{
cs.executeQuery();
int returnValue = cs.getInt(1);
}catch(Exception e){
e.printStackTrace();
}finally{
cs.close();
}
}
});
3.
session.getConnetion().createQuery("call XXX");
4.
session.createSQLQuery("{Call 存储过程名(参数)}");
相关文章推荐
- SQL*LOADER
- oracle9
- Mysql数据引擎的选择
- sql server 2005的端口号启动问题
- MSSQL errorlog 引起磁盘空间不足的解决方法
- 【转】使用 Hive 构建数据库
- SQL Server 检测到基于一致性的逻辑 I/O 错误 pageid 不正确
- mysql常见问题
- hibernate修改数据库已有的对象【简化操作】
- oracle中的一些简单查询
- 软考数据库宏观
- SQL修改字段默认值
- Oracle 设置自增ID
- oracle之完整性约束
- mysql基本操作
- SQL server 约束
- MySQL执行报错:ERROR 1148 (42000): The used command is not allowed with this MySQL version
- oracle8
- Oracle 查询转换之子查询展开
- mysql 报错 Too many connections