mardi 12 août 2014

Java - différence entre ces 1 de 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 Java code used to execute SQL Queries:


getSession().createSQLQuery(query);

getSession().createQuery

jdbcTemplate.queryForList(query);

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


We use MySql database, hibernate and spring mvc in our application.


Also i find all the above 3 types of queries in my application. so getting a bit confused like in which situation which type of query do we have to use.




getSession().createQuery() - For JP-QL or HQL


getSession().createSQLQuery() - For native sql


jdbcTemplate.queryForList(query); - For native sql. Its a Spring convenient jdbcTemplate which helps to avoid more coding like connection open / close, exception handling and much more.


Option #1: Is portable across different DB vendors. So we should prefer the first approach in JPA / Hibernate codes.


Option #2: If you can't manage with first option you can use the seconf option with native sql (eg. Oracle DB). But this is not portable if you move to another DB vendor like MySQL.


Option #3: If you are working on plain JDBC code use this.



Hi can any one explain me with brief example, the difference between the following Java code used to execute SQL Queries:


getSession().createSQLQuery(query);

getSession().createQuery

jdbcTemplate.queryForList(query);

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


We use MySql database, hibernate and spring mvc in our application.


Also i find all the above 3 types of queries in my application. so getting a bit confused like in which situation which type of query do we have to use.



getSession().createQuery() - For JP-QL or HQL


getSession().createSQLQuery() - For native sql


jdbcTemplate.queryForList(query); - For native sql. Its a Spring convenient jdbcTemplate which helps to avoid more coding like connection open / close, exception handling and much more.


Option #1: Is portable across different DB vendors. So we should prefer the first approach in JPA / Hibernate codes.


Option #2: If you can't manage with first option you can use the seconf option with native sql (eg. Oracle DB). But this is not portable if you move to another DB vendor like MySQL.


Option #3: If you are working on plain JDBC code use this.


0 commentaires:

Enregistrer un commentaire