Hibernate application creates a Native Hibernate SQL Query uses the create SQLQuery () method. To execute the Native Hibernate SQL Query, object of SQLQuert needs to be created. Tables and columns will be directly given while executing the queries. Following are the mandatory points in Native SQL Query.
Following is the syntax to create an SQL Query object by calling the createSQLQuery () of Session interface.
SQLQuery query = session.createSQLQuery(" select * from IToolsInfo "); List list=query.list();
To convert each row as POJO class object, then addEntity() has to be called. In addEntity() parameter is passed to the Pojo class object of the class.
SQLQuery query=session.createSQLQuery(" select * from IToolsInfo "); query.addEntity("IToolsInfo.class"); List list=query.list();
addEntity() present in query object.
To read the partial entity or more entities from a database, Hibernate internally stores a ResultSetMetaData.
SQLQuery query=session.createSQLQuery("select employeeName,employeeSalary from IToolsInfo "); query.addEntity("IToolsInfo.class"); List list=query.list();
This list object contains object[] array of ResultSetMetaData. Convert the specified columns of type, by calling the addScalar().
SQLQuery query=session.createSQLQuery("select employeeName,employeeSalary from IToolsInfo "); query.addSclar("employeeName", HIBERNATE.STRING); query.addSclar("employeeSalary", HIBERNATE.STRING); List list=query.list();
SQLQuery query = session.createSQLQuery("update IToolsInfo set salary=8000 where deptno=1"); Transaction transaction=session.beginTransaction(); int i=query.executeUpadate(); transaction.commit();