HHH-12621 Copy the query spaces when initializing a new NativeQueryImpl

This could ultimately lead to ConcurrentModificationException.
This commit is contained in:
Guillaume Smet 2018-05-24 16:27:13 +02:00
parent 9081aaf23b
commit dc29e45af3
2 changed files with 21 additions and 2 deletions

View File

@ -82,7 +82,7 @@ public class NativeQueryImpl<T> extends AbstractProducedQuery<T> implements Nati
this.sqlString = queryDef.getQueryString();
this.callable = queryDef.isCallable();
this.querySpaces = queryDef.getQuerySpaces();
this.querySpaces = queryDef.getQuerySpaces() == null ? null : new ArrayList<>( queryDef.getQuerySpaces() );
if ( queryDef.getResultSetRef() != null ) {
ResultSetMappingDefinition definition = session.getFactory()

View File

@ -18,7 +18,7 @@ import javax.persistence.Query;
import org.hibernate.Session;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.query.NativeQuery;
import org.hibernate.testing.TestForIssue;
import org.junit.After;
import org.junit.Before;
@ -159,6 +159,25 @@ public class NamedQueryTest extends BaseEntityManagerFunctionalTestCase {
);
}
@Test
@TestForIssue(jiraKey = "HHH-12621")
public void testNativeQueriesFromNamedQueriesDoNotShareQuerySpaces() {
doInJPA( this::entityManagerFactory, entityManager -> {
Query originalQuery = entityManager.createNativeQuery( "select g from Game g where title = ?1" );
entityManager.getEntityManagerFactory().addNamedQuery( "myQuery", originalQuery );
NativeQuery<?> query1 = entityManager.createNamedQuery( "myQuery" ).unwrap( NativeQuery.class );
query1.addSynchronizedQuerySpace( "newQuerySpace" );
assertEquals( 1, query1.getSynchronizedQuerySpaces().size() );
assertEquals( "newQuerySpace", query1.getSynchronizedQuerySpaces().iterator().next() );
NativeQuery<?> query2 = entityManager.createNamedQuery( "myQuery" ).unwrap( NativeQuery.class );
assertEquals( 0, query2.getSynchronizedQuerySpaces().size() );
} );
}
@Entity(name = "Game")
@NamedQueries(@NamedQuery(name = "NamedQuery", query = "select g from Game g where title = ?1"))
@NamedNativeQueries(@NamedNativeQuery(name = "NamedNativeQuery", query = "select * from Game g where title = ?"))