diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/SQLSelectTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/SQLSelectTest.java new file mode 100644 index 000000000..6c08f85e8 --- /dev/null +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/SQLSelectTest.java @@ -0,0 +1,118 @@ +/* Hibernate, Relational Persistence for Idiomatic Java + * + * SPDX-License-Identifier: Apache-2.0 + * Copyright: Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.reactive; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + + +import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.SQLDeleteAll; +import org.hibernate.annotations.SQLInsert; +import org.hibernate.annotations.SQLSelect; +import org.hibernate.annotations.SQLUpdate; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import io.vertx.junit5.VertxTestContext; +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class SQLSelectTest extends BaseReactiveTest { + + private Person thePerson; + + @Override + protected Collection> annotatedEntities() { + return List.of( Person.class ); + } + + @BeforeEach + public void populateDb(VertxTestContext context) { + List phones = Arrays.asList( "999-999-9999", "111-111-1111", "123-456-7890" ); + thePerson = new Person(); + thePerson.id = 724200; + thePerson.name = "Claude"; + thePerson.phones = phones; + + test( context, getMutinySessionFactory().withTransaction( (s, t) -> s.persist( thePerson ) )); + } + + @Test + public void findEntity(VertxTestContext context) { + test( context, openSession() + .thenCompose( session -> session.find( Person.class, thePerson.getId() ) ) + .thenAccept( found -> assertPhones( found, "999-999-9999", "111-111-1111", "123-456-7890" ) ) + ); + } + + @Test + public void findEntityWithNativeQuery(VertxTestContext context) { + test( context, openSession() + .thenCompose( s -> s.createNativeQuery( + "SELECT id, name FROM person WHERE id = $1", + Person.class + ) + .setParameter( 1, thePerson.getId() ) + .getSingleResult() ) + .thenAccept( found -> assertPhones( found, "999-999-9999", "111-111-1111", "123-456-7890" ) ) + ); + } + + private static void assertPhones(Person person, String... expectedPhones) { + assertNotNull( person ); + assertThat( person.getPhones() ).containsExactlyInAnyOrder( expectedPhones ); + } + + + @Entity(name = "Person") + @SQLInsert(sql = "INSERT INTO person (name, id) VALUES ($1, $2) ") + @SQLUpdate(sql = "UPDATE person SET name = $1 where id = $2 ") + @SQLDelete(sql = "UPDATE person SET WHERE id = $1 ") + @SQLSelect(sql = "SELECT id, name FROM person WHERE id = $1") + static class Person { + + @Id + private int id; + + private String name; + + @ElementCollection(fetch = FetchType.EAGER) + + @SQLInsert(sql = "INSERT INTO person_phones (person_id, phones) VALUES ($1, $2) ") + @SQLDeleteAll(sql = "UPDATE person_phones SET WHERE person_id = $1") + @SQLSelect(sql = "SELECT phones FROM Person_phones WHERE person_id = $1") + private List phones = new ArrayList<>(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getPhones() { + return phones; + } + } +}