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