Skip to content

Fix support for @SQLSelect #1721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.query.named.NamedQueryMemento;
import org.hibernate.query.spi.QueryImplementor;
import org.hibernate.reactive.loader.ast.spi.ReactiveSingleIdEntityLoader;
import org.hibernate.reactive.query.ReactiveSelectionQuery;

import jakarta.persistence.Parameter;

Expand Down Expand Up @@ -51,7 +52,7 @@ public CompletionStage<T> load(Object pkValue, LockOptions lockOptions, Boolean
query.setParameter( (Parameter<Object>) query.getParameters().iterator().next(), pkValue );
query.setHibernateFlushMode( FlushMode.MANUAL );

return completedFuture( query.uniqueResult() );
return ( (ReactiveSelectionQuery) query ).reactiveUnique();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ReactiveNamedObjectRepositoryImpl(NamedObjectRepository delegate) {

@Override
public NamedSqmQueryMemento getSqmQueryMemento(String queryName) {
return wrap( delegate.getSqmQueryMemento( queryName ) );
return wrapSqmQueryMemento( delegate.getSqmQueryMemento( queryName ) );
}

@Override
Expand All @@ -47,7 +47,7 @@ public void registerSqmQueryMemento(String name, NamedSqmQueryMemento descriptor

@Override
public NamedNativeQueryMemento getNativeQueryMemento(String queryName) {
return wrap( delegate.getNativeQueryMemento( queryName ) );
return wrapNativeQueryMemento( delegate.getNativeQueryMemento( queryName ) );
}

@Override
Expand Down Expand Up @@ -105,7 +105,7 @@ public NamedQueryMemento resolve(
SessionFactoryImplementor sessionFactory,
MetadataImplementor bootMetamodel,
String registrationName) {
return delegate.resolve( sessionFactory, bootMetamodel, registrationName );
return wrap(delegate.resolve( sessionFactory, bootMetamodel, registrationName ));
}

@Override
Expand All @@ -118,7 +118,17 @@ public void close() {
delegate.close();
}

private static NamedSqmQueryMemento wrap(final NamedSqmQueryMemento sqmQueryMemento) {
private static NamedQueryMemento wrap(final NamedQueryMemento namedQueryMemento) {
if ( namedQueryMemento == null ) {
return null;
} else if( namedQueryMemento instanceof NamedSqmQueryMemento ) {
return wrapSqmQueryMemento( (NamedSqmQueryMemento) namedQueryMemento );
} else {
return wrapNativeQueryMemento( (NamedNativeQueryMemento) namedQueryMemento );
}
}

private static NamedSqmQueryMemento wrapSqmQueryMemento(final NamedSqmQueryMemento sqmQueryMemento) {
if ( sqmQueryMemento == null ) {
return null;
}
Expand All @@ -131,7 +141,7 @@ else if ( sqmQueryMemento instanceof ReactiveNamedSqmQueryMemento ) {
}
}

private static NamedNativeQueryMemento wrap(final NamedNativeQueryMemento nativeQueryMemento) {
private static NamedNativeQueryMemento wrapNativeQueryMemento(final NamedNativeQueryMemento nativeQueryMemento) {
if ( nativeQueryMemento == null ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* 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.Collection;
import java.util.List;
import java.util.Objects;

import org.hibernate.annotations.SQLSelect;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.reactive.testing.DBSelectionExtension;
import org.hibernate.reactive.testing.SqlStatementTracker;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.vertx.junit5.VertxTestContext;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.reactive.SQLSelectTest.Person.SELECT_QUERY;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL;
import static org.hibernate.reactive.testing.DBSelectionExtension.runOnlyFor;

public class SQLSelectTest extends BaseReactiveTest {
@RegisterExtension // We use native queries, which may be different for other DBs
public DBSelectionExtension dbSelection = runOnlyFor( POSTGRESQL );

private SqlStatementTracker sqlTracker;

private Person thePerson;

@Override
protected Collection<Class<?>> annotatedEntities() {
return List.of( Person.class );
}

@Override
protected Configuration constructConfiguration() {
Configuration configuration = super.constructConfiguration();
sqlTracker = new SqlStatementTracker( SQLSelectTest::doCheckQuery, configuration.getProperties() );
return configuration;
}

private static boolean doCheckQuery(String s) {
return s.toLowerCase().startsWith( "select" );
}

@Override
protected void addServices(StandardServiceRegistryBuilder builder) {
sqlTracker.registerService( builder );
}

@BeforeEach
public void populateDb(VertxTestContext context) {
thePerson = new Person();
thePerson.id = 724200;
thePerson.name = "Claude";

test( context, getMutinySessionFactory().withTransaction( s -> s.persist( thePerson ) ) );
}

@Test
public void findEntity(VertxTestContext context) {
test( context, openSession()
.thenCompose( session -> session.find( Person.class, thePerson.getId() ) )
.thenAccept( found -> {
assertThat( found ).isEqualTo( thePerson );
assertThat( sqlTracker.getLoggedQueries() )
.containsExactly(
"select version()",
SELECT_QUERY.replace( "?", "$1" )
);
} )
);
}


@Entity(name = "Person")
@SQLSelect(sql = SELECT_QUERY)
static class Person {
// Query containing simple where check to distinguish from a possible generated query
public static final String SELECT_QUERY = "SELECT id, name FROM person WHERE id = ? and 'hreact' = 'hreact'";

@Id
private int id;

private String name;

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;
}

@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
Person person = (Person) o;
return Objects.equals( name, person.name );
}

@Override
public int hashCode() {
return Objects.hash( name );
}
}
}