Switch from createSQLQuery to createNativeQuery as the former is deprecated since 5.2

This commit is contained in:
Vlad Mihalcea 2017-05-30 09:52:56 +03:00
parent 6f35871266
commit 25e3aeb0b8
5 changed files with 21 additions and 21 deletions

View File

@ -12,7 +12,7 @@ Hibernate also allows you to specify handwritten SQL (including stored procedure
[[sql-jpa-query]]
=== Creating a native query using JPA
Execution of native SQL queries is controlled via the `SQLQuery` interface, which is obtained by calling `Session.createSQLQuery()`.
Execution of native SQL queries is controlled via the `NativeQuery` interface, which is obtained by calling `Session.createNativeQuery()`.
The following sections describe how to use this API for querying.
[[sql-scalar-query]]

View File

@ -56,7 +56,7 @@ public class AlwaysFlushTest extends BaseEntityManagerFunctionalTestCase {
Session session = entityManager.unwrap( Session.class);
assertTrue(((Number) session
.createSQLQuery("select count(*) from Person")
.createNativeQuery("select count(*) from Person")
.setFlushMode( FlushMode.ALWAYS)
.uniqueResult()).intValue() == 1);
//end::flushing-always-flush-sql-example[]

View File

@ -136,7 +136,7 @@ public class AutoFlushTest extends BaseEntityManagerFunctionalTestCase {
//session.setHibernateFlushMode( FlushMode.COMMIT );
assertTrue(((Number) session
.createSQLQuery( "select count(*) from Person")
.createNativeQuery( "select count(*) from Person")
.uniqueResult()).intValue() == 0 );
//end::flushing-auto-flush-sql-native-example[\]
} );
@ -159,7 +159,7 @@ public class AutoFlushTest extends BaseEntityManagerFunctionalTestCase {
Session session = entityManager.unwrap( Session.class );
assertTrue(((Number) session
.createSQLQuery( "select count(*) from Person")
.createNativeQuery( "select count(*) from Person")
.addSynchronizedEntityClass( Person.class )
.uniqueResult()).intValue() == 1 );
//end::flushing-auto-flush-sql-synchronization-example[]

View File

@ -62,7 +62,7 @@ public class ManualFlushTest extends BaseEntityManagerFunctionalTestCase {
.getSingleResult()).intValue() == 0);
assertTrue(((Number) session
.createSQLQuery("select count(*) from Person")
.createNativeQuery("select count(*) from Person")
.uniqueResult()).intValue() == 0);
//end::flushing-manual-flush-example[]
});

View File

@ -184,7 +184,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-all-columns-scalar-query-example[]
List<Object[]> persons = session.createSQLQuery(
List<Object[]> persons = session.createNativeQuery(
"SELECT * FROM Person" )
.list();
//end::sql-hibernate-all-columns-scalar-query-example[]
@ -197,7 +197,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-custom-column-selection-scalar-query-example[]
List<Object[]> persons = session.createSQLQuery(
List<Object[]> persons = session.createNativeQuery(
"SELECT id, name FROM Person" )
.list();
@ -215,7 +215,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-scalar-query-explicit-result-set-example[]
List<Object[]> persons = session.createSQLQuery(
List<Object[]> persons = session.createNativeQuery(
"SELECT * FROM Person" )
.addScalar( "id", LongType.INSTANCE )
.addScalar( "name", StringType.INSTANCE )
@ -236,7 +236,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-scalar-query-partial-explicit-result-set-example[]
List<Object[]> persons = session.createSQLQuery(
List<Object[]> persons = session.createNativeQuery(
"SELECT * FROM Person" )
.addScalar( "id", LongType.INSTANCE )
.addScalar( "name" )
@ -268,7 +268,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-entity-query-example[]
List<Person> persons = session.createSQLQuery(
List<Person> persons = session.createNativeQuery(
"SELECT * FROM Person" )
.addEntity( Person.class )
.list();
@ -295,7 +295,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-entity-query-explicit-result-set-example[]
List<Person> persons = session.createSQLQuery(
List<Person> persons = session.createNativeQuery(
"SELECT id, name, nickName, address, createdOn, version " +
"FROM Person" )
.addEntity( Person.class )
@ -323,7 +323,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-entity-associations-query-many-to-one-example[]
List<Phone> phones = session.createSQLQuery(
List<Phone> phones = session.createNativeQuery(
"SELECT id, phone_number, phone_type, person_id " +
"FROM Phone" )
.addEntity( Phone.class )
@ -356,7 +356,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-entity-associations-query-many-to-one-join-example[]
List<Object[]> tuples = session.createSQLQuery(
List<Object[]> tuples = session.createNativeQuery(
"SELECT * " +
"FROM Phone ph " +
"JOIN Person pr ON ph.person_id = pr.id" )
@ -378,7 +378,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-entity-associations-query-many-to-one-join-result-transformer-example[]
List<Person> persons = session.createSQLQuery(
List<Person> persons = session.createNativeQuery(
"SELECT * " +
"FROM Phone ph " +
"JOIN Person pr ON ph.person_id = pr.id" )
@ -421,7 +421,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
try {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
List<Phone> phones = session.createSQLQuery(
List<Phone> phones = session.createNativeQuery(
"SELECT * " +
"FROM Phone ph " +
"JOIN phone_call c ON c.phone_id = ph.id" )
@ -450,7 +450,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-entity-associations-query-one-to-many-join-example[]
List<Object[]> tuples = session.createSQLQuery(
List<Object[]> tuples = session.createNativeQuery(
"SELECT * " +
"FROM Phone ph " +
"JOIN phone_call c ON c.phone_id = ph.id" )
@ -493,7 +493,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-multi-entity-query-example[]
List<Object> entities = session.createSQLQuery(
List<Object> entities = session.createNativeQuery(
"SELECT * " +
"FROM Person pr, Partner pt " +
"WHERE pr.name = pt.name" )
@ -516,7 +516,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-multi-entity-query-alias-example[]
List<Object> entities = session.createSQLQuery(
List<Object> entities = session.createNativeQuery(
"SELECT {pr.*}, {pt.*} " +
"FROM Person pr, Partner pt " +
"WHERE pr.name = pt.name" )
@ -533,7 +533,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-dto-query-example[]
List<PersonSummaryDTO> dtos = session.createSQLQuery(
List<PersonSummaryDTO> dtos = session.createNativeQuery(
"SELECT p.id as \"id\", p.name as \"name\" " +
"FROM Person p")
.setResultTransformer( Transformers.aliasToBean( PersonSummaryDTO.class ) )
@ -548,7 +548,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-inheritance-query-example[]
List<CreditCardPayment> payments = session.createSQLQuery(
List<CreditCardPayment> payments = session.createNativeQuery(
"SELECT * " +
"FROM Payment p " +
"JOIN CreditCardPayment cp on cp.id = p.id" )
@ -579,7 +579,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-query-parameters-example[]
List<Person> persons = session.createSQLQuery(
List<Person> persons = session.createNativeQuery(
"SELECT * " +
"FROM Person " +
"WHERE name like :name" )