HHH-16077 - Added named native queries cannot specify result-class

This commit is contained in:
Steve Ebersole 2023-01-20 15:47:37 -06:00
parent ba53bc4dad
commit bab25b42e4
1 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,101 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.query.sql;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.hibernate.query.spi.QueryImplementor;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.FailureExpected;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
/**
* @author Steve Ebersole
*/
@RequiresDialect(H2Dialect.class)
@DomainModel
@SessionFactory
public class NativeQueryAsNamedTests {
private static final String THE_CREATE = "create table ce_generic_data ( " +
" parent_id bigint, " +
" parent_type varchar(50), " +
" generic_data_definition_id bigint, " +
" creator_id bigint" +
")";
private static final String THE_SELECT = "select parent_id " +
"from ce_generic_data " +
"where parent_id > ?2 " +
" and parent_type = ?1 " +
" and generic_data_definition_id < ?2 " +
" and creator_id <> ?3";
/**
* Port of the test attached on HHH-16068
*/
@Test
@JiraKey( "HHH-16068" )
public void testParameters(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final NativeQuery<String> nativeQuery = session.createNativeQuery(THE_SELECT);
bindParameters( nativeQuery, session );
nativeQuery.list();
// now add it as a named query
session.getSessionFactory().addNamedQuery( "the_select", nativeQuery );
// and execute it as a named query
final QueryImplementor<String> namedQuery = session.createNamedQuery( "the_select" );
bindParameters( namedQuery, session );
namedQuery.list();
} );
}
/**
* Seems like this should work, but currently does not
*/
@Test
@FailureExpected( reason = "Session#createNamedQuery for a native-query does not like passing the result-class" )
public void testResultClass(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final NativeQuery<String> nativeQuery = session.createNativeQuery( THE_SELECT, String.class );
bindParameters( nativeQuery, session );
nativeQuery.list();
// now add it as a named query
session.getSessionFactory().addNamedQuery( "the_select", nativeQuery );
// and execute it as a named query
final QueryImplementor<String> namedQuery = session.createNamedQuery( "the_select", String.class );
bindParameters( namedQuery, session );
namedQuery.list();
} );
}
private void bindParameters(Query<?> query, SessionImplementor session) {
query.setParameter(1, "98C1");
query.setParameter(2, 1000L);
query.setParameter(3, 2000L);
}
@BeforeAll
public void createSchema(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
session.doWork( (connection) -> {
session.createNativeMutationQuery( THE_CREATE ).executeUpdate();
} );
} );
}
}