使用hibernate的session.connection()来运行原生sql的任何问题 [英] Any problems in using hibernate's session.connection() to run native sql

查看:97
本文介绍了使用hibernate的session.connection()来运行原生sql的任何问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,他们按照下面提到的方式使用Hibernate的会话,然后使用事务保存实体对象。

 会话会话= HibernateUtil.getCurrentSession(); 
session.beginTransaction();

Employee employee = new Employee();

employee.setName(someName);
employee.setEmailId(someId)
employee.setID(100000);

session.saveOrUpdate(employee);

session.getTransaction()。commit();

现在为了少数功能,我决定运行原生SQL。以下是我用来运行本机sql的方式。我想在事务中运行查询,所以我决定按照以下方式编写代码:

  String query =从master_employee选择名称,其中id =? 

会话会话= HibernateUtil.getCurrentSession();
session.beginTransaction();

连接连接= session.connection();
PreparedStatement psStmt = connection.prepareStatement(query);
psStmt.setInt(1,id);

ResultSet resultSet = psStmt.executeQuery();
//这里我将从这个结果集中获取数据
psStmt.close();
resultSet.close();

//我没有关闭连接对象,因为我从休眠会话
//而不是关闭连接对象,而是提交hibernate的事务
session.getTransaction()。commit();

这是正确的方法吗?交易仍然会被管理?从会话中取出连接对象无法管理到事务中????

请告诉我们在使用这种方式时是否有任何问题?谢谢

解决方案

是的,这里没有任何问题。然而,

,使用 Session.createSQLQuery()运行本地查询会容易得多:

  Session session = HibernateUtil.getCurrentSession(); 
session.beginTransaction();

String name =(String)session
.createSQLQuery(select name from master_employee where id =?)
.setInteger(1,id)
.uniqueResult ();

session.getTransaction()。commit();

另请参阅:




In my project, they use Hibernate's session the below mentioned way and then save entity objects with in a transaction.

Session session = HibernateUtil.getCurrentSession();
session.beginTransaction();

Employee employee = new Employee() ;

employee.setName("someName") ;
employee.setEmailId ("someId")
employee.setID (100000) ;

session.saveOrUpdate(employee) ;

session.getTransaction().commit();

Now for few functionality I decided to run native SQL. Below is the way i used to run native sql. I wanted to run the queries with-in a transaction and so i decided to write the code the below way

String query = "select name from master_employee where id=?"

Session session = HibernateUtil.getCurrentSession();
session.beginTransaction();

Connection connection = session.connection();
PreparedStatement psStmt = connection.prepareStatement(query);
psStmt.setInt(1,id) ;

ResultSet resultSet = psStmt.executeQuery();
// here i will take data from this result set
psStmt.close();
resultSet.close() ;

// I am not closing the connection object since am taking it from hibernate session
// rather i commit hibernate's transaction
session.getTransaction().commit();

Is this the right way ?? will the transaction's still be managed ?? taking the connection object from session cannot be managed in to the transaction ????

Please tell if there any problems in using this way ??? thanks

解决方案

Yes, there are no problems here.

However, it would be much easier to run a native query using Session.createSQLQuery():

Session session = HibernateUtil.getCurrentSession(); 
session.beginTransaction(); 

String name = (String) session
    .createSQLQuery("select name from master_employee where id = ?")
    .setInteger(1, id)
    .uniqueResult();

session.getTransaction().commit();

See also:

这篇关于使用hibernate的session.connection()来运行原生sql的任何问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