HHH-14796 Cannot replace an existing JPQL NamedQuery with a native NamedQuery

This commit is contained in:
rmartinc 2021-09-02 10:55:40 +02:00 committed by Sanne Grinovero
parent 859280f297
commit f2db7302c9
2 changed files with 53 additions and 0 deletions

View File

@ -92,6 +92,14 @@ public class NamedQueryRepository {
return namedSqlResultSetMappingMap.get( mappingName );
}
private synchronized void removeNamedQueryDefinition(String name) {
if ( this.namedQueryDefinitionMap.containsKey( name ) ) {
final Map<String, NamedQueryDefinition> copy = CollectionHelper.makeCopy( namedQueryDefinitionMap );
copy.remove( name );
this.namedQueryDefinitionMap = toSmallMap( copy );
}
}
public synchronized void registerNamedQueryDefinition(String name, NamedQueryDefinition definition) {
if ( NamedSQLQueryDefinition.class.isInstance( definition ) ) {
throw new IllegalArgumentException( "NamedSQLQueryDefinition instance incorrectly passed to registerNamedQueryDefinition" );
@ -112,6 +120,15 @@ public class NamedQueryRepository {
}
this.namedQueryDefinitionMap = toSmallMap( copy );
removeNamedSQLQueryDefinition( name );
}
private synchronized void removeNamedSQLQueryDefinition(String name) {
if ( this.namedSqlQueryDefinitionMap.containsKey( name ) ) {
final Map<String, NamedSQLQueryDefinition> copy = CollectionHelper.makeCopy( namedSqlQueryDefinitionMap );
copy.remove( name );
this.namedSqlQueryDefinitionMap = toSmallMap( copy );
}
}
public synchronized void registerNamedSQLQueryDefinition(String name, NamedSQLQueryDefinition definition) {
@ -130,6 +147,7 @@ public class NamedQueryRepository {
}
this.namedSqlQueryDefinitionMap = toSmallMap( copy );
removeNamedQueryDefinition( name );
}
public synchronized void registerNamedProcedureCallMemento(String name, ProcedureCallMemento memento) {

View File

@ -54,6 +54,41 @@ public class AddNamedQueryTest extends BaseEntityManagerFunctionalTestCase {
em.close();
}
@Test
public void replaceTest() {
EntityManager em = getOrCreateEntityManager();
final String name = "myReplaceItemQuery";
// create a jpql query
String sql = "from Item";
Query query = em.createQuery( sql );
query.setHint( "org.hibernate.comment", sql );
em.getEntityManagerFactory().addNamedQuery( name, query );
query = em.createNamedQuery( name );
assertEquals( sql, query.getHints().get( "org.hibernate.comment" ) );
assertEquals( 0, query.getResultList().size() );
// create a native query and replace the previous jpql
sql = "select * from Item";
query = em.createNativeQuery( sql, Item.class );
query.setHint( "org.hibernate.comment", sql );
em.getEntityManagerFactory().addNamedQuery( name, query );
query = em.createNamedQuery( name );
assertEquals( sql, query.getHints().get( "org.hibernate.comment" ) );
assertEquals( 0, query.getResultList().size() );
// define back a named query
sql = "from Item";
query = em.createQuery( sql );
query.setHint( "org.hibernate.comment", sql );
em.getEntityManagerFactory().addNamedQuery( name, query );
query = em.createNamedQuery( name );
assertEquals( sql, query.getHints().get( "org.hibernate.comment" ) );
assertEquals( 0, query.getResultList().size() );
em.close();
}
@Test
public void testLockModeHandling() {
final String name = "lock-mode-handling";