HHH-18649 test

This commit is contained in:
Gavin King 2024-09-22 23:37:25 +02:00
parent efc50aad00
commit ad33eb4f64
3 changed files with 68 additions and 1 deletions

View File

@ -21,7 +21,6 @@ import org.hibernate.orm.test.jpa.metamodel.Customer_;
import org.hibernate.orm.test.jpa.metamodel.Order;
import org.hibernate.orm.test.jpa.metamodel.Order_;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.AfterEach;

View File

@ -0,0 +1,33 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.jpa.metadata;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.NamedEntityGraph;
import jakarta.persistence.NamedQuery;
import java.time.LocalDateTime;
@NamedQuery(name = "TextById",
query = "select text from Record where id = ?1")
@NamedQuery(name = "AllRecords",
query = "from Record order by timestamp, id")
@NamedQuery(name = "AllRecordsAsTuples",
query = "select id, text from Record order by timestamp, id")
@NamedEntityGraph(name = "CompleteRecord",
includeAllAttributes = true)
@Entity
public class Record {
@Id @GeneratedValue
String id;
String text;
LocalDateTime timestamp = LocalDateTime.now();
Record() {}
public Record(String text) {
this.text = text;
}
}

View File

@ -0,0 +1,35 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.jpa.metadata;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Jpa(annotatedClasses = Record.class)
public class TypesafeNamedQueryTest {
@Test void test(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
Record record1 = new Record("Hello, World!");
Record record2 = new Record("Goodbye!");
entityManager.persist(record1);
entityManager.persist(record2);
String text = entityManager.createQuery(Record_._TextById_).setParameter(1, record1.id).getSingleResult();
assertEquals("Hello, World!", text);
List<Record> all = entityManager.createQuery(Record_._AllRecords_).getResultList();
assertEquals(2, all.size());
List<Object[]> tuples = entityManager.createQuery(Record_._AllRecordsAsTuples_).getResultList();
assertEquals(2, all.size());
assertEquals(tuples.get(0)[0], record1.id);
assertEquals(tuples.get(1)[1], record2.text);
Record record = entityManager.find(Record_._CompleteRecord, record2.id);
assertEquals("Goodbye!", record.text);
});
}
}