mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-03-02 07:49:23 +00:00
HHH-14796 Cannot replace an existing JPQL NamedQuery with a native NamedQuery
This commit is contained in:
parent
859280f297
commit
f2db7302c9
@ -92,6 +92,14 @@ public ResultSetMappingDefinition getResultSetMappingDefinition(String mappingNa
|
|||||||
return namedSqlResultSetMappingMap.get( mappingName );
|
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) {
|
public synchronized void registerNamedQueryDefinition(String name, NamedQueryDefinition definition) {
|
||||||
if ( NamedSQLQueryDefinition.class.isInstance( definition ) ) {
|
if ( NamedSQLQueryDefinition.class.isInstance( definition ) ) {
|
||||||
throw new IllegalArgumentException( "NamedSQLQueryDefinition instance incorrectly passed to registerNamedQueryDefinition" );
|
throw new IllegalArgumentException( "NamedSQLQueryDefinition instance incorrectly passed to registerNamedQueryDefinition" );
|
||||||
@ -112,6 +120,15 @@ public synchronized void registerNamedQueryDefinition(String name, NamedQueryDef
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.namedQueryDefinitionMap = toSmallMap( copy );
|
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) {
|
public synchronized void registerNamedSQLQueryDefinition(String name, NamedSQLQueryDefinition definition) {
|
||||||
@ -130,6 +147,7 @@ public synchronized void registerNamedSQLQueryDefinition(String name, NamedSQLQu
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.namedSqlQueryDefinitionMap = toSmallMap( copy );
|
this.namedSqlQueryDefinitionMap = toSmallMap( copy );
|
||||||
|
removeNamedQueryDefinition( name );
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void registerNamedProcedureCallMemento(String name, ProcedureCallMemento memento) {
|
public synchronized void registerNamedProcedureCallMemento(String name, ProcedureCallMemento memento) {
|
||||||
|
@ -54,6 +54,41 @@ public void basicTest() {
|
|||||||
em.close();
|
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
|
@Test
|
||||||
public void testLockModeHandling() {
|
public void testLockModeHandling() {
|
||||||
final String name = "lock-mode-handling";
|
final String name = "lock-mode-handling";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user