METAGEN-22 Some cleanup and optimisatons prior adding new functionality
This commit is contained in:
parent
6e039d710b
commit
9fb1f05259
|
@ -306,7 +306,7 @@ public class AnnotationMetaEntity implements MetaEntity {
|
||||||
keyType = typeMirror.toString();
|
keyType = typeMirror.toString();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
keyType = getKeyType( declaredType );
|
keyType = TypeUtils.getKeyType( declaredType, context );
|
||||||
}
|
}
|
||||||
return new AnnotationMetaMap(
|
return new AnnotationMetaMap(
|
||||||
parent,
|
parent,
|
||||||
|
@ -341,14 +341,6 @@ public class AnnotationMetaEntity implements MetaEntity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getKeyType(DeclaredType t) {
|
|
||||||
List<? extends TypeMirror> typeArguments = t.getTypeArguments();
|
|
||||||
if ( typeArguments.size() == 0 ) {
|
|
||||||
context.logMessage( Diagnostic.Kind.ERROR, "Entity: " + getQualifiedName() );
|
|
||||||
}
|
|
||||||
return TypeUtils.extractClosestRealTypeAsString( typeArguments.get( 0 ), context );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param annotations list of annotation mirrors.
|
* @param annotations list of annotation mirrors.
|
||||||
*
|
*
|
||||||
|
|
|
@ -20,7 +20,6 @@ package org.hibernate.jpamodelgen.annotation;
|
||||||
import javax.lang.model.element.Element;
|
import javax.lang.model.element.Element;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author Max Andersen
|
* @author Max Andersen
|
||||||
* @author Hardy Ferentschik
|
* @author Hardy Ferentschik
|
||||||
* @author Emmanuel Bernard
|
* @author Emmanuel Bernard
|
||||||
|
@ -36,7 +35,7 @@ public class AnnotationMetaMap extends AnnotationMetaCollection {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDeclarationString() {
|
public String getDeclarationString() {
|
||||||
return "public static volatile " + parent.importType(getMetaType()) + "<" + parent.importType(parent.getQualifiedName()) + ", " + parent.importType(keyType) + ", " + parent.importType(getTypeDeclaration()) + "> " + getPropertyName() + ";";
|
return "public static volatile " + parent.importType( getMetaType() ) + "<" + parent.importType( parent.getQualifiedName() ) + ", " + parent
|
||||||
|
.importType( keyType ) + ", " + parent.importType( getTypeDeclaration() ) + "> " + getPropertyName() + ";";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -380,8 +380,15 @@ public class TypeUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class EmbeddedAttributeVisitor extends SimpleTypeVisitor6<String, Element> {
|
public static String getKeyType(DeclaredType t, Context context) {
|
||||||
|
List<? extends TypeMirror> typeArguments = t.getTypeArguments();
|
||||||
|
if ( typeArguments.size() == 0 ) {
|
||||||
|
context.logMessage( Diagnostic.Kind.ERROR, "Unable to determine type argument for " + t );
|
||||||
|
}
|
||||||
|
return extractClosestRealTypeAsString( typeArguments.get( 0 ), context );
|
||||||
|
}
|
||||||
|
|
||||||
|
static class EmbeddedAttributeVisitor extends SimpleTypeVisitor6<String, Element> {
|
||||||
private Context context;
|
private Context context;
|
||||||
|
|
||||||
EmbeddedAttributeVisitor(Context context) {
|
EmbeddedAttributeVisitor(Context context) {
|
||||||
|
|
|
@ -25,11 +25,9 @@ import org.hibernate.jpamodelgen.model.MetaAttribute;
|
||||||
*/
|
*/
|
||||||
public abstract class XmlMetaAttribute implements MetaAttribute {
|
public abstract class XmlMetaAttribute implements MetaAttribute {
|
||||||
|
|
||||||
private XmlMetaEntity parentEntity;
|
protected final XmlMetaEntity parentEntity;
|
||||||
|
private final String propertyName;
|
||||||
private String propertyName;
|
private final String type;
|
||||||
|
|
||||||
private String type;
|
|
||||||
|
|
||||||
XmlMetaAttribute(XmlMetaEntity parent, String propertyName, String type) {
|
XmlMetaAttribute(XmlMetaEntity parent, String propertyName, String type) {
|
||||||
this.parentEntity = parent;
|
this.parentEntity = parent;
|
||||||
|
@ -39,7 +37,10 @@ public abstract class XmlMetaAttribute implements MetaAttribute {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getDeclarationString() {
|
public String getDeclarationString() {
|
||||||
return "public static volatile " + parentEntity.importType(getMetaType()) + "<" + parentEntity.importType(parentEntity.getQualifiedName()) + ", " + parentEntity.importType(getTypeDeclaration()) + "> " + getPropertyName() + ";";
|
return "public static volatile " + parentEntity.importType( getMetaType() )
|
||||||
|
+ "<" + parentEntity.importType( parentEntity.getQualifiedName() )
|
||||||
|
+ ", " + parentEntity.importType( getTypeDeclaration() )
|
||||||
|
+ "> " + getPropertyName() + ";";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPropertyName() {
|
public String getPropertyName() {
|
||||||
|
|
|
@ -48,6 +48,7 @@ import org.hibernate.jpamodelgen.xml.jaxb.Entity;
|
||||||
import org.hibernate.jpamodelgen.xml.jaxb.Id;
|
import org.hibernate.jpamodelgen.xml.jaxb.Id;
|
||||||
import org.hibernate.jpamodelgen.xml.jaxb.ManyToMany;
|
import org.hibernate.jpamodelgen.xml.jaxb.ManyToMany;
|
||||||
import org.hibernate.jpamodelgen.xml.jaxb.ManyToOne;
|
import org.hibernate.jpamodelgen.xml.jaxb.ManyToOne;
|
||||||
|
import org.hibernate.jpamodelgen.xml.jaxb.MapKeyClass;
|
||||||
import org.hibernate.jpamodelgen.xml.jaxb.MappedSuperclass;
|
import org.hibernate.jpamodelgen.xml.jaxb.MappedSuperclass;
|
||||||
import org.hibernate.jpamodelgen.xml.jaxb.OneToMany;
|
import org.hibernate.jpamodelgen.xml.jaxb.OneToMany;
|
||||||
import org.hibernate.jpamodelgen.xml.jaxb.OneToOne;
|
import org.hibernate.jpamodelgen.xml.jaxb.OneToOne;
|
||||||
|
@ -182,8 +183,8 @@ public class XmlMetaEntity implements MetaEntity {
|
||||||
return context.isPersistenceUnitCompletelyXmlConfigured() || Boolean.TRUE.equals( metadataComplete );
|
return context.isPersistenceUnitCompletelyXmlConfigured() || Boolean.TRUE.equals( metadataComplete );
|
||||||
}
|
}
|
||||||
|
|
||||||
private String[] getCollectionType(String propertyName, String explicitTargetEntity, ElementKind expectedElementKind) {
|
private String[] getCollectionTypes(String propertyName, String explicitTargetEntity, String explicitMapKeyClass, ElementKind expectedElementKind) {
|
||||||
String types[] = new String[2];
|
String types[] = new String[3];
|
||||||
for ( Element elem : element.getEnclosedElements() ) {
|
for ( Element elem : element.getEnclosedElements() ) {
|
||||||
if ( expectedElementKind.equals( elem.getKind() ) ) {
|
if ( expectedElementKind.equals( elem.getKind() ) ) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -194,6 +195,31 @@ public class XmlMetaEntity implements MetaEntity {
|
||||||
}
|
}
|
||||||
|
|
||||||
DeclaredType type = ( ( DeclaredType ) elem.asType() );
|
DeclaredType type = ( ( DeclaredType ) elem.asType() );
|
||||||
|
determineTargetType( type, propertyName, explicitTargetEntity, types );
|
||||||
|
determineCollectionType( type, types );
|
||||||
|
if ( types[1].equals( "javax.persistence.metamodel.MapAttribute" ) ) {
|
||||||
|
determineMapType( type, explicitMapKeyClass, types );
|
||||||
|
}
|
||||||
|
return types;
|
||||||
|
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void determineMapType(DeclaredType type, String explicitMapKeyClass, String[] types) {
|
||||||
|
if ( explicitMapKeyClass != null ) {
|
||||||
|
types[2] = explicitMapKeyClass;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
types[2] = TypeUtils.getKeyType( type, context );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void determineCollectionType(DeclaredType type, String[] types) {
|
||||||
|
types[1] = COLLECTIONS.get( type.asElement().toString() );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void determineTargetType(DeclaredType type, String propertyName, String explicitTargetEntity, String[] types) {
|
||||||
List<? extends TypeMirror> typeArguments = type.getTypeArguments();
|
List<? extends TypeMirror> typeArguments = type.getTypeArguments();
|
||||||
|
|
||||||
if ( typeArguments.size() == 0 && explicitTargetEntity == null ) {
|
if ( typeArguments.size() == 0 && explicitTargetEntity == null ) {
|
||||||
|
@ -206,11 +232,6 @@ public class XmlMetaEntity implements MetaEntity {
|
||||||
else {
|
else {
|
||||||
types[0] = explicitTargetEntity;
|
types[0] = explicitTargetEntity;
|
||||||
}
|
}
|
||||||
types[1] = COLLECTIONS.get( type.asElement().toString() );
|
|
||||||
return types;
|
|
||||||
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -302,101 +323,158 @@ public class XmlMetaEntity implements MetaEntity {
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( Basic basic : attributes.getBasic() ) {
|
for ( Basic basic : attributes.getBasic() ) {
|
||||||
ElementKind elementKind = getElementKind( basic.getAccess() );
|
parseBasic( basic );
|
||||||
String type = getType( basic.getName(), null, elementKind );
|
|
||||||
if ( type != null ) {
|
|
||||||
attribute = new XmlMetaSingleAttribute( this, basic.getName(), type );
|
|
||||||
members.add( attribute );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( ManyToOne manyToOne : attributes.getManyToOne() ) {
|
for ( ManyToOne manyToOne : attributes.getManyToOne() ) {
|
||||||
ElementKind elementKind = getElementKind( manyToOne.getAccess() );
|
parseManyToOne( manyToOne );
|
||||||
String type = getType( manyToOne.getName(), manyToOne.getTargetEntity(), elementKind );
|
|
||||||
if ( type != null ) {
|
|
||||||
attribute = new XmlMetaSingleAttribute( this, manyToOne.getName(), type );
|
|
||||||
members.add( attribute );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( OneToOne oneToOne : attributes.getOneToOne() ) {
|
for ( OneToOne oneToOne : attributes.getOneToOne() ) {
|
||||||
ElementKind elementKind = getElementKind( oneToOne.getAccess() );
|
parseOneToOne( oneToOne );
|
||||||
String type = getType( oneToOne.getName(), oneToOne.getTargetEntity(), elementKind );
|
|
||||||
if ( type != null ) {
|
|
||||||
attribute = new XmlMetaSingleAttribute( this, oneToOne.getName(), type );
|
|
||||||
members.add( attribute );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
XmlMetaCollection metaCollection;
|
|
||||||
String[] types;
|
|
||||||
for ( ManyToMany manyToMany : attributes.getManyToMany() ) {
|
for ( ManyToMany manyToMany : attributes.getManyToMany() ) {
|
||||||
ElementKind elementKind = getElementKind( manyToMany.getAccess() );
|
if ( parseManyToMany( manyToMany ) ) {
|
||||||
try {
|
|
||||||
types = getCollectionType( manyToMany.getName(), manyToMany.getTargetEntity(), elementKind );
|
|
||||||
}
|
|
||||||
catch ( MetaModelGenerationException e ) {
|
|
||||||
logMetaModelException( manyToMany.getName(), e );
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ( types != null ) {
|
|
||||||
metaCollection = new XmlMetaCollection( this, manyToMany.getName(), types[0], types[1] );
|
|
||||||
members.add( metaCollection );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( OneToMany oneToMany : attributes.getOneToMany() ) {
|
for ( OneToMany oneToMany : attributes.getOneToMany() ) {
|
||||||
ElementKind elementKind = getElementKind( oneToMany.getAccess() );
|
if ( parseOneToMany( oneToMany ) ) {
|
||||||
try {
|
|
||||||
types = getCollectionType( oneToMany.getName(), oneToMany.getTargetEntity(), elementKind );
|
|
||||||
}
|
|
||||||
catch ( MetaModelGenerationException e ) {
|
|
||||||
logMetaModelException( oneToMany.getName(), e );
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ( types != null ) {
|
|
||||||
metaCollection = new XmlMetaCollection( this, oneToMany.getName(), types[0], types[1] );
|
|
||||||
members.add( metaCollection );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( ElementCollection collection : attributes.getElementCollection() ) {
|
for ( ElementCollection collection : attributes.getElementCollection() ) {
|
||||||
ElementKind elementKind = getElementKind( collection.getAccess() );
|
if ( parseElementCollection( collection ) ) {
|
||||||
try {
|
|
||||||
types = getCollectionType( collection.getName(), collection.getTargetClass(), elementKind );
|
|
||||||
}
|
|
||||||
catch ( MetaModelGenerationException e ) {
|
|
||||||
logMetaModelException( collection.getName(), e );
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ( types != null ) {
|
|
||||||
metaCollection = new XmlMetaCollection( this, collection.getName(), types[0], types[1] );
|
|
||||||
members.add( metaCollection );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseEmbeddableAttributes(EmbeddableAttributes attributes) {
|
private void parseEmbeddableAttributes(EmbeddableAttributes attributes) {
|
||||||
XmlMetaSingleAttribute attribute;
|
|
||||||
for ( Basic basic : attributes.getBasic() ) {
|
for ( Basic basic : attributes.getBasic() ) {
|
||||||
ElementKind elementKind = getElementKind( basic.getAccess() );
|
parseBasic( basic );
|
||||||
String type = getType( basic.getName(), null, elementKind );
|
|
||||||
if ( type != null ) {
|
|
||||||
attribute = new XmlMetaSingleAttribute( this, basic.getName(), type );
|
|
||||||
members.add( attribute );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( ManyToOne manyToOne : attributes.getManyToOne() ) {
|
for ( ManyToOne manyToOne : attributes.getManyToOne() ) {
|
||||||
ElementKind elementKind = getElementKind( manyToOne.getAccess() );
|
parseManyToOne( manyToOne );
|
||||||
String type = getType( manyToOne.getName(), manyToOne.getTargetEntity(), elementKind );
|
|
||||||
if ( type != null ) {
|
|
||||||
attribute = new XmlMetaSingleAttribute( this, manyToOne.getName(), type );
|
|
||||||
members.add( attribute );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( OneToOne oneToOne : attributes.getOneToOne() ) {
|
for ( OneToOne oneToOne : attributes.getOneToOne() ) {
|
||||||
|
parseOneToOne( oneToOne );
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( ManyToMany manyToMany : attributes.getManyToMany() ) {
|
||||||
|
if ( parseManyToMany( manyToMany ) ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( OneToMany oneToMany : attributes.getOneToMany() ) {
|
||||||
|
if ( parseOneToMany( oneToMany ) ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( ElementCollection collection : attributes.getElementCollection() ) {
|
||||||
|
if ( parseElementCollection( collection ) ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean parseElementCollection(ElementCollection collection) {
|
||||||
|
String[] types;
|
||||||
|
XmlMetaCollection metaCollection;
|
||||||
|
ElementKind elementKind = getElementKind( collection.getAccess() );
|
||||||
|
MapKeyClass mapKeyClass = collection.getMapKeyClass();
|
||||||
|
String explicitMapKey = null;
|
||||||
|
if ( mapKeyClass != null ) {
|
||||||
|
explicitMapKey = mapKeyClass.getClazz();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
types = getCollectionTypes(
|
||||||
|
collection.getName(), collection.getTargetClass(), explicitMapKey, elementKind
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch ( MetaModelGenerationException e ) {
|
||||||
|
logMetaModelException( collection.getName(), e );
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ( types != null ) {
|
||||||
|
if ( types[2] == null ) {
|
||||||
|
metaCollection = new XmlMetaCollection( this, collection.getName(), types[0], types[1] );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
metaCollection = new XmlMetaMap( this, collection.getName(), types[0], types[1], types[2] );
|
||||||
|
}
|
||||||
|
members.add( metaCollection );
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean parseOneToMany(OneToMany oneToMany) {
|
||||||
|
String[] types;
|
||||||
|
XmlMetaCollection metaCollection;
|
||||||
|
ElementKind elementKind = getElementKind( oneToMany.getAccess() );
|
||||||
|
MapKeyClass mapKeyClass = oneToMany.getMapKeyClass();
|
||||||
|
String explicitMapKey = null;
|
||||||
|
if ( mapKeyClass != null ) {
|
||||||
|
explicitMapKey = mapKeyClass.getClazz();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
types = getCollectionTypes( oneToMany.getName(), oneToMany.getTargetEntity(), explicitMapKey, elementKind );
|
||||||
|
}
|
||||||
|
catch ( MetaModelGenerationException e ) {
|
||||||
|
logMetaModelException( oneToMany.getName(), e );
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ( types != null ) {
|
||||||
|
if ( types[2] == null ) {
|
||||||
|
metaCollection = new XmlMetaCollection( this, oneToMany.getName(), types[0], types[1] );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
metaCollection = new XmlMetaMap( this, oneToMany.getName(), types[0], types[1], types[2] );
|
||||||
|
}
|
||||||
|
members.add( metaCollection );
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean parseManyToMany(ManyToMany manyToMany) {
|
||||||
|
String[] types;
|
||||||
|
XmlMetaCollection metaCollection;
|
||||||
|
ElementKind elementKind = getElementKind( manyToMany.getAccess() );
|
||||||
|
MapKeyClass mapKeyClass = manyToMany.getMapKeyClass();
|
||||||
|
String explicitMapKey = null;
|
||||||
|
if ( mapKeyClass != null ) {
|
||||||
|
explicitMapKey = mapKeyClass.getClazz();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
types = getCollectionTypes(
|
||||||
|
manyToMany.getName(), manyToMany.getTargetEntity(), explicitMapKey, elementKind
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch ( MetaModelGenerationException e ) {
|
||||||
|
logMetaModelException( manyToMany.getName(), e );
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ( types != null ) {
|
||||||
|
if ( types[2] == null ) {
|
||||||
|
metaCollection = new XmlMetaCollection( this, manyToMany.getName(), types[0], types[1] );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
metaCollection = new XmlMetaMap( this, manyToMany.getName(), types[0], types[1], types[2] );
|
||||||
|
}
|
||||||
|
members.add( metaCollection );
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void parseOneToOne(OneToOne oneToOne) {
|
||||||
|
XmlMetaSingleAttribute attribute;
|
||||||
ElementKind elementKind = getElementKind( oneToOne.getAccess() );
|
ElementKind elementKind = getElementKind( oneToOne.getAccess() );
|
||||||
String type = getType( oneToOne.getName(), oneToOne.getTargetEntity(), elementKind );
|
String type = getType( oneToOne.getName(), oneToOne.getTargetEntity(), elementKind );
|
||||||
if ( type != null ) {
|
if ( type != null ) {
|
||||||
|
@ -405,51 +483,23 @@ public class XmlMetaEntity implements MetaEntity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
XmlMetaCollection metaCollection;
|
private void parseManyToOne(ManyToOne manyToOne) {
|
||||||
String[] types;
|
XmlMetaSingleAttribute attribute;
|
||||||
for ( ManyToMany manyToMany : attributes.getManyToMany() ) {
|
ElementKind elementKind = getElementKind( manyToOne.getAccess() );
|
||||||
ElementKind elementKind = getElementKind( manyToMany.getAccess() );
|
String type = getType( manyToOne.getName(), manyToOne.getTargetEntity(), elementKind );
|
||||||
try {
|
if ( type != null ) {
|
||||||
types = getCollectionType( manyToMany.getName(), manyToMany.getTargetEntity(), elementKind );
|
attribute = new XmlMetaSingleAttribute( this, manyToOne.getName(), type );
|
||||||
}
|
members.add( attribute );
|
||||||
catch ( MetaModelGenerationException e ) {
|
|
||||||
logMetaModelException( manyToMany.getName(), e );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if ( types != null ) {
|
|
||||||
metaCollection = new XmlMetaCollection( this, manyToMany.getName(), types[0], types[1] );
|
|
||||||
members.add( metaCollection );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( OneToMany oneToMany : attributes.getOneToMany() ) {
|
private void parseBasic(Basic basic) {
|
||||||
ElementKind elementKind = getElementKind( oneToMany.getAccess() );
|
XmlMetaSingleAttribute attribute;
|
||||||
try {
|
ElementKind elementKind = getElementKind( basic.getAccess() );
|
||||||
types = getCollectionType( oneToMany.getName(), oneToMany.getTargetEntity(), elementKind );
|
String type = getType( basic.getName(), null, elementKind );
|
||||||
}
|
if ( type != null ) {
|
||||||
catch ( MetaModelGenerationException e ) {
|
attribute = new XmlMetaSingleAttribute( this, basic.getName(), type );
|
||||||
logMetaModelException( oneToMany.getName(), e );
|
members.add( attribute );
|
||||||
break;
|
|
||||||
}
|
|
||||||
if ( types != null ) {
|
|
||||||
metaCollection = new XmlMetaCollection( this, oneToMany.getName(), types[0], types[1] );
|
|
||||||
members.add( metaCollection );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for ( ElementCollection collection : attributes.getElementCollection() ) {
|
|
||||||
ElementKind elementKind = getElementKind( collection.getAccess() );
|
|
||||||
try {
|
|
||||||
types = getCollectionType( collection.getName(), collection.getTargetClass(), elementKind );
|
|
||||||
}
|
|
||||||
catch ( MetaModelGenerationException e ) {
|
|
||||||
logMetaModelException( collection.getName(), e );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if ( types != null ) {
|
|
||||||
metaCollection = new XmlMetaCollection( this, collection.getName(), types[0], types[1] );
|
|
||||||
members.add( metaCollection );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
// $Id$
|
||||||
|
/*
|
||||||
|
* JBoss, Home of Professional Open Source
|
||||||
|
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
|
||||||
|
* by the @authors tag. See the copyright.txt in the distribution for a
|
||||||
|
* full listing of individual contributors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.hibernate.jpamodelgen.xml;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Hardy Ferentschik
|
||||||
|
*/
|
||||||
|
public class XmlMetaMap extends XmlMetaCollection {
|
||||||
|
|
||||||
|
private final String keyType;
|
||||||
|
|
||||||
|
public XmlMetaMap(XmlMetaEntity parent, String propertyName, String type, String collectionType, String keyType) {
|
||||||
|
super( parent, propertyName, type, collectionType );
|
||||||
|
this.keyType = keyType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDeclarationString() {
|
||||||
|
return "public static volatile "
|
||||||
|
+ parentEntity.importType( getMetaType() )
|
||||||
|
+ "<" + parentEntity.importType( parentEntity.getQualifiedName() )
|
||||||
|
+ ", " + parentEntity.importType( keyType ) + ", "
|
||||||
|
+ parentEntity.importType( getTypeDeclaration() )
|
||||||
|
+ "> " + getPropertyName() + ";";
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,6 +18,7 @@
|
||||||
package org.hibernate.jpamodelgen.test.elementcollection;
|
package org.hibernate.jpamodelgen.test.elementcollection;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Hardy Ferentschik
|
* @author Hardy Ferentschik
|
||||||
|
@ -28,6 +29,7 @@ public class Cleaner {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
@Id
|
||||||
public int getId() {
|
public int getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,9 +17,14 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.jpamodelgen.test.elementcollection;
|
package org.hibernate.jpamodelgen.test.elementcollection;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import org.hibernate.jpamodelgen.test.util.CompilationTest;
|
import org.hibernate.jpamodelgen.test.util.CompilationTest;
|
||||||
|
import org.hibernate.jpamodelgen.test.util.TestUtil;
|
||||||
|
|
||||||
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMapAttributesInMetaModelFor;
|
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMapAttributesInMetaModelFor;
|
||||||
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
|
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
|
||||||
|
@ -55,8 +60,30 @@ public class ElementCollectionTest extends CompilationTest {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* METAGEN-22
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testMapKeyClassXmlConfigured() {
|
||||||
|
assertMetamodelClassGeneratedFor( Hostel.class );
|
||||||
|
assertMapAttributesInMetaModelFor(
|
||||||
|
Hostel.class, "roomsByName", String.class, Room.class, "Wrong type in map attribute."
|
||||||
|
);
|
||||||
|
|
||||||
|
assertMapAttributesInMetaModelFor(
|
||||||
|
Hostel.class, "cleaners", Room.class, Cleaner.class, "Wrong type in map attribute."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPackageNameOfTestSources() {
|
protected String getPackageNameOfTestSources() {
|
||||||
return ElementCollectionTest.class.getPackage().getName();
|
return ElementCollectionTest.class.getPackage().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Collection<String> getOrmFiles() {
|
||||||
|
List<String> ormFiles = new ArrayList<String>();
|
||||||
|
ormFiles.add( TestUtil.fcnToPath( ElementCollectionTest.class.getPackage().getName() ) + "/hostel.xml" );
|
||||||
|
return ormFiles;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
// $Id$
|
||||||
|
/*
|
||||||
|
* JBoss, Home of Professional Open Source
|
||||||
|
* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
|
||||||
|
* by the @authors tag. See the copyright.txt in the distribution for a
|
||||||
|
* full listing of individual contributors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.hibernate.jpamodelgen.test.elementcollection;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import javax.persistence.ElementCollection;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.MapKeyClass;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Hardy Ferentschik
|
||||||
|
*/
|
||||||
|
public class Hostel {
|
||||||
|
private Map roomsByName;
|
||||||
|
|
||||||
|
private Map cleaners;
|
||||||
|
|
||||||
|
@ElementCollection(targetClass = Room.class)
|
||||||
|
@MapKeyClass(String.class)
|
||||||
|
public Map getRoomsByName() {
|
||||||
|
return roomsByName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoomsByName(Map roomsByName) {
|
||||||
|
this.roomsByName = roomsByName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map getCleaners() {
|
||||||
|
return cleaners;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCleaners(Map cleaners) {
|
||||||
|
this.cleaners = cleaners;
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,6 +20,8 @@ package org.hibernate.jpamodelgen.test.elementcollection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.persistence.ElementCollection;
|
import javax.persistence.ElementCollection;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
import javax.persistence.MapKeyClass;
|
import javax.persistence.MapKeyClass;
|
||||||
import javax.persistence.OneToMany;
|
import javax.persistence.OneToMany;
|
||||||
|
|
||||||
|
@ -29,10 +31,20 @@ import javax.persistence.OneToMany;
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
public class Hotel {
|
public class Hotel {
|
||||||
|
private int id;
|
||||||
private Map roomsByName;
|
private Map roomsByName;
|
||||||
|
|
||||||
private Map cleaners;
|
private Map cleaners;
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
@ElementCollection(targetClass = Room.class)
|
@ElementCollection(targetClass = Room.class)
|
||||||
@MapKeyClass(String.class)
|
@MapKeyClass(String.class)
|
||||||
public Map getRoomsByName() {
|
public Map getRoomsByName() {
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?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"
|
||||||
|
>
|
||||||
|
<entity class="org.hibernate.jpamodelgen.test.elementcollection.Hostel">
|
||||||
|
<attributes>
|
||||||
|
<one-to-many name="cleaners" target-entity="org.hibernate.jpamodelgen.test.elementcollection.Cleaner">
|
||||||
|
<map-key-class class="org.hibernate.jpamodelgen.test.elementcollection.Room"/>
|
||||||
|
</one-to-many>
|
||||||
|
<element-collection name="roomsByName"
|
||||||
|
target-class="org.hibernate.jpamodelgen.test.elementcollection.Room">
|
||||||
|
<map-key-class class="java.lang.String"/>
|
||||||
|
</element-collection>
|
||||||
|
</attributes>
|
||||||
|
</entity>
|
||||||
|
</entity-mappings>
|
||||||
|
|
Loading…
Reference in New Issue