HHH-15914 - remove deprecated method calls and provide an example of Tuple.class usage

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2022-12-22 22:36:20 +01:00 committed by Jan Schatteman
parent a666f98357
commit c45330023f
1 changed files with 37 additions and 40 deletions

View File

@ -9,6 +9,8 @@ package org.hibernate.userguide.sql;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import jakarta.persistence.PersistenceException;
import jakarta.persistence.Tuple;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
@ -18,7 +20,6 @@ import org.hibernate.loader.NonUniqueDiscoveredSqlAliasException;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.proxy.HibernateProxy; import org.hibernate.proxy.HibernateProxy;
import org.hibernate.query.TupleTransformer; import org.hibernate.query.TupleTransformer;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes; import org.hibernate.type.StandardBasicTypes;
import org.hibernate.userguide.model.Account; import org.hibernate.userguide.model.Account;
import org.hibernate.userguide.model.AddressType; import org.hibernate.userguide.model.AddressType;
@ -37,8 +38,6 @@ import org.hibernate.testing.TestForIssue;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import jakarta.persistence.PersistenceException;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
@ -187,7 +186,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
Session session = entityManager.unwrap(Session.class); Session session = entityManager.unwrap(Session.class);
//tag::sql-hibernate-all-columns-scalar-query-example[] //tag::sql-hibernate-all-columns-scalar-query-example[]
List<Object[]> persons = session.createNativeQuery( List<Object[]> persons = session.createNativeQuery(
"SELECT * FROM Person") "SELECT * FROM Person", Object[].class)
.list(); .list();
//end::sql-hibernate-all-columns-scalar-query-example[] //end::sql-hibernate-all-columns-scalar-query-example[]
assertEquals(3, persons.size()); assertEquals(3, persons.size());
@ -200,7 +199,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
Session session = entityManager.unwrap(Session.class); Session session = entityManager.unwrap(Session.class);
//tag::sql-hibernate-custom-column-selection-scalar-query-example[] //tag::sql-hibernate-custom-column-selection-scalar-query-example[]
List<Object[]> persons = session.createNativeQuery( List<Object[]> persons = session.createNativeQuery(
"SELECT id, name FROM Person") "SELECT id, name FROM Person", Object[].class)
.list(); .list();
for(Object[] person : persons) { for(Object[] person : persons) {
@ -218,7 +217,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
Session session = entityManager.unwrap(Session.class); Session session = entityManager.unwrap(Session.class);
//tag::sql-hibernate-scalar-query-explicit-result-set-example[] //tag::sql-hibernate-scalar-query-explicit-result-set-example[]
List<Object[]> persons = session.createNativeQuery( List<Object[]> persons = session.createNativeQuery(
"SELECT * FROM Person") "SELECT * FROM Person", Object[].class)
.addScalar("id", StandardBasicTypes.LONG) .addScalar("id", StandardBasicTypes.LONG)
.addScalar("name", StandardBasicTypes.STRING) .addScalar("name", StandardBasicTypes.STRING)
.list(); .list();
@ -239,7 +238,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
Session session = entityManager.unwrap(Session.class); Session session = entityManager.unwrap(Session.class);
//tag::sql-hibernate-scalar-query-partial-explicit-result-set-example[] //tag::sql-hibernate-scalar-query-partial-explicit-result-set-example[]
List<Object[]> persons = session.createNativeQuery( List<Object[]> persons = session.createNativeQuery(
"SELECT * FROM Person") "SELECT * FROM Person", Object[].class)
.addScalar("id", StandardBasicTypes.LONG) .addScalar("id", StandardBasicTypes.LONG)
.addScalar("name") .addScalar("name")
.list(); .list();
@ -271,8 +270,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
Session session = entityManager.unwrap(Session.class); Session session = entityManager.unwrap(Session.class);
//tag::sql-hibernate-entity-query-example[] //tag::sql-hibernate-entity-query-example[]
List<Person> persons = session.createNativeQuery( List<Person> persons = session.createNativeQuery(
"SELECT * FROM Person") "SELECT * FROM Person", Person.class)
.addEntity(Person.class)
.list(); .list();
//end::sql-hibernate-entity-query-example[] //end::sql-hibernate-entity-query-example[]
assertEquals(3, persons.size()); assertEquals(3, persons.size());
@ -299,8 +297,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
//tag::sql-hibernate-entity-query-explicit-result-set-example[] //tag::sql-hibernate-entity-query-explicit-result-set-example[]
List<Person> persons = session.createNativeQuery( List<Person> persons = session.createNativeQuery(
"SELECT id, name, nickName, address, createdOn, version " + "SELECT id, name, nickName, address, createdOn, version " +
"FROM Person") "FROM Person", Person.class)
.addEntity(Person.class)
.list(); .list();
//end::sql-hibernate-entity-query-explicit-result-set-example[] //end::sql-hibernate-entity-query-explicit-result-set-example[]
assertEquals(3, persons.size()); assertEquals(3, persons.size());
@ -327,8 +324,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
//tag::sql-hibernate-entity-associations-query-many-to-one-example[] //tag::sql-hibernate-entity-associations-query-many-to-one-example[]
List<Phone> phones = session.createNativeQuery( List<Phone> phones = session.createNativeQuery(
"SELECT id, phone_number, phone_type, person_id " + "SELECT id, phone_number, phone_type, person_id " +
"FROM Phone") "FROM Phone", Phone.class)
.addEntity(Phone.class)
.list(); .list();
//end::sql-hibernate-entity-associations-query-many-to-one-example[] //end::sql-hibernate-entity-associations-query-many-to-one-example[]
assertEquals(3, phones.size()); assertEquals(3, phones.size());
@ -359,8 +355,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
List<Phone> tuples = session.createNativeQuery( List<Phone> tuples = session.createNativeQuery(
"SELECT {ph.*}, {pr.*} " + "SELECT {ph.*}, {pr.*} " +
"FROM Phone ph " + "FROM Phone ph " +
"JOIN Person pr ON ph.person_id = pr.id") "JOIN Person pr ON ph.person_id = pr.id", Phone.class, "ph")
.addEntity("ph", Phone.class)
.addJoin("pr", "ph.person") .addJoin("pr", "ph.person")
.list(); .list();
@ -380,8 +375,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
List<Phone> phones = session.createNativeQuery( List<Phone> phones = session.createNativeQuery(
"SELECT {ph.*}, {pr.*} " + "SELECT {ph.*}, {pr.*} " +
"FROM Phone ph " + "FROM Phone ph " +
"JOIN Person pr ON ph.person_id = pr.id") "JOIN Person pr ON ph.person_id = pr.id", Phone.class, "ph")
.addEntity("ph", Phone.class)
.addJoin("pr", "ph.person") .addJoin("pr", "ph.person")
.setTupleTransformer( (TupleTransformer<Phone>) (tuple, aliases) -> (Phone) tuple[0] ) .setTupleTransformer( (TupleTransformer<Phone>) (tuple, aliases) -> (Phone) tuple[0] )
.list(); .list();
@ -423,8 +417,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
List<Phone> phones = session.createNativeQuery( List<Phone> phones = session.createNativeQuery(
"SELECT {ph.*}, {c.*} " + "SELECT {ph.*}, {c.*} " +
"FROM Phone ph " + "FROM Phone ph " +
"JOIN phone_call c ON c.phone_id = ph.id") "JOIN phone_call c ON c.phone_id = ph.id", Phone.class, "ph")
.addEntity("ph", Phone.class)
.addJoin("c", "ph.calls") .addJoin("c", "ph.calls")
.list(); .list();
@ -446,8 +439,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
List<Phone> tuples = session.createNativeQuery( List<Phone> tuples = session.createNativeQuery(
"SELECT {ph.*}, {c.*} " + "SELECT {ph.*}, {c.*} " +
"FROM Phone ph " + "FROM Phone ph " +
"JOIN phone_call c ON c.phone_id = ph.id") "JOIN phone_call c ON c.phone_id = ph.id", Phone.class, "ph")
.addEntity("ph", Phone.class)
.addJoin("c", "ph.calls") .addJoin("c", "ph.calls")
.list(); .list();
@ -511,7 +503,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
List<Object> entities = session.createNativeQuery( List<Object> entities = session.createNativeQuery(
"SELECT {pr.*}, {pt.*} " + "SELECT {pr.*}, {pt.*} " +
"FROM Person pr, Partner pt " + "FROM Person pr, Partner pt " +
"WHERE pr.name = pt.name") "WHERE pr.name = pt.name", Object.class)
.addEntity("pr", Person.class) .addEntity("pr", Person.class)
.addEntity("pt", Partner.class) .addEntity("pt", Partner.class)
.list(); .list();
@ -527,8 +519,15 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
//tag::sql-hibernate-dto-query-example[] //tag::sql-hibernate-dto-query-example[]
List<PersonSummaryDTO> dtos = session.createNativeQuery( List<PersonSummaryDTO> dtos = session.createNativeQuery(
"SELECT p.id as \"id\", p.name as \"name\" " + "SELECT p.id as \"id\", p.name as \"name\" " +
"FROM Person p") "FROM Person p", Tuple.class)
.setResultTransformer(Transformers.aliasToBean(PersonSummaryDTO.class)) .setTupleTransformer(
(tuple, aliases) -> {
PersonSummaryDTO dto = new PersonSummaryDTO();
dto.setId( (Long)tuple[0] );
dto.setName( (String)tuple[1] );
return dto;
}
)
.list(); .list();
//end::sql-hibernate-dto-query-example[] //end::sql-hibernate-dto-query-example[]
assertEquals(3, dtos.size()); assertEquals(3, dtos.size());
@ -543,8 +542,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
List<CreditCardPayment> payments = session.createNativeQuery( List<CreditCardPayment> payments = session.createNativeQuery(
"SELECT * " + "SELECT * " +
"FROM Payment p " + "FROM Payment p " +
"JOIN CreditCardPayment cp on cp.id = p.id") "JOIN CreditCardPayment cp on cp.id = p.id", CreditCardPayment.class)
.addEntity(CreditCardPayment.class)
.list(); .list();
//end::sql-hibernate-inheritance-query-example[] //end::sql-hibernate-inheritance-query-example[]
assertEquals(1, payments.size()); assertEquals(1, payments.size());
@ -574,8 +572,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
List<Person> persons = session.createNativeQuery( List<Person> persons = session.createNativeQuery(
"SELECT * " + "SELECT * " +
"FROM Person " + "FROM Person " +
"WHERE name like :name") "WHERE name like :name", Person.class)
.addEntity(Person.class)
.setParameter("name", "J%") .setParameter("name", "J%")
.list(); .list();
//end::sql-hibernate-query-parameters-example[] //end::sql-hibernate-query-parameters-example[]
@ -630,8 +627,8 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA(this::entityManagerFactory, entityManager -> { doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class); Session session = entityManager.unwrap(Session.class);
//tag::sql-hibernate-multiple-scalar-values-named-query-example[] //tag::sql-hibernate-multiple-scalar-values-named-query-example[]
List<Object[]> tuples = session.getNamedQuery( List<Object[]> tuples = session.createNamedQuery(
"find_person_name_and_nickName") "find_person_name_and_nickName", Object[].class)
.list(); .list();
for(Object[] tuple : tuples) { for(Object[] tuple : tuples) {
@ -662,8 +659,8 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA(this::entityManagerFactory, entityManager -> { doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class); Session session = entityManager.unwrap(Session.class);
//tag::sql-hibernate-multiple-scalar-values-dto-named-query-example[] //tag::sql-hibernate-multiple-scalar-values-dto-named-query-example[]
List<PersonNames> personNames = session.getNamedQuery( List<PersonNames> personNames = session.createNamedQuery(
"find_person_name_and_nickName_dto") "find_person_name_and_nickName_dto", PersonNames.class)
.list(); .list();
//end::sql-hibernate-multiple-scalar-values-dto-named-query-example[] //end::sql-hibernate-multiple-scalar-values-dto-named-query-example[]
assertEquals(3, personNames.size()); assertEquals(3, personNames.size());
@ -675,8 +672,8 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA(this::entityManagerFactory, entityManager -> { doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class); Session session = entityManager.unwrap(Session.class);
//tag::sql-hibernate-multiple-scalar-values-dto-hibernate-named-query-example[] //tag::sql-hibernate-multiple-scalar-values-dto-hibernate-named-query-example[]
List<PersonPhoneCount> personNames = session.getNamedNativeQuery( List<PersonPhoneCount> personNames = session.createNamedQuery(
"get_person_phone_count") "get_person_phone_count", PersonPhoneCount.class)
.getResultList(); .getResultList();
//end::sql-hibernate-multiple-scalar-values-dto-hibernate-named-query-example[] //end::sql-hibernate-multiple-scalar-values-dto-hibernate-named-query-example[]
assertEquals(2, personNames.size()); assertEquals(2, personNames.size());
@ -705,8 +702,8 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA(this::entityManagerFactory, entityManager -> { doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class); Session session = entityManager.unwrap(Session.class);
//tag::sql-hibernate-entity-named-query-example[] //tag::sql-hibernate-entity-named-query-example[]
List<Person> persons = session.getNamedQuery( List<Person> persons = session.createNamedQuery(
"find_person_by_name") "find_person_by_name", Person.class)
.setParameter("name", "J%") .setParameter("name", "J%")
.list(); .list();
//end::sql-hibernate-entity-named-query-example[] //end::sql-hibernate-entity-named-query-example[]
@ -744,8 +741,8 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA(this::entityManagerFactory, entityManager -> { doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class); Session session = entityManager.unwrap(Session.class);
//tag::sql-hibernate-entity-associations_named-query-example[] //tag::sql-hibernate-entity-associations_named-query-example[]
List<Object[]> tuples = session.getNamedQuery( List<Object[]> tuples = session.createNamedQuery(
"find_person_with_phones_by_name") "find_person_with_phones_by_name", Object[].class)
.setParameter("name", "J%") .setParameter("name", "J%")
.list(); .list();
@ -785,8 +782,8 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA(this::entityManagerFactory, entityManager -> { doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class); Session session = entityManager.unwrap(Session.class);
//tag::sql-hibernate-composite-key-entity-associations_named-query-example[] //tag::sql-hibernate-composite-key-entity-associations_named-query-example[]
List<Object[]> tuples = session.getNamedQuery( List<Object[]> tuples = session.createNamedQuery(
"find_all_spaceships") "find_all_spaceships", Object[].class)
.list(); .list();
for(Object[] tuple : tuples) { for(Object[] tuple : tuples) {