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 f6b1e56af5
commit 5758c9e0b5
2 changed files with 54 additions and 0 deletions

View File

@ -90,6 +90,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 = Collections.unmodifiableMap( copy );
}
}
public synchronized void registerNamedQueryDefinition(String name, NamedQueryDefinition definition) {
if ( NamedSQLQueryDefinition.class.isInstance( definition ) ) {
throw new IllegalArgumentException( "NamedSQLQueryDefinition instance incorrectly passed to registerNamedQueryDefinition" );
@ -109,7 +117,17 @@ public class NamedQueryRepository {
);
}
this.namedQueryDefinitionMap = Collections.unmodifiableMap( 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 = Collections.unmodifiableMap( copy );
}
}
public synchronized void registerNamedSQLQueryDefinition(String name, NamedSQLQueryDefinition definition) {
@ -128,6 +146,7 @@ public class NamedQueryRepository {
}
this.namedSqlQueryDefinitionMap = Collections.unmodifiableMap( 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";