[hibernate-commits] Hibernate SVN: r17056 - in jpamodelgen/trunk: generator/src/main/java/org/hibernate/jpa/metamodel/ap/xml and 3 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Jul 9 05:53:56 EDT 2009


Author: hardy.ferentschik
Date: 2009-07-09 05:53:56 -0400 (Thu, 09 Jul 2009)
New Revision: 17056

Added:
   jpamodelgen/trunk/test/src/main/java/model/xmlmapped/Building.java
   jpamodelgen/trunk/test/src/main/resources/META-INF/building.xml
Modified:
   jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/JPAMetaModelEntityProcessor.java
   jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/xml/XmlMetaEntity.java
   jpamodelgen/trunk/test/src/main/resources/META-INF/address.xml
   jpamodelgen/trunk/test/src/main/resources/META-INF/persistence.xml
   jpamodelgen/trunk/test/src/test/java/tests/XmlMappingTest.java
Log:
added xml mapped superclasses

Modified: jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/JPAMetaModelEntityProcessor.java
===================================================================
--- jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/JPAMetaModelEntityProcessor.java	2009-07-08 23:25:41 UTC (rev 17055)
+++ jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/JPAMetaModelEntityProcessor.java	2009-07-09 09:53:56 UTC (rev 17056)
@@ -3,17 +3,10 @@
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.PrintWriter;
-import java.io.StringWriter;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
-import javax.annotation.Generated;
 import javax.annotation.processing.AbstractProcessor;
-import javax.annotation.processing.FilerException;
 import javax.annotation.processing.ProcessingEnvironment;
 import javax.annotation.processing.RoundEnvironment;
 import javax.annotation.processing.SupportedAnnotationTypes;
@@ -24,9 +17,6 @@
 import javax.lang.model.element.ElementKind;
 import javax.lang.model.element.TypeElement;
 import javax.lang.model.util.Elements;
-import javax.lang.model.type.TypeMirror;
-import javax.lang.model.type.TypeKind;
-import javax.lang.model.type.DeclaredType;
 import javax.persistence.Embeddable;
 import javax.persistence.MappedSuperclass;
 import javax.tools.Diagnostic;
@@ -66,7 +56,7 @@
 
 	public void init(ProcessingEnvironment env) {
 		super.init( env );
-		context = new Context(env);
+		context = new Context( env );
 		processingEnv.getMessager().printMessage( Diagnostic.Kind.NOTE, "Init Processor " + this );
 	}
 
@@ -164,6 +154,7 @@
 
 		parseEntities( mappings );
 		parseEmbeddable( mappings );
+		parseMappedSuperClass( mappings );
 	}
 
 	private void parseEntities(EntityMappings mappings) {
@@ -173,7 +164,7 @@
 			String fullyQualifiedClassName = packageName + "." + entity.getClazz();
 			Elements utils = processingEnv.getElementUtils();
 			XmlMetaEntity metaEntity = new XmlMetaEntity(
-					entity, packageName, utils.getTypeElement( fullyQualifiedClassName ), context
+					entity, packageName, utils.getTypeElement( fullyQualifiedClassName )
 			);
 
 			if ( context.getMetaEntitiesToProcess().containsKey( fullyQualifiedClassName ) ) {
@@ -206,6 +197,26 @@
 		}
 	}
 
