Author: epbernard
Date: 2006-04-15 12:31:35 -0400 (Sat, 15 Apr 2006)
New Revision: 9748
Added:
trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java
trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Administration.java
trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Competition.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/XMLContextTest.java
trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/metadata-complete.xml
trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/orm.xml
Removed:
trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3ClassAnnotationReader.java
Modified:
trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/xml/XMLContext.java
Log:
Starting support for XML file overriding
Deleted: trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3ClassAnnotationReader.java
===================================================================
--- trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3ClassAnnotationReader.java 2006-04-13 12:14:07 UTC (rev 9747)
+++ trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3ClassAnnotationReader.java 2006-04-15 16:31:35 UTC (rev 9748)
@@ -1,103 +0,0 @@
-package org.hibernate.reflection.java;
-
-import java.lang.annotation.Annotation;
-import java.lang.reflect.AnnotatedElement;
-import java.util.Map;
-import java.util.HashMap;
-import java.util.List;
-import java.util.ArrayList;
-
-import javax.persistence.Entity;
-import javax.persistence.MappedSuperclass;
-import javax.persistence.Embeddable;
-
-import org.dom4j.Element;
-import org.hibernate.annotationfactory.AnnotationDescriptor;
-import org.hibernate.annotationfactory.AnnotationFactory;
-import org.hibernate.reflection.java.xml.XMLContext;
-
-/**
- * Encapsulates the overriding of Java annotations from an EJB 3.0 descriptor.
- *
- * @author Paolo Perrotta
- * @author Davide Marchignoli
- */
-class EJB3ClassAnnotationReader extends JavaAnnotationReader {
- private XMLContext xmlContext;
- private String className;
- private static final Map<Class, String> annotationToXml;
-
- static {
- annotationToXml = new HashMap<Class, String>();
- annotationToXml.put(Entity.class, "entity");
- annotationToXml.put(MappedSuperclass.class, "mapped-superclass");
- annotationToXml.put(Embeddable.class, "embeddable");
- }
-
- public EJB3ClassAnnotationReader( AnnotatedElement el, XMLContext xmlContext ) {
- super( el );
- this.xmlContext = xmlContext;
- if (el instanceof Class) {
- Class clazz = (Class) el;
- className = clazz.getName();
- }
- }
-
- public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
-
- return super.getAnnotation( annotationType );
- }
-
- public <T extends Annotation> boolean isAnnotationPresent(Class<T> annotationType) {
- return super.isAnnotationPresent( annotationType );
- }
-
- public Annotation[] getAnnotations() {
- XMLContext.Default defaults = xmlContext.getDefault(className);
- Element tree = xmlContext.getXMLTree( className, null);
- Annotation[] annotations = super.getAnnotations();
- List<Annotation> annotationList = new ArrayList<Annotation>(annotations.length + 5);
- for (Annotation annotation : annotations) {
- if (! annotationToXml.containsKey( annotation ) ) {
- //unknown annotations are left over
- annotationList.add(annotation);
- }
- }
- Annotation current = getEntity(tree, defaults);
- if (current != null) annotationList.add(current);
- return super.getAnnotations();
- }
-
- private Entity getEntity(Element tree, XMLContext.Default defaults) {
- if (tree == null) {
- return Boolean.TRUE.equals( defaults.getMetadataComplete() ) ? super.getAnnotation( Entity.class ) : null;
- }
- else {
- if ( "entity".equals( tree.getName() ) ) {
- AnnotationDescriptor entity = new AnnotationDescriptor( Entity.class );
- entity.setValue( "name", tree.attributeValue( "name", "" ) );
- return AnnotationFactory.create( entity );
- }
- else {
- return null; //this is not an entity
- }
- }
- }
-
- private Annotation getTopLevelElement(Class<Annotation> annotation, Element tree, XMLContext.Default defaults) {
- if (tree == null) {
- return getAnnotation( annotation );
- }
- else {
- if ( tree.getName().equals( annotationToXml.get( annotation ) ) ) {
- //TODO resume ***********************************
- return null;
- }
- else {
- return null; //this is not an entity
- }
- }
- }
-
-
-}
Copied: trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java (from rev 9709, trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3ClassAnnotationReader.java)
===================================================================
--- trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3ClassAnnotationReader.java 2006-03-29 03:32:24 UTC (rev 9709)
+++ trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java 2006-04-15 16:31:35 UTC (rev 9748)
@@ -0,0 +1,387 @@
+package org.hibernate.reflection.java;
+
+import java.beans.Introspector;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import javax.persistence.Embeddable;
+import javax.persistence.Entity;
+import javax.persistence.MappedSuperclass;
+import javax.persistence.PrimaryKeyJoinColumn;
+import javax.persistence.SecondaryTable;
+import javax.persistence.SecondaryTables;
+import javax.persistence.Table;
+import javax.persistence.UniqueConstraint;
+
+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.util.StringHelper;
+
+/**
+ * Encapsulates the overriding of Java annotations from an EJB 3.0 descriptor.
+ *
+ * @author Paolo Perrotta
+ * @author Davide Marchignoli
+ */
+public class EJB3OverridenAnnotationReader extends JavaAnnotationReader {
+ private static final Map<Class, String> annotationToXml;
+ private static final Filter FILTER = new Filter() {
+ public boolean returnStatic() {
+ return false;
+ }
+
+ public boolean returnTransient() {
+ return false;
+ }
+ };
+
+ static {
+ annotationToXml = new HashMap<Class, String>();
+ annotationToXml.put( Entity.class, "entity" );
+ annotationToXml.put( MappedSuperclass.class, "mapped-superclass" );
+ annotationToXml.put( Embeddable.class, "embeddable" );
+ annotationToXml.put( Table.class, "table" );
+ annotationToXml.put( SecondaryTable.class, "secondary-table" );
+ annotationToXml.put( SecondaryTables.class, "secondary-table" );
+ }
+
+ private XMLContext xmlContext;
+ private String className;
+ private String propertyName;
+ private boolean isFieldAccess;
+ private transient Annotation[] annotations;
+
+ public EJB3OverridenAnnotationReader(AnnotatedElement el, XMLContext xmlContext) {
+ super( el );
+ this.xmlContext = xmlContext;
+ if ( el instanceof Class ) {
+ Class clazz = (Class) el;
+ className = clazz.getName();
+ }
+ else if ( el instanceof Field ) {
+ propertyName = ( (Field) el ).getName();
+ isFieldAccess = true;
+ }
+ else if ( el instanceof Method ) {
+ Method method = (Method) el;
+ propertyName = method.getName();
+ if ( JavaXProperty.isProperty(
+ method,
+ null, //this is yukky!! we'd rather get the TypeEnvironment()
+ FILTER
+ ) ) {
+ if ( propertyName.startsWith( "get" ) ) {
+ propertyName = Introspector.decapitalize( propertyName.substring( "get".length() ) );
+ }
+ if ( propertyName.startsWith( "is" ) ) {
+ propertyName = Introspector.decapitalize( propertyName.substring( "is".length() ) );
+ }
+ throw new RuntimeException( "Method " + propertyName + " is not a property getter" );
+ }
+ isFieldAccess = true;
+ }
+ else {
+ className = null;
+ propertyName = null;
+ }
+ }
+
+ public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
+ initAnnotations();
+ for ( Annotation annotation : annotations ) {
+ if ( annotation.annotationType().equals( annotationType ) ) return (T) annotation;
+ }
+ return null;
+ }
+
+ public <T extends Annotation> boolean isAnnotationPresent(Class<T> annotationType) {
+ initAnnotations();
+ for ( Annotation annotation : annotations ) {
+ if ( annotation.getClass().equals( annotationType ) ) return true;
+ }
+ return false;
+ }
+
+ public Annotation[] getAnnotations() {
+ initAnnotations();
+ return annotations;
+ }
+
+ private void initAnnotations() {
+ if ( annotations == null ) {
+ XMLContext.Default defaults = xmlContext.getDefault( className );
+ if ( className != null ) {
+ Element tree = xmlContext.getXMLTree( className, null );
+ Annotation[] annotations = super.getAnnotations();
+ List<Annotation> annotationList = new ArrayList<Annotation>( annotations.length + 5 );
+ for ( Annotation annotation : annotations ) {
+ if ( ! annotationToXml.containsKey( annotation.annotationType() ) ) {
+ //unknown annotations are left over
+ annotationList.add( annotation );
+ }
+ }
+ Annotation current = getEntity( tree, defaults );
+ if ( current != null ) annotationList.add( current );
+ current = getMappedSuperclass( tree, defaults );
+ if ( current != null ) annotationList.add( current );
+ current = getEmbeddable( tree, defaults );
+ if ( current != null ) annotationList.add( current );
+ current = getTable( tree, defaults );
+ if ( current != null ) annotationList.add( current );
+ current = getSecondaryTables( tree, defaults );
+ if ( current != null ) annotationList.add( current );
+ this.annotations = annotationList.toArray( new Annotation[ annotationList.size() ] );
+ }
+ else if ( propertyName != null ) {
+ //TODO do it
+ this.annotations = super.getAnnotations();
+ }
+ else {
+ this.annotations = super.getAnnotations();
+ }
+ }
+ }
+
+ private Entity getEntity(Element tree, XMLContext.Default defaults) {
+ if ( tree == null ) {
+ return ! Boolean.TRUE.equals( defaults.getMetadataComplete() ) ? super.getAnnotation( Entity.class ) : null;
+ }
+ else {
+ if ( "entity".equals( tree.getName() ) ) {
+ AnnotationDescriptor entity = new AnnotationDescriptor( Entity.class );
+ copyAttribute( entity, tree, "name" );
+ if ( ! Boolean.TRUE.equals( defaults.getMetadataComplete() )
+ && StringHelper.isEmpty( (String) entity.valueOf( "name" ) ) ) {
+ Entity javaAnn = super.getAnnotation( Entity.class );
+ if ( javaAnn != null ) entity.setValue( "name", javaAnn.name() );
+ }
+ return AnnotationFactory.create( entity );
+ }
+ else {
+ return null; //this is not an entity
+ }
+ }
+ }
+
+ private MappedSuperclass getMappedSuperclass(Element tree, XMLContext.Default defaults) {
+ if ( tree == null ) {
+ return ! Boolean.TRUE.equals( defaults.getMetadataComplete() ) ? super.getAnnotation(
+ MappedSuperclass.class
+ ) : null;
+ }
+ else {
+ if ( "mapped-superclass".equals( tree.getName() ) ) {
+ AnnotationDescriptor entity = new AnnotationDescriptor( Entity.class );
+ return AnnotationFactory.create( entity );
+ }
+ else {
+ return null; //this is not an entity
+ }
+ }
+ }
+
+ private Embeddable getEmbeddable(Element tree, XMLContext.Default defaults) {
+ if ( tree == null ) {
+ return ! Boolean.TRUE.equals( defaults.getMetadataComplete() ) ? super.getAnnotation(
+ Embeddable.class
+ ) : null;
+ }
+ else {
+ if ( "embeddable".equals( tree.getName() ) ) {
+ AnnotationDescriptor entity = new AnnotationDescriptor( Embeddable.class );
+ return AnnotationFactory.create( entity );
+ }
+ else {
+ return null; //this is not an entity
+ }
+ }
+ }
+
+ private Table getTable(Element tree, XMLContext.Default defaults) {
+ Element subelement = tree == null ? null : tree.element( "table" );
+ if ( subelement == null ) {
+ //no element but might have some default or some annotation
+ if ( StringHelper.isNotEmpty( defaults.getCatalog() )
+ || StringHelper.isNotEmpty( defaults.getSchema() ) ) {
+ AnnotationDescriptor annotation = new AnnotationDescriptor( Table.class );
+ if ( ! Boolean.TRUE.equals( defaults.getMetadataComplete() ) ) {
+ Table table = super.getAnnotation( Table.class );
+ if ( table != null ) {
+ annotation.setValue( "name", table.name() );
+ annotation.setValue( "schema", table.schema() );
+ annotation.setValue( "catalog", table.catalog() );
+ annotation.setValue( "uniqueConstraints", table.uniqueConstraints() );
+ }
+ }
+ if ( StringHelper.isEmpty( (String) annotation.valueOf( "schema" ) )
+ && StringHelper.isNotEmpty( defaults.getSchema() ) ) {
+ annotation.setValue( "schema", defaults.getSchema() );
+ }
+ if ( StringHelper.isEmpty( (String) annotation.valueOf( "catalog" ) )
+ && StringHelper.isNotEmpty( defaults.getCatalog() ) ) {
+ annotation.setValue( "catalog", defaults.getCatalog() );
+ }
+ return AnnotationFactory.create( annotation );
+ }
+ else if ( ! Boolean.TRUE.equals( defaults.getMetadataComplete() ) ) {
+ return super.getAnnotation( Table.class );
+ }
+ else {
+ return null;
+ }
+ }
+ else {
+ //ignore java annotation, an element is defined
+ AnnotationDescriptor annotation = new AnnotationDescriptor( Table.class );
+ copyAttribute( annotation, subelement, "name" );
+ copyAttribute( annotation, subelement, "catalog" );
+ if ( StringHelper.isNotEmpty( defaults.getCatalog() )
+ && StringHelper.isEmpty( (String) annotation.valueOf( "catalog" ) ) ) {
+ annotation.setValue( "catalog", defaults.getCatalog() );
+ }
+ copyAttribute( annotation, subelement, "schema" );
+ if ( StringHelper.isNotEmpty( defaults.getSchema() )
+ && StringHelper.isEmpty( (String) annotation.valueOf( "schema" ) ) ) {
+ annotation.setValue( "schema", defaults.getSchema() );
+ }
+ buildUniqueConstraints( annotation, subelement );
+ return AnnotationFactory.create( annotation );
+ }
+ }
+
+ private SecondaryTables getSecondaryTables(Element tree, XMLContext.Default defaults) {
+ List<Element> elements = tree == null ?
+ new ArrayList<Element>() :
+ (List<Element>) tree.elements( "secondary-table" );
+ List<SecondaryTable> secondaryTables = new ArrayList<SecondaryTable>( 3 );
+ for ( Element element : elements ) {
+ AnnotationDescriptor annotation = new AnnotationDescriptor( SecondaryTable.class );
+ copyAttribute( annotation, element, "name" );
+ copyAttribute( annotation, element, "catalog" );
+ if ( StringHelper.isNotEmpty( defaults.getCatalog() )
+ && StringHelper.isEmpty( (String) annotation.valueOf( "catalog" ) ) ) {
+ annotation.setValue( "catalog", defaults.getCatalog() );
+ }
+ copyAttribute( annotation, element, "schema" );
+ if ( StringHelper.isNotEmpty( defaults.getSchema() )
+ && StringHelper.isEmpty( (String) annotation.valueOf( "schema" ) ) ) {
+ annotation.setValue( "schema", defaults.getSchema() );
+ }
+ buildUniqueConstraints( annotation, element );
+ buildPrimaryKeyJoinColumns( annotation, element );
+ secondaryTables.add( (SecondaryTable) AnnotationFactory.create( annotation ) );
+ }
+ /*
+ * You can't have both secondary table in XML and Java,
+ * since there would be no way to "remove" a secondary table
+ */
+ if ( secondaryTables.size() == 0 && ! Boolean.TRUE.equals( defaults.getMetadataComplete() ) ) {
+ SecondaryTable secTableAnn = super.getAnnotation( SecondaryTable.class );
+ overridesDefaultInSecondaryTable( secTableAnn, defaults, secondaryTables );
+ SecondaryTables secTablesAnn = super.getAnnotation( SecondaryTables.class );
+ if (secTablesAnn != null) {
+ for ( SecondaryTable table : secTablesAnn.value() ) {
+ overridesDefaultInSecondaryTable( table, defaults, secondaryTables );
+ }
+ }
+ }
+ if (secondaryTables.size() > 0) {
+ AnnotationDescriptor descriptor = new AnnotationDescriptor( SecondaryTables.class );
+ descriptor.setValue( "value", secondaryTables.toArray( new SecondaryTable[secondaryTables.size()] ) );
+ return AnnotationFactory.create( descriptor );
+ }
+ else {
+ return null;
+ }
+ }
+
+ private void overridesDefaultInSecondaryTable(
+ SecondaryTable secTableAnn, XMLContext.Default defaults, List<SecondaryTable> secondaryTables
+ ) {
+ if ( secTableAnn != null ) {
+ //handle default values
+ if ( StringHelper.isNotEmpty( defaults.getCatalog() )
+ || StringHelper.isNotEmpty( defaults.getSchema() ) ) {
+ AnnotationDescriptor annotation = new AnnotationDescriptor( SecondaryTable.class );
+ annotation.setValue( "name", secTableAnn.name() );
+ annotation.setValue( "schema", secTableAnn.schema() );
+ annotation.setValue( "catalog", secTableAnn.catalog() );
+ annotation.setValue( "uniqueConstraints", secTableAnn.uniqueConstraints() );
+ annotation.setValue( "pkJoinColumns", secTableAnn.pkJoinColumns() );
+ if ( StringHelper.isEmpty( (String) annotation.valueOf( "schema" ) )
+ && StringHelper.isNotEmpty( defaults.getSchema() ) ) {
+ annotation.setValue( "schema", defaults.getSchema() );
+ }
+ if ( StringHelper.isEmpty( (String) annotation.valueOf( "catalog" ) )
+ && StringHelper.isNotEmpty( defaults.getCatalog() ) ) {
+ annotation.setValue( "catalog", defaults.getCatalog() );
+ }
+ secondaryTables.add( (SecondaryTable) AnnotationFactory.create( annotation ) );
+ }
+ else {
+ secondaryTables.add( secTableAnn );
+ }
+ }
+ }
+
+ private void buildUniqueConstraints(AnnotationDescriptor annotation, Element element) {
+ List uniqueConstraintElementList = element.elements( "unique-constraint" );
+ UniqueConstraint[] uniqueConstraints = new UniqueConstraint[ uniqueConstraintElementList.size() ];
+ int ucIndex = 0;
+ Iterator ucIt = uniqueConstraintElementList.listIterator();
+ while ( ucIt.hasNext() ) {
+ Element subelement = (Element) ucIt.next();
+ List<Element> columnNamesElements = subelement.elements( "column-name" );
+ String[] columnNames = new String[columnNamesElements.size()];
+ int columnNameIndex = 0;
+ Iterator it = columnNamesElements.listIterator();
+ while ( it.hasNext() ) {
+ Element columnNameElt = (Element) it.next();
+ columnNames[columnNameIndex++] = columnNameElt.getTextTrim();
+ }
+ AnnotationDescriptor ucAnn = new AnnotationDescriptor( UniqueConstraint.class );
+ ucAnn.setValue( "columnNames", columnNames );
+ uniqueConstraints[ucIndex++] = AnnotationFactory.create( ucAnn );
+ }
+ annotation.setValue( "uniqueConstraints", uniqueConstraints );
+ }
+
+ private void buildPrimaryKeyJoinColumns(AnnotationDescriptor annotation, Element element) {
+ List pkJoinColumnElementList = element.elements( "primary-key-join-column" );
+ PrimaryKeyJoinColumn[] pkJoinColumns = new PrimaryKeyJoinColumn[ pkJoinColumnElementList.size() ];
+ int index = 0;
+ Iterator pkIt = pkJoinColumnElementList.listIterator();
+ while ( pkIt.hasNext() ) {
+ Element subelement = (Element) pkIt.next();
+ AnnotationDescriptor pkAnn = new AnnotationDescriptor( PrimaryKeyJoinColumn.class );
+ Attribute attribute = subelement.attribute( "name" );
+ if ( attribute != null ) {
+ pkAnn.setValue( "name", attribute.getValue() );
+ }
+ attribute = subelement.attribute( "referenced-column-name" );
+ if ( attribute != null ) {
+ pkAnn.setValue( "referencedColumnName", attribute.getValue() );
+ }
+ attribute = subelement.attribute( "column-definition" );
+ if ( attribute != null ) {
+ pkAnn.setValue( "columnDefinition", attribute.getValue() );
+ }
+ pkJoinColumns[index++] = AnnotationFactory.create( pkAnn );
+ }
+ annotation.setValue( "pkJoinColumns", pkJoinColumns );
+ }
+
+ private void copyAttribute(AnnotationDescriptor annotation, Element element, String attributeName) {
+ annotation.setValue( attributeName, element.attributeValue( attributeName, "" ) );
+ }
+}
Property changes on: trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/xml/XMLContext.java
===================================================================
--- trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/xml/XMLContext.java 2006-04-13 12:14:07 UTC (rev 9747)
+++ trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/xml/XMLContext.java 2006-04-15 16:31:35 UTC (rev 9748)
@@ -23,7 +23,6 @@
public void addDocument(Document doc) {
Element root = doc.getRootElement();
-
//global defaults
Element metadata = root.element( "persistence-unit-metadata" );
if (metadata != null) {
@@ -35,15 +34,17 @@
null
);
Element defaultElement = metadata.element( "persistence-unit-defaults" );
- Element unitElement = defaultElement.element( "schema" );
- globalDefaults.setSchema( unitElement != null ? unitElement.getTextTrim() : null );
- unitElement = defaultElement.element( "catalog" );
- globalDefaults.setCatalog( unitElement != null ? unitElement.getTextTrim() : null );
- unitElement = defaultElement.element( "access" );
- globalDefaults.setAccess( unitElement != null ? unitElement.getTextTrim() : null );
- unitElement = defaultElement.element( "cascade-persist" );
- globalDefaults.setCascadePersist( unitElement != null ? Boolean.TRUE : null );
- //TODO entity listeners
+ if ( defaultElement != null ) {
+ Element unitElement = defaultElement.element( "schema" );
+ globalDefaults.setSchema( unitElement != null ? unitElement.getTextTrim() : null );
+ unitElement = defaultElement.element( "catalog" );
+ globalDefaults.setCatalog( unitElement != null ? unitElement.getTextTrim() : null );
+ unitElement = defaultElement.element( "access" );
+ globalDefaults.setAccess( unitElement != null ? unitElement.getTextTrim() : null );
+ unitElement = defaultElement.element( "cascade-persist" );
+ globalDefaults.setCascadePersist( unitElement != null ? Boolean.TRUE : null );
+ //TODO entity listeners
+ }
}
else {
log.warn("Found more than one <persistence-unit-metadata>, ignored");
Added: trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Administration.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Administration.java 2006-04-13 12:14:07 UTC (rev 9747)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Administration.java 2006-04-15 16:31:35 UTC (rev 9748)
@@ -0,0 +1,44 @@
+//$Id: $
+package org.hibernate.test.reflection.java.xml;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.SecondaryTable;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity(name="JavaAdministration")
+@Table(name="JavaAdministration")
+@SecondaryTable(name="Extend")
+public class Administration {
+ @Id private Integer id;
+ private String firstname;
+ private String lastname;
+ private String address;
+
+ public String getFirstname() {
+ return firstname;
+ }
+
+ public void setFirstname(String firstname) {
+ this.firstname = firstname;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getLastname() {
+ return lastname;
+ }
+
+ public void setLastname(String lastname) {
+ this.lastname = lastname;
+ }
+}
Added: trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Competition.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Competition.java 2006-04-13 12:14:07 UTC (rev 9747)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Competition.java 2006-04-15 16:31:35 UTC (rev 9748)
@@ -0,0 +1,11 @@
+//$Id: $
+package org.hibernate.test.reflection.java.xml;
+
+import javax.persistence.MappedSuperclass;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@MappedSuperclass
+public class Competition {
+}
Added: 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-13 12:14:07 UTC (rev 9747)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/EJB3OverridenAnnotationReaderTest.java 2006-04-15 16:31:35 UTC (rev 9748)
@@ -0,0 +1,108 @@
+//$Id: $
+package org.hibernate.test.reflection.java.xml;
+
+import java.io.InputStream;
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+
+import javax.persistence.Entity;
+import javax.persistence.Table;
+import javax.persistence.MappedSuperclass;
+import javax.persistence.SecondaryTables;
+import javax.persistence.SecondaryTable;
+
+import junit.framework.TestCase;
+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;
+
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class EJB3OverridenAnnotationReaderTest extends TestCase {
+ public void testEntityRelatedAnnotations() throws Exception {
+ XMLContext context = buildContext("org/hibernate/test/reflection/java/xml/orm.xml");
+ EJB3OverridenAnnotationReader reader = new EJB3OverridenAnnotationReader(Administration.class, context);
+ assertNotNull( reader.getAnnotation( Entity.class) );
+ assertEquals( "Default value in xml entity should not override @Entity.name", "JavaAdministration", reader.getAnnotation( Entity.class).name() );
+ assertNotNull( reader.getAnnotation( Table.class) );
+ assertEquals( "@Table not overriden", "tbl_admin", reader.getAnnotation( Table.class ).name() );
+ assertEquals( "Default schema not overriden", "myschema", reader.getAnnotation( Table.class ).schema() );
+ assertEquals( "Proper @Table.uniqueConstraints", 2, reader.getAnnotation( Table.class ).uniqueConstraints()[0].columnNames().length );
+ String columnName = reader.getAnnotation( Table.class ).uniqueConstraints()[0].columnNames()[0];
+ assertTrue( "Proper @Table.uniqueConstraints", "firstname".equals( columnName ) || "lastname".equals( columnName ) );
+ assertNull( "Both Java and XML used", reader.getAnnotation( SecondaryTable.class) );
+ assertNotNull( "XML does not work", reader.getAnnotation( SecondaryTables.class) );
+ SecondaryTable[] tables = reader.getAnnotation( SecondaryTables.class).value();
+ assertEquals( 1, tables.length );
+ assertEquals( "admin2", tables[0].name() );
+ assertEquals( "unique constraints ignored", 1, tables[0].uniqueConstraints().length );
+ assertEquals( "pk join column ignored", 1, tables[0].pkJoinColumns().length );
+ assertEquals( "pk join column ignored", "admin_id", tables[0].pkJoinColumns()[0].name() );
+ reader = new EJB3OverridenAnnotationReader(Match.class, context);
+ assertNotNull( reader.getAnnotation( Table.class) );
+ assertEquals( "Java annotation not taken into account", "matchtable", reader.getAnnotation( Table.class ).name() );
+ assertEquals( "Java annotation not taken into account", "matchschema", reader.getAnnotation( Table.class ).schema() );
+ assertEquals( "Overriding not taken into account", "mycatalog", reader.getAnnotation( Table.class ).catalog() );
+ assertNotNull( "SecondaryTable swallowed", reader.getAnnotation( SecondaryTables.class ) );
+ assertEquals( "Default schema not taken into account", "myschema",
+ reader.getAnnotation( SecondaryTables.class ).value()[0].schema()
+ );
+ reader = new EJB3OverridenAnnotationReader(Competition.class, context);
+ assertNotNull( reader.getAnnotation( MappedSuperclass.class) );
+ }
+
+ public void testEntityRelatedAnnotationsMetadataComplete() throws Exception {
+ XMLContext context = buildContext("org/hibernate/test/reflection/java/xml/metadata-complete.xml");
+ EJB3OverridenAnnotationReader reader = new EJB3OverridenAnnotationReader(Administration.class, context);
+ assertNotNull( reader.getAnnotation( Entity.class) );
+ assertEquals( "Metadata complete should ignore java annotations", "", reader.getAnnotation( Entity.class).name() );
+ 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() );
+ assertEquals( "Overriding not taken into account", "myschema", reader.getAnnotation( Table.class ).schema() );
+ assertEquals( "Overriding not taken into account", "mycatalog", reader.getAnnotation( Table.class ).catalog() );
+ reader = new EJB3OverridenAnnotationReader(Competition.class, context);
+ assertNull( reader.getAnnotation( MappedSuperclass.class) );
+ }
+
+ private XMLContext buildContext(String ormfile) throws SAXException, DocumentException, IOException {
+ XMLHelper xmlHelper = new XMLHelper();
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ InputStream is = cl.getResourceAsStream( ormfile );
+ assertNotNull( "ORM.xml not found: " + ormfile, is );
+ XMLContext context = new XMLContext();
+ List errors = new ArrayList();
+ SAXReader saxReader = xmlHelper.createSAXReader( "XML InputStream", errors, EJB3DTDEntityResolver.INSTANCE );
+ //saxReader.setValidation( false );
+ try {
+ saxReader.setFeature( "http://apache.org/xml/features/validation/schema", true );
+ }
+ catch (SAXNotSupportedException e) {
+ saxReader.setValidation( false );
+ }
+ org.dom4j.Document doc;
+ try {
+ doc = saxReader
+ .read( new InputSource( new BufferedInputStream( is ) ) );
+ }
+ finally {
+ is.close();
+ }
+ assertEquals( 0, errors.size() );
+ context.addDocument( doc );
+ return context;
+ }
+}
Added: 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-13 12:14:07 UTC (rev 9747)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Match.java 2006-04-15 16:31:35 UTC (rev 9748)
@@ -0,0 +1,15 @@
+//$Id: $
+package org.hibernate.test.reflection.java.xml;
+
+import javax.persistence.Entity;
+import javax.persistence.Table;
+import javax.persistence.SecondaryTable;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+@Table(name="matchtable", schema = "matchschema")
+@SecondaryTable(name="extendedMatch")
+public class Match extends Competition {
+}
Added: trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/XMLContextTest.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/XMLContextTest.java 2006-04-13 12:14:07 UTC (rev 9747)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/XMLContextTest.java 2006-04-15 16:31:35 UTC (rev 9748)
@@ -0,0 +1,53 @@
+//$Id: $
+package org.hibernate.test.reflection.java.xml;
+
+import java.io.BufferedInputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+import org.dom4j.io.SAXReader;
+import org.hibernate.cfg.EJB3DTDEntityResolver;
+import org.hibernate.reflection.java.xml.XMLContext;
+import org.hibernate.util.XMLHelper;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXNotSupportedException;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class XMLContextTest extends TestCase {
+ public void testAll() throws Exception {
+ XMLHelper xmlHelper = new XMLHelper();
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ InputStream is = cl.getResourceAsStream( "org/hibernate/test/reflection/java/xml/orm.xml" );
+ assertNotNull( "ORM.xml not found", is );
+ XMLContext context = new XMLContext();
+ List errors = new ArrayList();
+ SAXReader saxReader = xmlHelper.createSAXReader( "XML InputStream", errors, EJB3DTDEntityResolver.INSTANCE );
+ //saxReader.setValidation( false );
+ try {
+ saxReader.setFeature( "http://apache.org/xml/features/validation/schema", true );
+ }
+ catch (SAXNotSupportedException e) {
+ saxReader.setValidation( false );
+ }
+ org.dom4j.Document doc;
+ try {
+ doc = saxReader
+ .read( new InputSource( new BufferedInputStream( is ) ) );
+ }
+ finally {
+ try {
+ is.close();
+ }
+ catch (IOException ioe) {
+ //log.warn( "Could not close input stream", ioe );
+ }
+ }
+ assertEquals( 0, errors.size() );
+ context.addDocument( doc );
+ }
+}
Added: trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/metadata-complete.xml
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/metadata-complete.xml 2006-04-13 12:14:07 UTC (rev 9747)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/metadata-complete.xml 2006-04-15 16:31:35 UTC (rev 9748)
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
+ version="1.0"
+>
+ <persistence-unit-metadata>
+ <xml-mapping-metadata-complete/>
+ <persistence-unit-defaults>
+ <schema>myschema</schema>
+ <catalog>mycatalog</catalog>
+ <cascade-persist/>
+ </persistence-unit-defaults>
+ </persistence-unit-metadata>
+ <package>org.hibernate.test.reflection.java.xml</package>
+ <entity class="Administration">
+ </entity>
+ <entity class="Match">
+ </entity>
+</entity-mappings>
\ No newline at end of file
Added: 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-13 12:14:07 UTC (rev 9747)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/orm.xml 2006-04-15 16:31:35 UTC (rev 9748)
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
+ version="1.0"
+>
+ <persistence-unit-metadata>
+ <persistence-unit-defaults>
+ <schema>myschema</schema>
+ <catalog>mycatalog</catalog>
+ <cascade-persist/>
+ </persistence-unit-defaults>
+ </persistence-unit-metadata>
+ <package>org.hibernate.test.reflection.java.xml</package>
+ <entity class="Administration">
+ <table name="tbl_admin">
+ <unique-constraint>
+ <column-name>firstname</column-name>
+ <column-name>lastname</column-name>
+ </unique-constraint>
+ </table>
+ <secondary-table name="admin2">
+ <primary-key-join-column name="admin_id" referenced-column-name="id"/>
+ <unique-constraint>
+ <column-name>address</column-name>
+ </unique-constraint>
+ </secondary-table>
+ </entity>
+ <entity class="Match">
+ </entity>
+</entity-mappings>
\ No newline at end of file
|