Skip to content

Commit

Permalink
HHH-8266 Binding of named-stored-procedure XML element tries to creat…
Browse files Browse the repository at this point in the history
…e duplicate
  • Loading branch information
stliu committed May 23, 2013
1 parent 7678fae commit 5ea40ce
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
Expand Up @@ -272,7 +272,7 @@ public static void bindDefaults(Mappings mappings) {
(List<NamedStoredProcedureQuery>) defaults.get( NamedStoredProcedureQuery.class );
if ( annotations != null ) {
for ( NamedStoredProcedureQuery annotation : annotations ) {
bindNamedStoredProcedureQuery( mappings, annotation );
bindNamedStoredProcedureQuery( mappings, annotation, true );
}
}
}
Expand All @@ -281,7 +281,7 @@ public static void bindDefaults(Mappings mappings) {
(List<NamedStoredProcedureQueries>) defaults.get( NamedStoredProcedureQueries.class );
if ( annotations != null ) {
for ( NamedStoredProcedureQueries annotation : annotations ) {
bindNamedStoredProcedureQueries( mappings, annotation );
bindNamedStoredProcedureQueries( mappings, annotation, true );
}
}
}
Expand Down Expand Up @@ -392,26 +392,27 @@ private static void bindQueries(XAnnotatedElement annotatedElement, Mappings map
}

// 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 );
}
}

Expand Down
18 changes: 15 additions & 3 deletions hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java
Expand Up @@ -263,6 +263,8 @@ public class Configuration implements Serializable {
private Set<String> defaultNamedQueryNames;
private Set<String> defaultNamedNativeQueryNames;
private Set<String> defaultSqlResultSetMappingNames;
private Set<String> defaultNamedProcedure;

private Set<String> defaultNamedGenerators;
private Map<String, Properties> generatorTables;
private Map<Table, List<UniqueConstraintHolder>> uniqueConstraintHoldersByTable;
Expand Down Expand Up @@ -339,6 +341,7 @@ protected void reset() {
defaultNamedQueryNames = new HashSet<String>();
defaultNamedNativeQueryNames = new HashSet<String>();
defaultSqlResultSetMappingNames = new HashSet<String>();
defaultNamedProcedure = new HashSet<String>( );
defaultNamedGenerators = new HashSet<String>();
uniqueConstraintHoldersByTable = new HashMap<Table, List<UniqueConstraintHolder>>();
jpaIndexHoldersByTable = new HashMap<Table,List<JPAIndexHolder>>( );
Expand Down Expand Up @@ -2890,16 +2893,25 @@ private void applySQLQuery(String name, NamedSQLQueryDefinition query) throws Du
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 );
Expand Down
11 changes: 11 additions & 0 deletions hibernate-core/src/main/java/org/hibernate/cfg/Mappings.java
Expand Up @@ -349,6 +349,17 @@ public Table addDenormalizedTable(String schema, String catalog, String name, bo
*/
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
*
Expand Down
Expand Up @@ -318,7 +318,7 @@ public static void bindQueries(org.hibernate.annotations.NamedQueries queriesAnn
}
}

public static void bindNamedStoredProcedureQuery(NamedStoredProcedureQuery annotation, Mappings mappings) {
public static void bindNamedStoredProcedureQuery(NamedStoredProcedureQuery annotation, Mappings mappings, boolean isDefault) {
if ( annotation == null ) {
return;
}
Expand All @@ -328,7 +328,12 @@ public static void bindNamedStoredProcedureQuery(NamedStoredProcedureQuery annot
}

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() );
}

Expand Down

0 comments on commit 5ea40ce

Please sign in to comment.