From 5ea40ce3f5ff3a122ff168bc6b3301cc04b241dd Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Thu, 23 May 2013 16:09:28 -0700 Subject: [PATCH] HHH-8266 Binding of named-stored-procedure XML element tries to create duplicate --- .../org/hibernate/cfg/AnnotationBinder.java | 17 +++++++++-------- .../java/org/hibernate/cfg/Configuration.java | 18 +++++++++++++++--- .../main/java/org/hibernate/cfg/Mappings.java | 11 +++++++++++ .../hibernate/cfg/annotations/QueryBinder.java | 9 +++++++-- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 875cc3d769..3d982551b6 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -272,7 +272,7 @@ public final class AnnotationBinder { (List) defaults.get( NamedStoredProcedureQuery.class ); if ( annotations != null ) { for ( NamedStoredProcedureQuery annotation : annotations ) { - bindNamedStoredProcedureQuery( mappings, annotation ); + bindNamedStoredProcedureQuery( mappings, annotation, true ); } } } @@ -281,7 +281,7 @@ public final class AnnotationBinder { (List) defaults.get( NamedStoredProcedureQueries.class ); if ( annotations != null ) { for ( NamedStoredProcedureQueries annotation : annotations ) { - bindNamedStoredProcedureQueries( mappings, annotation ); + bindNamedStoredProcedureQueries( mappings, annotation, true ); } } } @@ -392,26 +392,27 @@ public final class AnnotationBinder { } // NamedStoredProcedureQuery handling ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - bindNamedStoredProcedureQuery( mappings, annotatedElement.getAnnotation( NamedStoredProcedureQuery.class ) ); + bindNamedStoredProcedureQuery( mappings, annotatedElement.getAnnotation( NamedStoredProcedureQuery.class ), false ); // NamedStoredProcedureQueries handling ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bindNamedStoredProcedureQueries( mappings, - annotatedElement.getAnnotation( NamedStoredProcedureQueries.class ) + annotatedElement.getAnnotation( NamedStoredProcedureQueries.class ), + false ); } - private static void bindNamedStoredProcedureQueries(Mappings mappings, NamedStoredProcedureQueries annotation) { + private static void bindNamedStoredProcedureQueries(Mappings mappings, NamedStoredProcedureQueries annotation, boolean isDefault) { if ( annotation != null ) { for ( NamedStoredProcedureQuery queryAnnotation : annotation.value() ) { - bindNamedStoredProcedureQuery( mappings, queryAnnotation ); + bindNamedStoredProcedureQuery( mappings, queryAnnotation, isDefault ); } } } - private static void bindNamedStoredProcedureQuery(Mappings mappings, NamedStoredProcedureQuery annotation) { + private static void bindNamedStoredProcedureQuery(Mappings mappings, NamedStoredProcedureQuery annotation, boolean isDefault) { if ( annotation != null ) { - QueryBinder.bindNamedStoredProcedureQuery( annotation, mappings ); + QueryBinder.bindNamedStoredProcedureQuery( annotation, mappings, isDefault ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java index 83a1949894..ef240e4cb7 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java @@ -263,6 +263,8 @@ public class Configuration implements Serializable { private Set defaultNamedQueryNames; private Set defaultNamedNativeQueryNames; private Set defaultSqlResultSetMappingNames; + private Set defaultNamedProcedure; + private Set defaultNamedGenerators; private Map generatorTables; private Map> uniqueConstraintHoldersByTable; @@ -339,6 +341,7 @@ public class Configuration implements Serializable { defaultNamedQueryNames = new HashSet(); defaultNamedNativeQueryNames = new HashSet(); defaultSqlResultSetMappingNames = new HashSet(); + defaultNamedProcedure = new HashSet( ); defaultNamedGenerators = new HashSet(); uniqueConstraintHoldersByTable = new HashMap>(); jpaIndexHoldersByTable = new HashMap>( ); @@ -2890,16 +2893,25 @@ public class Configuration implements Serializable { public void addNamedProcedureCallDefinition(NamedProcedureCallDefinition definition) throws DuplicateMappingException { final String name = definition.getRegisteredName(); - final NamedProcedureCallDefinition previous = namedProcedureCallMap.put( name, definition ); - if ( previous != null ) { - throw new DuplicateMappingException( "named stored procedure query", name ); + if ( !defaultNamedProcedure.contains( name ) ) { + final NamedProcedureCallDefinition previous = namedProcedureCallMap.put( name, definition ); + if ( previous != null ) { + throw new DuplicateMappingException( "named stored procedure query", name ); + } } } + @Override + public void addDefaultNamedProcedureCallDefinition(NamedProcedureCallDefinition definition) + throws DuplicateMappingException { + addNamedProcedureCallDefinition( definition ); + defaultNamedProcedure.add( definition.getRegisteredName() ); + } @Override public void addNamedEntityGraphDefintion(NamedEntityGraphDefinition definition) throws DuplicateMappingException { final String name = definition.getRegisteredName(); + final NamedEntityGraphDefinition previous = namedEntityGraphMap.put( name, definition ); if ( previous != null ) { throw new DuplicateMappingException( "NamedEntityGraph", name ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Mappings.java b/hibernate-core/src/main/java/org/hibernate/cfg/Mappings.java index 6ea34e60b1..07c2327777 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Mappings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Mappings.java @@ -349,6 +349,17 @@ public interface Mappings { */ public void addNamedProcedureCallDefinition(NamedProcedureCallDefinition definition) throws DuplicateMappingException; + /** + * Adds metadata for a named stored procedure call to this repository. + * + * @param definition The procedure call information + * + * @throws DuplicateMappingException If a query already exists with that name. + */ + public void addDefaultNamedProcedureCallDefinition(NamedProcedureCallDefinition definition) throws DuplicateMappingException; + + + /** * Adds metadata for a named entity graph to this repository * diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/QueryBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/QueryBinder.java index 28f8a0cdd9..ddeefb6ca9 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/QueryBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/QueryBinder.java @@ -318,7 +318,7 @@ public abstract class QueryBinder { } } - public static void bindNamedStoredProcedureQuery(NamedStoredProcedureQuery annotation, Mappings mappings) { + public static void bindNamedStoredProcedureQuery(NamedStoredProcedureQuery annotation, Mappings mappings, boolean isDefault) { if ( annotation == null ) { return; } @@ -328,7 +328,12 @@ public abstract class QueryBinder { } final NamedProcedureCallDefinition def = new NamedProcedureCallDefinition( annotation ); - mappings.addNamedProcedureCallDefinition( def ); + + if(isDefault){ + mappings.addDefaultNamedProcedureCallDefinition( def ); + } else{ + mappings.addNamedProcedureCallDefinition( def ); + } LOG.debugf( "Bound named stored procedure query : %s => %s", def.getRegisteredName(), def.getProcedureName() ); }