mysql> select e.emp_id as emp_id1_1_0_, e.emp_name as emp_name2_1_0_, e.emp_salary as emp_sala3_1_0_, a.emp_id as emp_id1_0_1_, a.address_line1 as address_2_0_1_, a.city as cit圓_0_1_, a.zipcode as zipcode4_0_1_ from Employee e join Address a ON e.emp_id=a.emp_id You can run both the queries in the mysql client and notice that the output produced is same. Id= 1, Name= Pankaj, Salary= 100.0, Īddress Info::AddressLine1= City Centre, City=New Delhi, Zipcode=100100 Hibernate: select emp_id, emp_name, emp_salary from Employee ![]() When we execute above code for the data setup we have, it produces following output. SQLQuery query = session.createSQLQuery("select emp_id, emp_name, emp_salary from Employee") Įmp.setId(Long.parseLong(row.toString())) Įmp.setSalary(Double.parseDouble(row.toString())) Transaction tx = session.beginTransaction() Session session = sessionFactory.getCurrentSession() SessionFactory sessionFactory = HibernateUtil.getSessionFactory() For example, if you want to read all the records from Employee table, we can do it through below code. Hibernate Native SQL Exampleįor Hibernate Native SQL Query, we use Session.createSQLQuery(String query) to create the SQLQuery object and execute it. I will use MySQL database and same tables and data setup as used in HQL example, so you should check out that first to understand the tables and corresponding model classes mapping. For normal scenarios, Hibernate SQL query is not the recommended approach because we loose benefits related to hibernate association and hibernate first level cache. For example query hints or the CONNECT keyword in Oracle Database. ![]() Hibernate SQL Query is very handy when we have to execute database vendor specific queries that are not supported by Hibernate API. ![]() Hibernate provide option to execute native SQL queries through the use of SQLQuery object. We looked into Hibernate Query Language and Hibernate Criteria in earlier articles, today we will look into Hibernate Native SQL query with examples. Welcome to the Hibernate Native SQL Query example tutorial.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |