From: <hib...@li...> - 2006-04-17 23:15:36
|
Author: epbernard Date: 2006-04-17 19:15:32 -0400 (Mon, 17 Apr 2006) New Revision: 9756 Modified: trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/EJB3OverridenAnnotationReaderTest.java trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Match.java trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/orm.xml Log: Support xml overriding for named(native)queries and sqlresultsetmappings Modified: trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java =================================================================== --- trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java 2006-04-17 19:44:22 UTC (rev 9755) +++ trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java 2006-04-17 23:15:32 UTC (rev 9756) @@ -17,26 +17,36 @@ import javax.persistence.SecondaryTable; import javax.persistence.SecondaryTables; import javax.persistence.Table; -import javax.persistence.UniqueConstraint; import javax.persistence.PrimaryKeyJoinColumns; import javax.persistence.IdClass; import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; import javax.persistence.DiscriminatorValue; import javax.persistence.DiscriminatorColumn; -import javax.persistence.DiscriminatorType; import javax.persistence.SequenceGenerator; import javax.persistence.TableGenerator; +import javax.persistence.NamedQuery; +import javax.persistence.NamedQueries; +import javax.persistence.QueryHint; +import javax.persistence.DiscriminatorType; +import javax.persistence.InheritanceType; +import javax.persistence.UniqueConstraint; +import javax.persistence.NamedNativeQuery; +import javax.persistence.NamedNativeQueries; +import javax.persistence.SqlResultSetMapping; +import javax.persistence.SqlResultSetMappings; +import javax.persistence.EntityResult; +import javax.persistence.FieldResult; +import javax.persistence.ColumnResult; +import org.dom4j.Element; import org.dom4j.Attribute; -import org.dom4j.Element; import org.hibernate.annotationfactory.AnnotationDescriptor; import org.hibernate.annotationfactory.AnnotationFactory; import org.hibernate.reflection.Filter; import org.hibernate.reflection.java.xml.XMLContext; +import org.hibernate.AnnotationException; import org.hibernate.util.StringHelper; import org.hibernate.util.ReflectHelper; -import org.hibernate.AnnotationException; /** * Encapsulates the overriding of Java annotations from an EJB 3.0 descriptor. @@ -47,6 +57,7 @@ */ public class EJB3OverridenAnnotationReader extends JavaAnnotationReader { private static final Map<Class, String> annotationToXml; + private static final String SCHEMA_VALIDATION = "Activate schema validation for more informations"; private static final Filter FILTER = new Filter() { public boolean returnStatic() { return false; @@ -73,6 +84,12 @@ annotationToXml.put( DiscriminatorColumn.class, "discriminator-column" ); annotationToXml.put( SequenceGenerator.class, "sequence-generator" ); annotationToXml.put( TableGenerator.class, "table-generator" ); + annotationToXml.put( NamedQuery.class, "named-query" ); + annotationToXml.put( NamedQueries.class, "named-query" ); + annotationToXml.put( NamedNativeQuery.class, "named-native-query" ); + annotationToXml.put( NamedNativeQueries.class, "named-native-query" ); + annotationToXml.put( SqlResultSetMapping.class, "sql-result-set-mapping" ); + annotationToXml.put( SqlResultSetMappings.class, "sql-result-set-mapping" ); } private XMLContext xmlContext; @@ -175,6 +192,12 @@ if ( current != null ) annotationList.add( current ); current = getTableGenerator( tree, defaults ); if ( current != null ) annotationList.add( current ); + current = getNamedQueries( tree, defaults ); + if ( current != null ) annotationList.add( current ); + current = getNamedNativeQueries( tree, defaults ); + if ( current != null ) annotationList.add( current ); + current = getSqlResultSetMappings( tree, defaults ); + if ( current != null ) annotationList.add( current ); this.annotations = annotationList.toArray( new Annotation[ annotationList.size() ] ); } else if ( propertyName != null ) { @@ -187,17 +210,214 @@ } } + private SqlResultSetMappings getSqlResultSetMappings(Element tree, XMLContext.Default defaults) { + List<SqlResultSetMapping> results = (List<SqlResultSetMapping>) buildSqlResultsetMappings( tree ); + if ( defaults.canUseJavaAnnotations() ) { + SqlResultSetMapping annotation = super.getAnnotation( SqlResultSetMapping.class ); + addSqlResultsetMappingIfNeeded( annotation, results ); + SqlResultSetMappings annotations = super.getAnnotation( SqlResultSetMappings.class ); + if (annotations != null) { + for (SqlResultSetMapping current : annotations.value() ) { + addSqlResultsetMappingIfNeeded( current, results ); + } + } + } + if (results.size() > 0) { + AnnotationDescriptor ad = new AnnotationDescriptor( SqlResultSetMappings.class ); + ad.setValue( "value", results.toArray( new SqlResultSetMapping[ results.size() ] ) ); + return AnnotationFactory.create( ad ); + } + else { + return null; + } + } + + private List<SqlResultSetMapping> buildSqlResultsetMappings(Element element) { + if (element == null) return new ArrayList<SqlResultSetMapping>(); + List resultsetElementList = element.elements( "sql-result-set-mapping" ); + List<SqlResultSetMapping> resultsets = new ArrayList<SqlResultSetMapping>(); + Iterator it = resultsetElementList.listIterator(); + while ( it.hasNext() ) { + Element subelement = (Element) it.next(); + AnnotationDescriptor ann = new AnnotationDescriptor(SqlResultSetMapping.class); + copyStringAttribute( ann, subelement, "name", true ); + List<Element> elements = subelement.elements( "entity-result" ); + List<EntityResult> entityResults = new ArrayList<EntityResult>( elements.size() ); + for (Element entityResult : elements) { + AnnotationDescriptor entityResultDescriptor = new AnnotationDescriptor( EntityResult.class ); + String clazzName = entityResult.attributeValue( "entity-class" ); + if (clazzName == null) + throw new AnnotationException("<entity-result> without entity-class. " + SCHEMA_VALIDATION); + Class clazz = null; + try { + clazz = ReflectHelper.classForName( clazzName, this.getClass() ); + } + catch( ClassNotFoundException e ) { + throw new AnnotationException( "Unable to find entity-class: " + clazzName, e ); + } + entityResultDescriptor.setValue( "entityClass", clazz); + copyStringAttribute( entityResultDescriptor, entityResult, "discriminator-column", false ); + List<FieldResult> fieldResults = new ArrayList<FieldResult>(); + for(Element fieldResult : (List<Element>) entityResult.elements( "field-result" ) ) { + AnnotationDescriptor fieldResultDescriptor = new AnnotationDescriptor( FieldResult.class ); + copyStringAttribute( fieldResultDescriptor, fieldResult, "name", true ); + copyStringAttribute( fieldResultDescriptor, fieldResult, "column", true ); + fieldResults.add( (FieldResult) AnnotationFactory.create( fieldResultDescriptor ) ); + } + entityResultDescriptor.setValue( "fields", fieldResults.toArray( new FieldResult[ fieldResults.size() ] ) ); + entityResults.add( (EntityResult) AnnotationFactory.create( entityResultDescriptor ) ); + } + ann.setValue( "entities", entityResults.toArray( new EntityResult[ entityResults.size() ] ) ); + + elements = subelement.elements( "column-result" ); + List<ColumnResult> columnResults = new ArrayList<ColumnResult>( elements.size() ); + for (Element columnResult : elements) { + AnnotationDescriptor columnResultDescriptor = new AnnotationDescriptor( ColumnResult.class ); + copyStringAttribute( columnResultDescriptor, columnResult, "name", true ); + columnResults.add( (ColumnResult) AnnotationFactory.create( columnResultDescriptor ) ); + } + ann.setValue( "columns", columnResults.toArray( new ColumnResult[ columnResults.size() ] ) ); + copyStringAttribute( ann, subelement, "result-class", false ); + copyStringAttribute( ann, subelement, "result-set-mapping", false ); + resultsets.add( (SqlResultSetMapping) AnnotationFactory.create( ann ) ); + } + return resultsets; + } + + private void addSqlResultsetMappingIfNeeded(SqlResultSetMapping annotation, List<SqlResultSetMapping> resultsets) { + if (annotation != null) { + String resultsetName = annotation.name(); + boolean present = false; + for (SqlResultSetMapping current : resultsets ) { + if ( current.name().equals( resultsetName ) ) { + present = true; + break; + } + } + if (!present) resultsets.add(annotation); + } + } + + private NamedQueries getNamedQueries(Element tree, XMLContext.Default defaults) { + List<NamedQuery> queries = (List<NamedQuery>) buildNamedQueries( tree, false ); + if ( defaults.canUseJavaAnnotations() ) { + NamedQuery annotation = super.getAnnotation( NamedQuery.class ); + addNamedQueryIfNeeded( annotation, queries ); + NamedQueries annotations = super.getAnnotation( NamedQueries.class ); + if (annotations != null) { + for (NamedQuery current : annotations.value() ) { + addNamedQueryIfNeeded( current, queries ); + } + } + } + if (queries.size() > 0) { + AnnotationDescriptor ad = new AnnotationDescriptor( NamedQueries.class ); + ad.setValue( "value", queries.toArray( new NamedQuery[ queries.size() ] ) ); + return AnnotationFactory.create( ad ); + } + else { + return null; + } + } + + private void addNamedQueryIfNeeded(NamedQuery annotation, List<NamedQuery> queries) { + if (annotation != null) { + String queryName = annotation.name(); + boolean present = false; + for (NamedQuery current : queries) { + if ( current.name().equals( queryName ) ) { + present = true; + break; + } + } + if (!present) queries.add(annotation); + } + } + + private NamedNativeQueries getNamedNativeQueries(Element tree, XMLContext.Default defaults) { + List<NamedNativeQuery> queries = (List<NamedNativeQuery>) buildNamedQueries( tree, true ); + if ( defaults.canUseJavaAnnotations() ) { + NamedNativeQuery annotation = super.getAnnotation( NamedNativeQuery.class ); + addNamedNativeQueryIfNeeded( annotation, queries ); + NamedNativeQueries annotations = super.getAnnotation( NamedNativeQueries.class ); + if (annotations != null) { + for (NamedNativeQuery current : annotations.value() ) { + addNamedNativeQueryIfNeeded( current, queries ); + } + } + } + if (queries.size() > 0) { + AnnotationDescriptor ad = new AnnotationDescriptor( NamedNativeQueries.class ); + ad.setValue( "value", queries.toArray( new NamedNativeQuery[ queries.size() ] ) ); + return AnnotationFactory.create( ad ); + } + else { + return null; + } + } + + private void addNamedNativeQueryIfNeeded(NamedNativeQuery annotation, List<NamedNativeQuery> queries) { + if (annotation != null) { + String queryName = annotation.name(); + boolean present = false; + for (NamedNativeQuery current : queries) { + if ( current.name().equals( queryName ) ) { + present = true; + break; + } + } + if (!present) queries.add(annotation); + } + } + + private List buildNamedQueries(Element element, boolean isNative) { + if (element == null) return new ArrayList(); + List namedQueryElementList = isNative ? + element.elements( "named-native-query" ) : + element.elements( "named-query" ); + List namedQueries = new ArrayList(); + int index = 0; + Iterator it = namedQueryElementList.listIterator(); + while ( it.hasNext() ) { + Element subelement = (Element) it.next(); + AnnotationDescriptor ann = new AnnotationDescriptor( + isNative? NamedNativeQuery.class : NamedQuery.class + ); + copyStringAttribute( ann, subelement, "name", false ); + Element queryElt = subelement.element( "query" ); + if (queryElt == null) throw new AnnotationException("No <query> element found." + SCHEMA_VALIDATION); + ann.setValue( "query", queryElt.getTextTrim() ); + List<Element> elements = subelement.elements( "hint" ); + List<QueryHint> queryHints = new ArrayList<QueryHint>( elements.size() ); + for (Element hint : elements) { + AnnotationDescriptor hintDescriptor = new AnnotationDescriptor( QueryHint.class ); + String value = hint.attributeValue( "name" ); + if ( value == null ) throw new AnnotationException("<hint> without name. " + SCHEMA_VALIDATION); + hintDescriptor.setValue( "name", value ); + value = hint.attributeValue( "value" ); + if ( value == null ) throw new AnnotationException("<hint> without value. " + SCHEMA_VALIDATION); + hintDescriptor.setValue( "value", value ); + queryHints.add( (QueryHint) AnnotationFactory.create( hintDescriptor) ); + } + ann.setValue( "hints", queryHints.toArray( new QueryHint[ queryHints.size() ] ) ); + copyStringAttribute( ann, subelement, "result-class", false ); + copyStringAttribute( ann, subelement, "result-set-mapping", false ); + namedQueries.add( AnnotationFactory.create( ann ) ); + } + return namedQueries; + } + private TableGenerator getTableGenerator(Element tree, XMLContext.Default defaults) { Element element = tree != null ? tree.element( annotationToXml.get(TableGenerator.class) ) : null; if ( element != null ) { AnnotationDescriptor ad = new AnnotationDescriptor( TableGenerator.class ); - copyStringAttribute( ad, element, "name" ); - copyStringAttribute( ad, element, "table" ); - copyStringAttribute( ad, element, "catalog" ); - copyStringAttribute( ad, element, "schema" ); - copyStringAttribute( ad, element, "pk-column-name" ); - copyStringAttribute( ad, element, "value-column-name" ); - copyStringAttribute( ad, element, "pk-column-value" ); + copyStringAttribute( ad, element, "name", false ); + copyStringAttribute( ad, element, "table", false ); + copyStringAttribute( ad, element, "catalog", false ); + copyStringAttribute( ad, element, "schema", false ); + copyStringAttribute( ad, element, "pk-column-name", false ); + copyStringAttribute( ad, element, "value-column-name", false ); + copyStringAttribute( ad, element, "pk-column-value", false ); copyIntegerAttribute( ad, element, "initial-value" ); copyIntegerAttribute( ad, element, "allocation-size" ); buildUniqueConstraints( ad, element ); @@ -249,8 +469,8 @@ Element element = tree != null ? tree.element( annotationToXml.get(SequenceGenerator.class) ) : null; if ( element != null ) { AnnotationDescriptor ad = new AnnotationDescriptor( SequenceGenerator.class ); - copyStringAttribute( ad, element, "name" ); - copyStringAttribute( ad, element, "sequence-name" ); + copyStringAttribute( ad, element, "name", false ); + copyStringAttribute( ad, element, "sequence-name", false ); copyIntegerAttribute( ad, element, "initial-value" ); copyIntegerAttribute( ad, element, "allocation-size" ); return AnnotationFactory.create( ad ); @@ -267,8 +487,8 @@ Element element = tree != null ? tree.element( "discriminator-column" ) : null; if ( element != null ) { AnnotationDescriptor ad = new AnnotationDescriptor( DiscriminatorColumn.class ); - copyStringAttribute( ad, element, "name" ); - copyStringAttribute( ad, element, "column-definition" ); + copyStringAttribute( ad, element, "name", false ); + copyStringAttribute( ad, element, "column-definition", false ); String value = element.attributeValue( "discriminator-type" ); DiscriminatorType type = DiscriminatorType.STRING; if (value != null) { @@ -282,7 +502,7 @@ type = DiscriminatorType.INTEGER; } else { - throw new AnnotationException( "Unknown DiscrimiatorType in XML, activate schema validation: " + value ); + throw new AnnotationException( "Unknown DiscrimiatorType in XML: " + value + " (" + SCHEMA_VALIDATION + ")"); } } ad.setValue( "discriminatorType", type ); @@ -330,7 +550,7 @@ strategy = InheritanceType.TABLE_PER_CLASS; } else { - throw new AnnotationException( "Unknown InheritanceType in XML, activate schema validation: " + value ); + throw new AnnotationException( "Unknown InheritanceType in XML: " + value + " (" + SCHEMA_VALIDATION + ")" ); } } ad.setValue( "strategy", strategy ); @@ -361,7 +581,7 @@ return AnnotationFactory.create( ad ); } else { - throw new AnnotationException("id-class without class, please ensure schema validation"); + throw new AnnotationException("id-class without class. " + SCHEMA_VALIDATION); } } else if ( defaults.canUseJavaAnnotations() ) { @@ -401,7 +621,7 @@ else { if ( "entity".equals( tree.getName() ) ) { AnnotationDescriptor entity = new AnnotationDescriptor( Entity.class ); - copyStringAttribute( entity, tree, "name" ); + copyStringAttribute( entity, tree, "name", false ); if ( defaults.canUseJavaAnnotations() && StringHelper.isEmpty( (String) entity.valueOf( "name" ) ) ) { Entity javaAnn = super.getAnnotation( Entity.class ); @@ -481,13 +701,13 @@ else { //ignore java annotation, an element is defined AnnotationDescriptor annotation = new AnnotationDescriptor( Table.class ); - copyStringAttribute( annotation, subelement, "name" ); - copyStringAttribute( annotation, subelement, "catalog" ); + copyStringAttribute( annotation, subelement, "name", false ); + copyStringAttribute( annotation, subelement, "catalog", false ); if ( StringHelper.isNotEmpty( defaults.getCatalog() ) && StringHelper.isEmpty( (String) annotation.valueOf( "catalog" ) ) ) { annotation.setValue( "catalog", defaults.getCatalog() ); } - copyStringAttribute( annotation, subelement, "schema" ); + copyStringAttribute( annotation, subelement, "schema", false ); if ( StringHelper.isNotEmpty( defaults.getSchema() ) && StringHelper.isEmpty( (String) annotation.valueOf( "schema" ) ) ) { annotation.setValue( "schema", defaults.getSchema() ); @@ -504,13 +724,13 @@ List<SecondaryTable> secondaryTables = new ArrayList<SecondaryTable>( 3 ); for ( Element element : elements ) { AnnotationDescriptor annotation = new AnnotationDescriptor( SecondaryTable.class ); - copyStringAttribute( annotation, element, "name" ); - copyStringAttribute( annotation, element, "catalog" ); + copyStringAttribute( annotation, element, "name", false ); + copyStringAttribute( annotation, element, "catalog", false ); if ( StringHelper.isNotEmpty( defaults.getCatalog() ) && StringHelper.isEmpty( (String) annotation.valueOf( "catalog" ) ) ) { annotation.setValue( "catalog", defaults.getCatalog() ); } - copyStringAttribute( annotation, element, "schema" ); + copyStringAttribute( annotation, element, "schema", false ); if ( StringHelper.isNotEmpty( defaults.getSchema() ) && StringHelper.isEmpty( (String) annotation.valueOf( "schema" ) ) ) { annotation.setValue( "schema", defaults.getSchema() ); @@ -603,26 +823,33 @@ while ( pkIt.hasNext() ) { Element subelement = (Element) pkIt.next(); AnnotationDescriptor pkAnn = new AnnotationDescriptor( PrimaryKeyJoinColumn.class ); - copyStringAttribute( pkAnn, subelement, "name"); - copyStringAttribute( pkAnn, subelement, "referenced-column-name"); - copyStringAttribute( pkAnn, subelement, "column-definition"); + copyStringAttribute( pkAnn, subelement, "name", false ); + copyStringAttribute( pkAnn, subelement, "referenced-column-name", false ); + copyStringAttribute( pkAnn, subelement, "column-definition", false ); pkJoinColumns[index++] = AnnotationFactory.create( pkAnn ); } return pkJoinColumns; } - private void copyStringAttribute(AnnotationDescriptor annotation, Element element, String attributeName) { + private void copyStringAttribute( + AnnotationDescriptor annotation, Element element, String attributeName, boolean mandatory + ) { String attribute = element.attributeValue( attributeName ); if (attribute != null) { StringBuilder annotationAttributeName = new StringBuilder( attributeName ); int index = annotationAttributeName.indexOf( WORD_SEPARATOR ); while ( index != -1 ) { annotationAttributeName.deleteCharAt( index ); - annotationAttributeName.setCharAt( index, Character.toUpperCase( annotationAttributeName.charAt( index ) )); + annotationAttributeName.setCharAt( index, Character.toUpperCase( annotationAttributeName.charAt( index ) ) ); index = annotationAttributeName.indexOf( WORD_SEPARATOR ); } annotation.setValue( annotationAttributeName.toString(), attribute ); } + else { + if (mandatory) { + throw new AnnotationException( element.getName() + "." + attributeName + " is mandatory in XML overring. " + SCHEMA_VALIDATION ); + } + } } private void copyIntegerAttribute(AnnotationDescriptor annotation, Element element, String attributeName) { @@ -641,7 +868,7 @@ annotation.setValue( annotationAttributeName.toString(), length ); } catch (NumberFormatException e) { - throw new AnnotationException( attributeName + " not parseable, activate schema validation: " + attribute ); + throw new AnnotationException( attributeName + " not parseable: " + attribute + " (" + SCHEMA_VALIDATION + ")" ); } } } Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/EJB3OverridenAnnotationReaderTest.java =================================================================== --- trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/EJB3OverridenAnnotationReaderTest.java 2006-04-17 19:44:22 UTC (rev 9755) +++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/EJB3OverridenAnnotationReaderTest.java 2006-04-17 23:15:32 UTC (rev 9756) @@ -1,37 +1,39 @@ //$Id: $ package org.hibernate.test.reflection.java.xml; -import java.io.InputStream; import java.io.BufferedInputStream; import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; import java.util.List; -import java.util.ArrayList; - +import javax.persistence.DiscriminatorColumn; +import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; -import javax.persistence.Table; +import javax.persistence.IdClass; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; import javax.persistence.MappedSuperclass; -import javax.persistence.SecondaryTables; -import javax.persistence.SecondaryTable; +import javax.persistence.NamedNativeQueries; +import javax.persistence.NamedQueries; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.PrimaryKeyJoinColumns; -import javax.persistence.IdClass; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; -import javax.persistence.DiscriminatorValue; -import javax.persistence.DiscriminatorColumn; +import javax.persistence.SecondaryTable; +import javax.persistence.SecondaryTables; import javax.persistence.SequenceGenerator; +import javax.persistence.SqlResultSetMappings; +import javax.persistence.Table; import javax.persistence.TableGenerator; import junit.framework.TestCase; +import org.dom4j.DocumentException; +import org.dom4j.io.SAXReader; +import org.hibernate.cfg.EJB3DTDEntityResolver; +import org.hibernate.reflection.java.EJB3OverridenAnnotationReader; +import org.hibernate.reflection.java.xml.XMLContext; import org.hibernate.util.XMLHelper; -import org.hibernate.reflection.java.xml.XMLContext; -import org.hibernate.reflection.java.EJB3OverridenAnnotationReader; -import org.hibernate.cfg.EJB3DTDEntityResolver; -import org.dom4j.io.SAXReader; -import org.dom4j.DocumentException; -import org.xml.sax.SAXNotSupportedException; import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import org.xml.sax.SAXNotSupportedException; /** @@ -75,12 +77,26 @@ ); assertNotNull( reader.getAnnotation( Inheritance.class ) ); assertEquals( "inheritance strategy not overriden", InheritanceType.JOINED, reader.getAnnotation( Inheritance.class ).strategy() ); + assertNotNull( "NamedQuery not overriden", reader.getAnnotation( NamedQueries.class ) ); + assertEquals( "No deduplication", 3, reader.getAnnotation( NamedQueries.class ).value().length ); + assertEquals( "deduplication kept the Java version", 1, reader.getAnnotation( NamedQueries.class ).value()[1].hints().length ); + assertEquals( "org.hibernate.timeout", reader.getAnnotation( NamedQueries.class ).value()[1].hints()[0].name() ); + assertNotNull( "NamedNativeQuery not overriden", reader.getAnnotation( NamedNativeQueries.class ) ); + assertEquals( "No deduplication", 3, reader.getAnnotation( NamedNativeQueries.class ).value().length ); + assertEquals( "deduplication kept the Java version", 1, reader.getAnnotation( NamedNativeQueries.class ).value()[1].hints().length ); + assertEquals( "org.hibernate.timeout", reader.getAnnotation( NamedNativeQueries.class ).value()[1].hints()[0].name() ); + assertNotNull( reader.getAnnotation( SqlResultSetMappings.class ) ); + assertEquals( "competitor1Point", reader.getAnnotation( SqlResultSetMappings.class ).value()[0].columns()[0].name() ); + assertEquals( "competitor1Point", reader.getAnnotation( SqlResultSetMappings.class ).value()[0].entities()[0].fields()[0].column() ); + reader = new EJB3OverridenAnnotationReader(Competition.class, context); assertNotNull( reader.getAnnotation( MappedSuperclass.class) ); + reader = new EJB3OverridenAnnotationReader(TennisMatch.class, context); assertNull( "Mutualize PKJC into PKJCs", reader.getAnnotation( PrimaryKeyJoinColumn.class) ); assertNotNull( reader.getAnnotation( PrimaryKeyJoinColumns.class) ); assertEquals( "PrimaryKeyJoinColumn overrden", "id", reader.getAnnotation( PrimaryKeyJoinColumns.class).value()[0].name() ); + reader = new EJB3OverridenAnnotationReader(SocialSecurityPhysicalAccount.class, context); assertNotNull( reader.getAnnotation( IdClass.class ) ); assertEquals( "id-class not used", SocialSecurityNumber.class, reader.getAnnotation( IdClass.class ).value() ); @@ -98,6 +114,7 @@ assertNotNull( reader.getAnnotation( Table.class) ); assertEquals( "@Table should not be used", "", reader.getAnnotation( Table.class ).name() ); assertEquals( "Default schema not overriden", "myschema", reader.getAnnotation( Table.class ).schema() ); + reader = new EJB3OverridenAnnotationReader(Match.class, context); assertNotNull( reader.getAnnotation( Table.class) ); assertEquals( "@Table should not be used", "", reader.getAnnotation( Table.class ).name() ); @@ -106,11 +123,16 @@ assertNull( "Ignore Java annotation", reader.getAnnotation( SecondaryTable.class ) ); assertNull( "Ignore Java annotation", reader.getAnnotation( SecondaryTables.class ) ); assertNull( "Ignore Java annotation", reader.getAnnotation( Inheritance.class ) ); + assertNull( reader.getAnnotation( NamedQueries.class ) ); + assertNull( reader.getAnnotation( NamedNativeQueries.class ) ); + reader = new EJB3OverridenAnnotationReader(TennisMatch.class, context); assertNull( reader.getAnnotation( PrimaryKeyJoinColumn.class) ); assertNull( reader.getAnnotation( PrimaryKeyJoinColumns.class) ); + reader = new EJB3OverridenAnnotationReader(Competition.class, context); assertNull( reader.getAnnotation( MappedSuperclass.class) ); + reader = new EJB3OverridenAnnotationReader(SocialSecurityMoralAccount.class, context); assertNull( reader.getAnnotation( IdClass.class ) ); assertNull( reader.getAnnotation( DiscriminatorValue.class ) ); Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Match.java =================================================================== --- trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Match.java 2006-04-17 19:44:22 UTC (rev 9755) +++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Match.java 2006-04-17 23:15:32 UTC (rev 9756) @@ -2,10 +2,14 @@ package org.hibernate.test.reflection.java.xml; import javax.persistence.Entity; -import javax.persistence.Table; -import javax.persistence.SecondaryTable; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; +import javax.persistence.NamedQuery; +import javax.persistence.SecondaryTable; +import javax.persistence.Table; +import javax.persistence.NamedQueries; +import javax.persistence.NamedNativeQueries; +import javax.persistence.NamedNativeQuery; /** * @author Emmanuel Bernard @@ -14,5 +18,14 @@ @Table(name="matchtable", schema = "matchschema") @SecondaryTable(name="extendedMatch") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@NamedQueries( { + @NamedQuery(name="matchbyid", query="select m from Match m where m.id = :id"), + @NamedQuery(name="getAllMatches2", query="select m from Match m") + }) +@NamedNativeQueries( { + @NamedNativeQuery(name="matchbyid", query="select m from Match m where m.id = :id", resultSetMapping = "matchrs"), + @NamedNativeQuery(name="getAllMatches2", query="select m from Match m", resultSetMapping = "matchrs") + }) public class Match extends Competition { + public String competitor1Point; } Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/orm.xml =================================================================== --- trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/orm.xml 2006-04-17 19:44:22 UTC (rev 9755) +++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/orm.xml 2006-04-17 23:15:32 UTC (rev 9756) @@ -31,6 +31,28 @@ </entity> <entity class="Match"> <inheritance strategy="JOINED"/> + <named-query name="allmatches"> + <query>select m from Match m</query> + <hint name="org.hibernate.timeout" value="200"/> + </named-query> + <named-query name="matchbyid"> + <query>select m from Match m where m.id = :id</query> + <hint name="org.hibernate.timeout" value="200"/> + </named-query> + <named-native-query name="allmatches" result-set-mapping="matchrs"> + <query>select m from Match m</query> + <hint name="org.hibernate.timeout" value="200"/> + </named-native-query> + <named-native-query name="matchbyid" result-set-mapping="matchrs"> + <query>select m from Match m where m.id = :id</query> + <hint name="org.hibernate.timeout" value="200"/> + </named-native-query> + <sql-result-set-mapping name="matchrs"> + <entity-result entity-class="org.hibernate.test.reflection.java.xml.Match"> + <field-result name="competitor1Point" column="competitor1Point"/> + </entity-result> + <column-result name="competitor1Point"/> + </sql-result-set-mapping> </entity> <entity class="TennisMatch"> <primary-key-join-column name="id"/> |