jeudi 1 mai 2014

Java - clarté entre les différents types de 1 requêtes SQL) createSQLQuery(query) ; createQuery 2) 3) jdbcTemplate.queryForList(query) ; -Débordement de pile


Hi can any one explain me with brief example, the difference between the following SQL Queries:



  1. getSession().createSQLQuery(query);


  2. getSession().createQuery


  3. jdbcTemplate.queryForList(query);



I would be grateful if you also explain, in which situation we have to use which query out of these three.




Short answer for each one:



  1. getSession().createSQLQuery(query) Here you pass a native SQL query string to the method, which will execute it as is. Note that this one requires a valid native SQL query for the underlying database (beware of specific features of your database)

  2. getSession().createQuery Here you pass an HQL query. This is a query format specific to Hibernate, which you can find in its documentation: http://docs.jboss.org/hibernate/core/4.3/manual/en-US/html/ch16.html

  3. jdbcTemplate.queryForList(query) This is similar to the first one, since it accepts a native SQL query.


Remember that the Hibernate session and Spring persistence management by templates are different things (though both can be complementary).


If your application relies on an ORM such as Hibernate, your choice is 2). You may like to consider Criteria too if your choice is ORM (you can choose Criteria or HQL in different parts of your code, since both rely on your entities configuration and bring different advantages). Some info on Criteria queries: http://docs.jboss.org/hibernate/core/4.3/manual/en-US/html/ch17.html


If you intend to use native SQL through your app, I would go for 3) and rely on Spring template features for comfort.



Hi can any one explain me with brief example, the difference between the following SQL Queries:



  1. getSession().createSQLQuery(query);


  2. getSession().createQuery


  3. jdbcTemplate.queryForList(query);



I would be grateful if you also explain, in which situation we have to use which query out of these three.



Short answer for each one:



  1. getSession().createSQLQuery(query) Here you pass a native SQL query string to the method, which will execute it as is. Note that this one requires a valid native SQL query for the underlying database (beware of specific features of your database)

  2. getSession().createQuery Here you pass an HQL query. This is a query format specific to Hibernate, which you can find in its documentation: http://docs.jboss.org/hibernate/core/4.3/manual/en-US/html/ch16.html

  3. jdbcTemplate.queryForList(query) This is similar to the first one, since it accepts a native SQL query.


Remember that the Hibernate session and Spring persistence management by templates are different things (though both can be complementary).


If your application relies on an ORM such as Hibernate, your choice is 2). You may like to consider Criteria too if your choice is ORM (you can choose Criteria or HQL in different parts of your code, since both rely on your entities configuration and bring different advantages). Some info on Criteria queries: http://docs.jboss.org/hibernate/core/4.3/manual/en-US/html/ch17.html


If you intend to use native SQL through your app, I would go for 3) and rely on Spring template features for comfort.


0 commentaires:

Enregistrer un commentaire