Search code examples
hibernatehqlhibernate-criteria

Is CreateSQLQuery in HQL is Database independant


Could anyone help me to understand, CreateSQLQuery in HQL is Database independent

Please Find below example native SQL of SQLServer using CreateSQLQuery, in future if i need to migrate into Oracle will i need to do any code change required other than Connecting String to the DB.

Example of Native SQL SErver

Query query = session.createSQLQuery(
   "select * from stock s where s.stock_code = :stockCode")
   .addEntity(Stock.class)
   .setParameter("stockCode", "7277");
List result = query.list();

Solution

  • Native SQL queries created by createSQLQuery are database dependent. If you want to have a database independent code you should use HQL or Criteria queries.