+	private void parseMappedSuperClass(EntityMappings mappings) {
+		String packageName = mappings.getPackage();
+		Collection<org.hibernate.jpa.metamodel.xml.jaxb.MappedSuperclass> mappedSuperClasses = mappings.getMappedSuperclass();
+		for ( org.hibernate.jpa.metamodel.xml.jaxb.MappedSuperclass mappedSuperClass : mappedSuperClasses ) {
+			String fullyQualifiedClassName = packageName + "." + mappedSuperClass.getClazz();
+			Elements utils = processingEnv.getElementUtils();
+			XmlMetaEntity metaEntity = new XmlMetaEntity(
+					mappedSuperClass, packageName, utils.getTypeElement( fullyQualifiedClassName )
+			);
+
+			if ( context.getMetaSuperclassAndEmbeddableToProcess().containsKey( fullyQualifiedClassName ) ) {
+				processingEnv.getMessager().printMessage(
+						Diagnostic.Kind.WARNING,
+						fullyQualifiedClassName + " was already processed once. Skipping second occurance."
+				);
+			}
+			context.getMetaSuperclassAndEmbeddableToProcess().put( fullyQualifiedClassName, metaEntity );
+		}
+	}
+
 	private void handleRootElementAnnotationMirrors(final Element element) {
 
 		List<? extends AnnotationMirror> annotationMirrors = element

Modified: jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/xml/XmlMetaEntity.java
===================================================================
--- jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/xml/XmlMetaEntity.java	2009-07-08 23:25:41 UTC (rev 17055)
+++ jpamodelgen/trunk/generator/src/main/java/org/hibernate/jpa/metamodel/ap/xml/XmlMetaEntity.java	2009-07-09 09:53:56 UTC (rev 17056)
@@ -26,20 +26,20 @@
 import javax.lang.model.element.TypeElement;
 import javax.lang.model.type.DeclaredType;
 
+import org.hibernate.jpa.metamodel.ap.IMetaAttribute;
 import org.hibernate.jpa.metamodel.ap.IMetaEntity;
-import org.hibernate.jpa.metamodel.ap.IMetaAttribute;
 import org.hibernate.jpa.metamodel.ap.ImportContext;
 import org.hibernate.jpa.metamodel.ap.ImportContextImpl;
-import org.hibernate.jpa.metamodel.ap.Context;
 import org.hibernate.jpa.metamodel.xml.jaxb.Attributes;
 import org.hibernate.jpa.metamodel.xml.jaxb.Basic;
 import org.hibernate.jpa.metamodel.xml.jaxb.ElementCollection;
+import org.hibernate.jpa.metamodel.xml.jaxb.Embeddable;
+import org.hibernate.jpa.metamodel.xml.jaxb.EmbeddableAttributes;
 import org.hibernate.jpa.metamodel.xml.jaxb.Entity;
 import org.hibernate.jpa.metamodel.xml.jaxb.Id;
 import org.hibernate.jpa.metamodel.xml.jaxb.ManyToOne;
+import org.hibernate.jpa.metamodel.xml.jaxb.MappedSuperclass;
 import org.hibernate.jpa.metamodel.xml.jaxb.OneToMany;
-import org.hibernate.jpa.metamodel.xml.jaxb.Embeddable;
-import org.hibernate.jpa.metamodel.xml.jaxb.EmbeddableAttributes;
 
 /**
  * @author Hardy Ferentschik
@@ -64,10 +64,8 @@
 	final private List<IMetaAttribute> members = new ArrayList<IMetaAttribute>();
 
 	private TypeElement element;
-	private Context context;
 
-	public XmlMetaEntity(Entity ormEntity, String packageName, TypeElement element, Context context) {
-		this.context = context;
+	public XmlMetaEntity(Entity ormEntity, String packageName, TypeElement element) {
 		this.clazzName = ormEntity.getClazz();
 		this.packageName = packageName;
 		importContext = new ImportContextImpl( getPackageName() );
@@ -77,28 +75,17 @@
 		XmlMetaSingleAttribute attribute = new XmlMetaSingleAttribute( this, id.getName(), getType( id.getName() ) );
 		members.add( attribute );
 
-		for ( Basic basic : attributes.getBasic() ) {
-			attribute = new XmlMetaSingleAttribute( this, basic.getName(), getType( basic.getName() ) );
-			members.add( attribute );
-		}
+		parseAttributes( attributes );
+	}
 
-		for ( ManyToOne manyToOne : attributes.getManyToOne() ) {
-			attribute = new XmlMetaSingleAttribute( this, manyToOne.getName(), getType( manyToOne.getName() ) );
-			members.add( attribute );
-		}
+	public XmlMetaEntity(MappedSuperclass mappedSuperclass, String packageName, TypeElement element) {
+		this.clazzName = mappedSuperclass.getClazz();
+		this.packageName = packageName;
+		importContext = new ImportContextImpl( getPackageName() );
+		this.element = element;
+		Attributes attributes = mappedSuperclass.getAttributes();
 
-		XmlMetaCollection metaCollection;
-		for ( OneToMany oneToMany : attributes.getOneToMany() ) {
-			String[] types = getCollectionType( oneToMany.getName() );
-			metaCollection = new XmlMetaCollection( this, oneToMany.getName(), types[0], types[1] );
-			members.add( metaCollection );
-		}
-
-		for ( ElementCollection collection : attributes.getElementCollection() ) {
-			String[] types = getCollectionType( collection.getName() );
-			metaCollection = new XmlMetaCollection( this, collection.getName(), types[0], types[1] );
-			members.add( metaCollection );
-		}
+		parseAttributes( attributes );
 	}
 
 	public XmlMetaEntity(Embeddable embeddable, String packageName, TypeElement element) {
@@ -222,4 +209,30 @@
 		sb.append( '}' );
 		return sb.toString();
 	}
+
+	private void parseAttributes(Attributes attributes) {
+		XmlMetaSingleAttribute attribute;
+		for ( Basic basic : attributes.getBasic() ) {
+			attribute = new XmlMetaSingleAttribute( this, basic.getName(), getType( basic.getName() ) );
+			members.add( attribute );
+		}
+
+		for ( ManyToOne manyToOne : attributes.getManyToOne() ) {
+			attribute = new XmlMetaSingleAttribute( this, manyToOne.getName(), getType( manyToOne.getName() ) );
+			members.add( attribute );
+		}
+
+		XmlMetaCollection metaCollection;
+		for ( OneToMany oneToMany : attributes.getOneToMany() ) {
+			String[] types = getCollectionType( oneToMany.getName() );
+			metaCollection = new XmlMetaCollection( this, oneToMany.getName(), types[0], types[1] );
+			members.add( metaCollection );
+		}
+
+		for ( ElementCollection collection : attributes.getElementCollection() ) {
+			String[] types = getCollectionType( collection.getName() );
+			metaCollection = new XmlMetaCollection( this, collection.getName(), types[0], types[1] );
+			members.add( metaCollection );
+		}
+	}
 }

Copied: jpamodelgen/trunk/test/src/main/java/model/xmlmapped/Building.java (from rev 17055, jpamodelgen/trunk/test/src/main/java/model/Building.java)
===================================================================
--- jpamodelgen/trunk/test/src/main/java/model/xmlmapped/Building.java	                        (rev 0)
+++ jpamodelgen/trunk/test/src/main/java/model/xmlmapped/Building.java	2009-07-09 09:53:56 UTC (rev 17056)
@@ -0,0 +1,19 @@
+package model.xmlmapped;
+
+import model.Address;
+import model.Area;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class Building extends Area {
+	private Address address;
+
+	public Address getAddress() {
+		return address;
+	}
+
+	public void setAddress(Address address) {
+		this.address = address;
+	}
+}
\ No newline at end of file

Modified: jpamodelgen/trunk/test/src/main/resources/META-INF/address.xml
===================================================================
--- jpamodelgen/trunk/test/src/main/resources/META-INF/address.xml	2009-07-08 23:25:41 UTC (rev 17055)
+++ jpamodelgen/trunk/test/src/main/resources/META-INF/address.xml	2009-07-09 09:53:56 UTC (rev 17056)
@@ -16,13 +16,3 @@
     </embeddable>
 </entity-mappings>
 
-
-        <!--
-public class Order {
-	long id;
-	Set<Item> items;
-	boolean filled;
-	Date date;
-	List<String> notes;
-	Shop shop;
-} -->
\ No newline at end of file

Copied: jpamodelgen/trunk/test/src/main/resources/META-INF/building.xml (from rev 17055, jpamodelgen/trunk/test/src/main/resources/META-INF/address.xml)
===================================================================
--- jpamodelgen/trunk/test/src/main/resources/META-INF/building.xml	                        (rev 0)
+++ jpamodelgen/trunk/test/src/main/resources/META-INF/building.xml	2009-07-09 09:53:56 UTC (rev 17056)
@@ -0,0 +1,17 @@
+<?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_2_0.xsd"
+                 version="2.0"
+        >
+    <!-- careful old JPA1 schema -->
+    <package>model.xmlmapped</package>
+    <!-- default package -->
+    <mapped-superclass class="Building" access="FIELD" metadata-complete="true"> <!--means ignore annotations-->
+        <attributes>
+            <many-to-one name="address" fetch="LAZY"/>
+        </attributes>
+    </mapped-superclass>
+</entity-mappings>
+

Modified: jpamodelgen/trunk/test/src/main/resources/META-INF/persistence.xml
===================================================================
--- jpamodelgen/trunk/test/src/main/resources/META-INF/persistence.xml	2009-07-08 23:25:41 UTC (rev 17055)
+++ jpamodelgen/trunk/test/src/main/resources/META-INF/persistence.xml	2009-07-09 09:53:56 UTC (rev 17056)
@@ -6,5 +6,6 @@
         <description>Test persistence unit</description>
         <mapping-file>/META-INF/order.xml</mapping-file>
         <mapping-file>/META-INF/address.xml</mapping-file>
+        <mapping-file>/META-INF/building.xml</mapping-file>
     </persistence-unit>
 </persistence>

Modified: jpamodelgen/trunk/test/src/test/java/tests/XmlMappingTest.java
===================================================================
--- jpamodelgen/trunk/test/src/test/java/tests/XmlMappingTest.java	2009-07-08 23:25:41 UTC (rev 17055)
+++ jpamodelgen/trunk/test/src/test/java/tests/XmlMappingTest.java	2009-07-09 09:53:56 UTC (rev 17056)
@@ -6,10 +6,16 @@
 /**
  * @author Hardy Ferentschik
  */
- at Test
 public class XmlMappingTest {
 	@Test
-	public void testDefaultAccessType() throws Exception {
+	public void testXmlConfiguredEmbeddedClassGenerated() throws Exception {
 		assertNotNull( Class.forName( "model.xmlmapped.Address_" ) );
 	}
+
+	@Test
+	public void testXmlConfiguredMappedSuperclassGenerated() throws Exception {
+		Class<?> building =  Class.forName( "model.xmlmapped.Building_" );
+		assertNotNull( building );
+		assertNotNull( building.getField( "address" ));
+	}
 }
\ No newline at end of file




More information about the hibernate-commits mailing list