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 Christian Beikov
parent 88aa42ec30
commit 8a0198bba9
1 changed files with 36 additions and 38 deletions

View File

@ -10,6 +10,7 @@ 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 +19,6 @@ import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.loader.NonUniqueDiscoveredSqlAliasException;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.userguide.model.Account;
import org.hibernate.userguide.model.AddressType;
@ -184,7 +184,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());
@ -197,7 +197,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) {
@ -215,7 +215,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();
@ -236,7 +236,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();
@ -268,8 +268,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());
@ -296,8 +295,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());
@ -324,8 +322,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());
@ -356,8 +353,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();
@ -377,8 +373,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")
.list();
@ -419,8 +414,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();
@ -442,8 +436,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();
@ -507,7 +500,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();
@ -523,8 +516,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());
@ -539,8 +539,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());
@ -570,8 +569,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[]
@ -626,8 +624,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) {
@ -656,8 +654,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());
@ -669,8 +667,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());
@ -697,8 +695,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[]
@ -735,8 +733,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();
@ -776,8 +774,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) {