Skip to content

Commit

Permalink
HHH-17566 Test addNamedQuery for native queries targeting entities
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere authored and beikov committed Dec 15, 2023
1 parent 0c2ba82 commit 772af0d
Showing 1 changed file with 69 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
*/
package org.hibernate.orm.test.jpa.query;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.jpa.HibernateHints.HINT_NATIVE_LOCK_MODE;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import java.util.List;

import org.hibernate.LockMode;
Expand All @@ -14,6 +20,7 @@
import org.hibernate.query.NativeQuery;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.JiraKey;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -27,11 +34,7 @@
import jakarta.persistence.NamedQuery;
import jakarta.persistence.Query;
import jakarta.persistence.TypedQuery;

import static org.hibernate.jpa.HibernateHints.HINT_NATIVE_LOCK_MODE;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import org.assertj.core.api.InstanceOfAssertFactories;

/**
* @author Andrea Boriero
Expand Down Expand Up @@ -200,7 +203,8 @@ public void testNamedNativeQueryExceptionNoResultDefined() {
@TestForIssue(jiraKey = "HHH-11413")
public void testNamedQueryAddedFromTypedNativeQuery() {
doInJPA( this::entityManagerFactory, entityManager -> {
final Query query = entityManager.createNativeQuery( "select g.title from Game g where title = ?", String.class );
final Query query = entityManager.createNativeQuery(
"select g.title from Game g where title = ?", String.class );
entityManagerFactory().addNamedQuery( "the-query", query );

final TypedQuery<String> namedQuery = entityManager.createNamedQuery( "the-query", String.class );
Expand All @@ -209,6 +213,65 @@ public void testNamedQueryAddedFromTypedNativeQuery() {
} );
}

@Test
@JiraKey("HHH-17566")
public void testNamedQueryAddedFromEntityNativeQuery() {
// Check that the native query works
doInJPA( this::entityManagerFactory, entityManager -> {
final Query query = entityManager.createNativeQuery(
"select g.* from Game g where title = ?", Game.class );
query.setParameter( 1, "Halo" );
assertThat( (List<?>) query.getResultList() )
.hasSize( 1 )
.element( 0 )
.asInstanceOf( InstanceOfAssertFactories.type( Game.class ) )
.returns( "Halo", Game::getTitle );
} );
// Check corresponding named query can be used as a typed query
doInJPA( this::entityManagerFactory, entityManager -> {
final Query query = entityManager.createNativeQuery(
"select g.* from Game g where title = ?", Game.class );
entityManagerFactory().addNamedQuery( "the-query", query );

final TypedQuery<Game> namedQuery = entityManager.createNamedQuery( "the-query", Game.class );
namedQuery.setParameter( 1, "Halo" );
assertThat( namedQuery.getResultList() )
.hasSize( 1 )
.element( 0 )
.returns( "Halo", Game::getTitle );
} );
}

@Test
@JiraKey("HHH-17566")
public void testNamedQueryAddedFromEntityNativeQueryUsedAsUntyped() {
// Check corresponding named query can be used as an untyped query
doInJPA( this::entityManagerFactory, entityManager -> {
final Query query = entityManager.createNativeQuery(
"select g.* from Game g where title = ?", Game.class );
query.setParameter( 1, "Halo" );
assertThat( (List<?>) query.getResultList() )
.hasSize( 1 )
.element( 0 )
.asInstanceOf( InstanceOfAssertFactories.type( Game.class ) )
.returns( "Halo", Game::getTitle );
} );
// Check naming the native query works
doInJPA( this::entityManagerFactory, entityManager -> {
final Query query = entityManager.createNativeQuery(
"select g.* from Game g where title = ?", Game.class );
entityManagerFactory().addNamedQuery( "the-query", query );

final Query namedQuery = entityManager.createNamedQuery( "the-query" );
namedQuery.setParameter( 1, "Halo" );
assertThat( (List<?>) namedQuery.getResultList() )
.hasSize( 1 )
.element( 0 )
.asInstanceOf( InstanceOfAssertFactories.type( Game.class ) )
.returns( "Halo", Game::getTitle );
} );
}

@Test
@TestForIssue(jiraKey = "HHH-14816")
public void testQueryHintLockMode() {
Expand Down

0 comments on commit 772af0d

Please sign in to comment.