Merge commit 'upstream/master' into HHH-6176

This commit is contained in:
Michal Skowronek 2011-05-17 00:25:18 +02:00
commit 3b1cd7638d
20 changed files with 708 additions and 146 deletions

View File

@ -23,15 +23,12 @@
*/
package org.hibernate.metamodel.binding;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.metamodel.domain.Attribute;
import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.relational.SimpleValue;
import org.hibernate.metamodel.relational.Size;
import org.hibernate.metamodel.relational.TableSpecification;
import org.hibernate.metamodel.relational.Value;
@ -86,8 +83,9 @@ public interface AttributeBinding {
public Iterable<SimpleValue> getValues();
/**
* @deprecated Use {@link #getValue()}.{@link Value#getTable() getTable()} instead; to be removed on completion of new metamodel code
* @return
*
* @deprecated Use {@link #getValue()}.{@link Value#getTable() getTable()} instead; to be removed on completion of new metamodel code
*/
@Deprecated
public TableSpecification getTable();
@ -95,19 +93,24 @@ public interface AttributeBinding {
public String getPropertyAccessorName();
/**
*
* @return
*/
public boolean hasFormula();
public boolean isAlternateUniqueKey();
public boolean isNullable();
public boolean[] getColumnUpdateability();
public boolean[] getColumnInsertability();
public boolean isSimpleValue();
public boolean isLazy();
public void addEntityReferencingAttributeBinding(EntityReferencingAttributeBinding attributeBinding);
public Set<EntityReferencingAttributeBinding> getEntityReferencingAttributeBindings();
public void validate();

View File

@ -30,6 +30,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.AssertionFailure;
import org.hibernate.MappingException;
import org.hibernate.engine.internal.Versioning;
import org.hibernate.internal.util.ReflectHelper;
@ -49,9 +50,11 @@ import org.hibernate.metamodel.source.util.MappingHelper;
* Provides the link between the domain and the relational model for an entity.
*
* @author Steve Ebersole
* @author Hardy Ferentschik
*/
public class EntityBinding {
private final EntityIdentifier entityIdentifier = new EntityIdentifier( this );
private InheritanceType entityInheritanceType;
private EntityDiscriminator entityDiscriminator;
private SimpleAttributeBinding versionBinding;
@ -214,6 +217,13 @@ public class EntityBinding {
baseTable.getPrimaryKey().addColumn( Column.class.cast( attributeBinding.getValue() ) );
}
public void setInheritanceType(InheritanceType entityInheritanceType) {
this.entityInheritanceType = entityInheritanceType;
}
public InheritanceType getInheritanceType() {
return entityInheritanceType;
}
public SimpleAttributeBinding getVersioningValueBinding() {
return versionBinding;
@ -239,9 +249,9 @@ public class EntityBinding {
public SimpleAttributeBinding makeEntityDiscriminatorBinding(String name) {
if ( entityDiscriminator != null ) {
// TODO: LOG this!!!
throw new AssertionFailure( "Creation of entity discriminator was called more than once" );
}
entityDiscriminator = new EntityDiscriminator( this );
entityDiscriminator = new EntityDiscriminator();
entityDiscriminator.setValueBinding( makeSimpleAttributeBinding( name, true, false ) );
return entityDiscriminator.getValueBinding();
}

View File

@ -24,18 +24,16 @@
package org.hibernate.metamodel.binding;
/**
* TODO : javadoc
* Binding of the discriminator in a entity hierarchy
*
* @author Steve Ebersole
*/
public class EntityDiscriminator {
private final EntityBinding entityBinding;
private SimpleAttributeBinding valueBinding;
private boolean forced;
private boolean inserted = true;
public EntityDiscriminator(EntityBinding entityBinding) {
this.entityBinding = entityBinding;
public EntityDiscriminator() {
}
public SimpleAttributeBinding getValueBinding() {
@ -61,4 +59,15 @@ public class EntityDiscriminator {
public void setInserted(boolean inserted) {
this.inserted = inserted;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append( "EntityDiscriminator" );
sb.append( "{valueBinding=" ).append( valueBinding );
sb.append( ", forced=" ).append( forced );
sb.append( ", inserted=" ).append( inserted );
sb.append( '}' );
return sb.toString();
}
}

View File

@ -0,0 +1,63 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.binding;
import org.hibernate.MappingException;
/**
* The inheritance type for a given entity.
* <p>
* Note, we are not using the JPA enum, because we need the ability to extend the types if we need to.
*
* @author Hardy Ferentschik
*/
public enum InheritanceType {
JOINED,
SINGLE_TABLE,
TABLE_PER_CLASS,
NO_INHERITANCE;
/**
* @param jpaType The JPA inheritance type
*
* @return The inheritance type of this class.
*/
public static InheritanceType get(javax.persistence.InheritanceType jpaType) {
switch ( jpaType ) {
case SINGLE_TABLE: {
return InheritanceType.SINGLE_TABLE;
}
case JOINED: {
return InheritanceType.JOINED;
}
case TABLE_PER_CLASS: {
return InheritanceType.TABLE_PER_CLASS;
}
default: {
throw new MappingException( "Unknown jpa inheritance type:" + jpaType.name() );
}
}
}
}

View File

@ -33,7 +33,7 @@ import org.hibernate.AssertionFailure;
*
* @author Hardy Ferentschik
*/
public final class ColumnValues {
public class ColumnValues {
private String name = "";
private boolean unique = false;
private boolean nullable = true;
@ -45,14 +45,18 @@ public final class ColumnValues {
private int precision = 0;
private int scale = 0;
public ColumnValues(AnnotationInstance columnAnnotation, boolean isId) {
public ColumnValues() {
this( null );
}
public ColumnValues(AnnotationInstance columnAnnotation) {
if ( columnAnnotation != null && !JPADotNames.COLUMN.equals( columnAnnotation.name() ) ) {
throw new AssertionFailure( "A @Column annotation needs to be passed to the constructor" );
}
applyColumnValues( columnAnnotation, isId );
applyColumnValues( columnAnnotation );
}
private void applyColumnValues(AnnotationInstance columnAnnotation, boolean isId) {
private void applyColumnValues(AnnotationInstance columnAnnotation) {
if ( columnAnnotation == null ) {
return;
}
@ -62,26 +66,14 @@ public final class ColumnValues {
this.name = nameValue.asString();
}
// id attribute must be unique
if ( isId ) {
this.unique = true;
}
else {
AnnotationValue uniqueValue = columnAnnotation.value( "unique" );
if ( uniqueValue != null ) {
this.unique = nameValue.asBoolean();
}
AnnotationValue uniqueValue = columnAnnotation.value( "unique" );
if ( uniqueValue != null ) {
this.unique = nameValue.asBoolean();
}
// id attribute cannot be nullable
if ( isId ) {
this.nullable = false;
}
else {
AnnotationValue nullableValue = columnAnnotation.value( "nullable" );
if ( nullableValue != null ) {
this.nullable = nullableValue.asBoolean();
}
AnnotationValue nullableValue = columnAnnotation.value( "nullable" );
if ( nullableValue != null ) {
this.nullable = nullableValue.asBoolean();
}
AnnotationValue insertableValue = columnAnnotation.value( "insertable" );
@ -160,6 +152,46 @@ public final class ColumnValues {
return scale;
}
public void setName(String name) {
this.name = name;
}
public void setUnique(boolean unique) {
this.unique = unique;
}
public void setNullable(boolean nullable) {
this.nullable = nullable;
}
public void setInsertable(boolean insertable) {
this.insertable = insertable;
}
public void setUpdatable(boolean updatable) {
this.updatable = updatable;
}
public void setColumnDefinition(String columnDefinition) {
this.columnDefinition = columnDefinition;
}
public void setTable(String table) {
this.table = table;
}
public void setLength(int length) {
this.length = length;
}
public void setPrecision(int precision) {
this.precision = precision;
}
public void setScale(int scale) {
this.scale = scale;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();

View File

@ -35,7 +35,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.AccessType;
import javax.persistence.InheritanceType;
import com.fasterxml.classmate.ResolvedTypeWithMembers;
import com.fasterxml.classmate.members.HierarchicType;
@ -48,8 +47,11 @@ import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.MethodInfo;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.metamodel.binding.InheritanceType;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.annotations.util.ReflectionHelper;
import org.hibernate.service.ServiceRegistry;
@ -76,7 +78,8 @@ public class ConfiguredClass {
private final boolean hasOwnTable;
private final String primaryTableName;
private final ConfiguredClassType type;
private final ConfiguredClassType configuredClassType;
private final IdType idType;
private final Map<String, MappedAttribute> mappedAttributes;
@ -93,8 +96,9 @@ public class ConfiguredClass {
this.inheritanceType = inheritanceType;
this.clazz = serviceRegistry.getService( ClassLoaderService.class ).classForName( info.toString() );
this.type = determineType();
this.configuredClassType = determineType();
this.classAccessType = determineClassAccessType();
this.idType = determineIdType();
this.hasOwnTable = definesItsOwnTable();
this.primaryTableName = determinePrimaryTableName();
@ -113,6 +117,10 @@ public class ConfiguredClass {
return clazz.getName();
}
public String getSimpleName() {
return clazz.getSimpleName();
}
public ClassInfo getClassInfo() {
return classInfo;
}
@ -125,14 +133,18 @@ public class ConfiguredClass {
return isRoot;
}
public ConfiguredClassType getType() {
return type;
public ConfiguredClassType getConfiguredClassType() {
return configuredClassType;
}
public InheritanceType getInheritanceType() {
return inheritanceType;
}
public IdType getIdType() {
return idType;
}
public boolean hasOwnTable() {
return hasOwnTable;
}
@ -154,7 +166,7 @@ public class ConfiguredClass {
final StringBuilder sb = new StringBuilder();
sb.append( "ConfiguredClass" );
sb.append( "{clazz=" ).append( clazz.getSimpleName() );
sb.append( ", type=" ).append( type );
sb.append( ", type=" ).append( configuredClassType );
sb.append( ", classAccessType=" ).append( classAccessType );
sb.append( ", isRoot=" ).append( isRoot );
sb.append( ", inheritanceType=" ).append( inheritanceType );
@ -367,7 +379,7 @@ public class ConfiguredClass {
final Map<DotName, List<AnnotationInstance>> annotations = JandexHelper.getMemberAnnotations(
classInfo, member.getName()
);
return new MappedAttribute( name, (Class) type, annotations );
return MappedAttribute.createMappedAttribute( name, (Class) type, annotations );
}
private Type findResolvedType(String name, ResolvedMember[] resolvedMembers) {
@ -405,8 +417,8 @@ public class ConfiguredClass {
private boolean definesItsOwnTable() {
// mapped super classes and embeddables don't have their own tables
if ( ConfiguredClassType.MAPPED_SUPERCLASS.equals( getType() ) || ConfiguredClassType.EMBEDDABLE
.equals( getType() ) ) {
if ( ConfiguredClassType.MAPPED_SUPERCLASS.equals( getConfiguredClassType() ) || ConfiguredClassType.EMBEDDABLE
.equals( getConfiguredClassType() ) ) {
return false;
}
@ -437,12 +449,44 @@ public class ConfiguredClass {
}
}
else if ( parent != null
&& !parent.getType().equals( ConfiguredClassType.MAPPED_SUPERCLASS )
&& !parent.getType().equals( ConfiguredClassType.EMBEDDABLE ) ) {
&& !parent.getConfiguredClassType().equals( ConfiguredClassType.MAPPED_SUPERCLASS )
&& !parent.getConfiguredClassType().equals( ConfiguredClassType.EMBEDDABLE ) ) {
tableName = parent.getPrimaryTableName();
}
return tableName;
}
private IdType determineIdType() {
List<AnnotationInstance> idAnnotations = getClassInfo().annotations().get( JPADotNames.ENTITY );
List<AnnotationInstance> embeddedIdAnnotations = getClassInfo()
.annotations()
.get( JPADotNames.EMBEDDED_ID );
if ( idAnnotations != null && embeddedIdAnnotations != null ) {
throw new MappingException(
"@EmbeddedId and @Id cannot be used together. Check the configuration for " + getName() + "."
);
}
if ( embeddedIdAnnotations != null ) {
if ( embeddedIdAnnotations.size() == 1 ) {
return IdType.EMBEDDED;
}
else {
throw new AnnotationException( "Multiple @EmbeddedId annotations are not allowed" );
}
}
if ( idAnnotations != null ) {
if ( idAnnotations.size() == 1 ) {
return IdType.SIMPLE;
}
else {
return IdType.COMPOSED;
}
}
return IdType.NONE;
}
}

View File

@ -27,7 +27,6 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.AccessType;
import javax.persistence.InheritanceType;
import com.fasterxml.classmate.ResolvedTypeWithMembers;
import org.jboss.jandex.AnnotationInstance;
@ -36,6 +35,7 @@ import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.MethodInfo;
import org.hibernate.AnnotationException;
import org.hibernate.metamodel.binding.InheritanceType;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.annotations.util.ReflectionHelper;
import org.hibernate.service.ServiceRegistry;
@ -150,6 +150,10 @@ public class ConfiguredClassHierarchy implements Iterable<ConfiguredClass> {
}
private InheritanceType determineInheritanceType(List<ClassInfo> classes) {
if ( classes.size() == 1 ) {
return InheritanceType.NO_INHERITANCE;
}
InheritanceType inheritanceType = null;
for ( ClassInfo info : classes ) {
AnnotationInstance inheritanceAnnotation = JandexHelper.getSingleAnnotation(
@ -159,9 +163,10 @@ public class ConfiguredClassHierarchy implements Iterable<ConfiguredClass> {
continue;
}
InheritanceType tmpInheritanceType = Enum.valueOf(
InheritanceType.class, inheritanceAnnotation.value( "strategy" ).asEnum()
javax.persistence.InheritanceType jpaInheritanceType = Enum.valueOf(
javax.persistence.InheritanceType.class, inheritanceAnnotation.value( "strategy" ).asEnum()
);
InheritanceType tmpInheritanceType = InheritanceType.get( jpaInheritanceType );
if ( tmpInheritanceType == null ) {
// default inheritance type is single table
inheritanceType = InheritanceType.SINGLE_TABLE;

View File

@ -0,0 +1,113 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.source.annotations;
import java.util.List;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.DotName;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
/**
* Container for the properties of a discriminator column.
*
* @author Hardy Ferentschik
*/
public class DiscriminatorColumnValues extends ColumnValues {
public static final String DEFAULT_DISCRIMINATOR_COLUMN_NAME = "DTYPE";
private static final int DEFAULT_DISCRIMINATOR_LENGTH = 31;
private boolean isForced = true;
private boolean isIncludedInSql = true;
private String discriminatorValue = null;
public DiscriminatorColumnValues(Map<DotName, List<AnnotationInstance>> annotations) {
super();
AnnotationInstance discriminatorOptionsAnnotation = JandexHelper.getSingleAnnotation(
annotations, JPADotNames.DISCRIMINATOR_COLUMN
);
if ( discriminatorOptionsAnnotation != null ) {
setName( discriminatorOptionsAnnotation.value( "name" ).asString() );
setLength( discriminatorOptionsAnnotation.value( "length" ).asInt() );
if ( discriminatorOptionsAnnotation.value( "columnDefinition" ) != null ) {
setColumnDefinition( discriminatorOptionsAnnotation.value( "columnDefinition" ).asString() );
}
}
else {
setName( DEFAULT_DISCRIMINATOR_COLUMN_NAME );
setLength( DEFAULT_DISCRIMINATOR_LENGTH );
}
setNullable( false );
setDiscriminatorValue( annotations );
setDiscriminatorOptions( annotations );
setDiscriminatorFormula( annotations );
}
private void setDiscriminatorValue(Map<DotName, List<AnnotationInstance>> annotations) {
AnnotationInstance discriminatorValueAnnotation = JandexHelper.getSingleAnnotation(
annotations, JPADotNames.DISCRIMINATOR_VALUE
);
if ( discriminatorValueAnnotation != null ) {
discriminatorValue = discriminatorValueAnnotation.value().asString();
}
}
private void setDiscriminatorFormula(Map<DotName, List<AnnotationInstance>> annotations) {
AnnotationInstance discriminatorFormulaAnnotation = JandexHelper.getSingleAnnotation(
annotations, HibernateDotNames.DISCRIMINATOR_FORMULA
);
if ( discriminatorFormulaAnnotation != null ) {
// todo
}
}
public boolean isForced() {
return isForced;
}
public boolean isIncludedInSql() {
return isIncludedInSql;
}
public String getDiscriminatorValue() {
return discriminatorValue;
}
private void setDiscriminatorOptions(Map<DotName, List<AnnotationInstance>> annotations) {
AnnotationInstance discriminatorOptionsAnnotation = JandexHelper.getSingleAnnotation(
annotations, HibernateDotNames.DISCRIMINATOR_OPTIONS
);
if ( discriminatorOptionsAnnotation != null ) {
isForced = discriminatorOptionsAnnotation.value( "force" ).asBoolean();
isIncludedInSql = discriminatorOptionsAnnotation.value( "insert" ).asBoolean();
}
}
}

View File

@ -23,14 +23,11 @@
*/
package org.hibernate.metamodel.source.annotations;
import java.util.List;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.MappingException;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.OptimisticLockType;
import org.hibernate.annotations.PolymorphismType;
@ -69,14 +66,21 @@ public class EntityBinder {
public void bind() {
EntityBinding entityBinding = new EntityBinding();
bindJpaEntityAnnotation( entityBinding );
bindHibernateEntityAnnotation( entityBinding ); // optional hibernate specific @org.hibernate.annotations.Entity
bindWhereFilter( entityBinding );
bindJpaCaching( entityBinding );
bindHibernateCaching( entityBinding );
schemaName = createSchemaName();
bindTable( entityBinding );
entityBinding.setInheritanceType( configuredClass.getInheritanceType() );
bindInheritance( entityBinding );
bindWhereFilter( entityBinding );
bindJpaCaching( entityBinding );
bindHibernateCaching( entityBinding );
if ( configuredClass.isRoot() ) {
bindId( entityBinding );
}
@ -85,6 +89,42 @@ public class EntityBinder {
meta.addEntity( entityBinding );
}
private void bindInheritance(EntityBinding entityBinding) {
switch ( configuredClass.getInheritanceType() ) {
case SINGLE_TABLE: {
bindDiscriminatorColumn( entityBinding );
break;
}
case JOINED: {
// todo
break;
}
case TABLE_PER_CLASS: {
// todo
break;
}
default: {
// do nothing
}
}
}
private void bindDiscriminatorColumn(EntityBinding entityBinding) {
MappedAttribute discriminatorAttribute = MappedAttribute.createDiscriminatorAttribute(
configuredClass.getClassInfo().annotations()
);
bindSingleMappedAttribute( entityBinding, discriminatorAttribute );
if ( !( discriminatorAttribute.getColumnValues() instanceof DiscriminatorColumnValues ) ) {
throw new AssertionFailure( "Expected discriminator column values" );
}
DiscriminatorColumnValues discriminatorColumnvalues = (DiscriminatorColumnValues) discriminatorAttribute.getColumnValues();
entityBinding.getEntityDiscriminator().setForced( discriminatorColumnvalues.isForced() );
entityBinding.getEntityDiscriminator().setInserted( discriminatorColumnvalues.isIncludedInSql() );
entityBinding.setDiscriminatorValue( discriminatorColumnvalues.getDiscriminatorValue() );
}
private void bindWhereFilter(EntityBinding entityBinding) {
AnnotationInstance whereAnnotation = JandexHelper.getSingleAnnotation(
configuredClass.getClassInfo(), HibernateDotNames.WHERE
@ -216,7 +256,7 @@ public class EntityBinder {
}
private void bindId(EntityBinding entityBinding) {
switch ( determineIdType() ) {
switch ( configuredClass.getIdType() ) {
case SIMPLE: {
bindSingleIdAnnotation( entityBinding );
break;
@ -270,24 +310,33 @@ public class EntityBinder {
private void bindAttributes(EntityBinding entityBinding) {
for ( MappedAttribute mappedAttribute : configuredClass.getMappedAttributes() ) {
if ( mappedAttribute.isId() ) {
continue;
}
bindSingleMappedAttribute( entityBinding, mappedAttribute );
}
}
String attributeName = mappedAttribute.getName();
entityBinding.getEntity().getOrCreateSingularAttribute( attributeName );
SimpleAttributeBinding attributeBinding;
private void bindSingleMappedAttribute(EntityBinding entityBinding, MappedAttribute mappedAttribute) {
if ( mappedAttribute.isId() ) {
return;
}
if ( mappedAttribute.isVersioned() ) {
attributeBinding = entityBinding.makeVersionBinding( attributeName );
}
else {
attributeBinding = entityBinding.makeSimpleAttributeBinding( attributeName );
}
String attributeName = mappedAttribute.getName();
entityBinding.getEntity().getOrCreateSingularAttribute( attributeName );
SimpleAttributeBinding attributeBinding;
AttributeDomainState domainState = new AttributeDomainState( entityBinding, mappedAttribute );
attributeBinding.initialize( domainState );
if ( mappedAttribute.isVersioned() ) {
attributeBinding = entityBinding.makeVersionBinding( attributeName );
}
else if ( mappedAttribute.isDiscriminator() ) {
attributeBinding = entityBinding.makeEntityDiscriminatorBinding( attributeName );
}
else {
attributeBinding = entityBinding.makeSimpleAttributeBinding( attributeName );
}
AttributeDomainState domainState = new AttributeDomainState( entityBinding, mappedAttribute );
attributeBinding.initialize( domainState );
if ( configuredClass.hasOwnTable() ) {
AttributeColumnRelationalState columnRelationsState = new AttributeColumnRelationalState(
mappedAttribute, meta
);
@ -368,59 +417,14 @@ public class EntityBinder {
return null;
}
EntityBinding parentBinding = meta.getEntityBinding( parent.getName() );
EntityBinding parentBinding = meta.getEntityBinding( parent.getSimpleName() );
if ( parentBinding == null ) {
throw new AssertionFailure(
"Parent entity " + parent.getName() + " of entity " + configuredClass.getName() + "not yet created!"
"Parent entity " + parent.getName() + " of entity " + configuredClass.getName() + " not yet created!"
);
}
return parentBinding.getEntity();
}
private IdType determineIdType() {
List<AnnotationInstance> idAnnotations = configuredClass.getClassInfo().annotations().get( JPADotNames.ENTITY );
List<AnnotationInstance> embeddedIdAnnotations = configuredClass.getClassInfo()
.annotations()
.get( JPADotNames.EMBEDDED_ID );
if ( idAnnotations != null && embeddedIdAnnotations != null ) {
throw new MappingException(
"@EmbeddedId and @Id cannot be used together. Check the configuration for " + configuredClass.getName() + "."
);
}
if ( embeddedIdAnnotations != null ) {
if ( embeddedIdAnnotations.size() == 1 ) {
return IdType.EMBEDDED;
}
else {
throw new MappingException( "Multiple @EmbeddedId annotations are not allowed" );
}
}
if ( idAnnotations != null ) {
if ( idAnnotations.size() == 1 ) {
return IdType.SIMPLE;
}
else {
return IdType.COMPOSED;
}
}
return IdType.NONE;
}
enum IdType {
// single @Id annotation
SIMPLE,
// multiple @Id annotations
COMPOSED,
// @EmbeddedId annotation
EMBEDDED,
// does not contain any identifier mappings
NONE
}
}

View File

@ -34,7 +34,6 @@ import org.hibernate.annotations.Cache;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.Check;
import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.ColumnTransformer;
import org.hibernate.annotations.ColumnTransformers;
import org.hibernate.annotations.Columns;
@ -50,7 +49,6 @@ import org.hibernate.annotations.FilterDefs;
import org.hibernate.annotations.FilterJoinTable;
import org.hibernate.annotations.FilterJoinTables;
import org.hibernate.annotations.Filters;
import org.hibernate.annotations.ForceDiscriminator;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.Formula;
import org.hibernate.annotations.Generated;
@ -66,8 +64,6 @@ import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.Loader;
import org.hibernate.annotations.ManyToAny;
import org.hibernate.annotations.MapKey;
import org.hibernate.annotations.MapKeyManyToMany;
import org.hibernate.annotations.MapKeyType;
import org.hibernate.annotations.MetaValue;
import org.hibernate.annotations.NamedNativeQueries;
@ -119,7 +115,6 @@ public interface HibernateDotNames {
public static final DotName CASCADE = DotName.createSimple( Cascade.class.getName() );
public static final DotName CHECK = DotName.createSimple( Check.class.getName() );
public static final DotName COLLECTION_ID = DotName.createSimple( CollectionId.class.getName() );
public static final DotName COLLECTION_OF_ELEMENTS = DotName.createSimple( CollectionOfElements.class.getName() );
public static final DotName COLUMNS = DotName.createSimple( Columns.class.getName() );
public static final DotName COLUMN_TRANSFORMER = DotName.createSimple( ColumnTransformer.class.getName() );
public static final DotName COLUMN_TRANSFORMERS = DotName.createSimple( ColumnTransformers.class.getName() );
@ -135,7 +130,6 @@ public interface HibernateDotNames {
public static final DotName FILTER_JOIN_TABLE = DotName.createSimple( FilterJoinTable.class.getName() );
public static final DotName FILTER_JOIN_TABLES = DotName.createSimple( FilterJoinTables.class.getName() );
public static final DotName FILTERS = DotName.createSimple( Filters.class.getName() );
public static final DotName FORCE_DISCRIMINATOR = DotName.createSimple( ForceDiscriminator.class.getName() );
public static final DotName FOREIGN_KEY = DotName.createSimple( ForeignKey.class.getName() );
public static final DotName FORMULA = DotName.createSimple( Formula.class.getName() );
public static final DotName GENERATED = DotName.createSimple( Generated.class.getName() );
@ -151,8 +145,6 @@ public interface HibernateDotNames {
public static final DotName LAZY_TO_ONE = DotName.createSimple( LazyToOne.class.getName() );
public static final DotName LOADER = DotName.createSimple( Loader.class.getName() );
public static final DotName MANY_TO_ANY = DotName.createSimple( ManyToAny.class.getName() );
public static final DotName MAP_KEY = DotName.createSimple( MapKey.class.getName() );
public static final DotName MAP_KEY_MANY_TO_MANY = DotName.createSimple( MapKeyManyToMany.class.getName() );
public static final DotName MAP_KEY_TYPE = DotName.createSimple( MapKeyType.class.getName() );
public static final DotName META_VALUE = DotName.createSimple( MetaValue.class.getName() );
public static final DotName NAMED_NATIVE_QUERIES = DotName.createSimple( NamedNativeQueries.class.getName() );

View File

@ -0,0 +1,40 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.source.annotations;
/**
* An emum for the type of id configuration for an entity.
*
* @author Hardy Ferentschik
*/
public enum IdType {
// single @Id annotation
SIMPLE,
// multiple @Id annotations
COMPOSED,
// @EmbeddedId annotation
EMBEDDED,
// does not contain any identifier mappings
NONE
}

View File

@ -25,14 +25,19 @@ package org.hibernate.metamodel.source.annotations;
import java.util.List;
import java.util.Map;
import javax.persistence.DiscriminatorType;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.DotName;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
/**
* Represent a mapped attribute (explicitly or implicitly mapped).
* Represent a mapped attribute (explicitly or implicitly mapped). Also used for synthetic attributes likes a
* discriminator column.
*
* @author Hardy Ferentschik
*/
@ -43,24 +48,78 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
private final ColumnValues columnValues;
private final boolean isId;
private final boolean isVersioned;
private final boolean isDiscriminator;
MappedAttribute(String name, Class<?> type, Map<DotName, List<AnnotationInstance>> annotations) {
static MappedAttribute createMappedAttribute(String name, Class<?> type, Map<DotName, List<AnnotationInstance>> annotations) {
return new MappedAttribute( name, type, annotations, false );
}
static MappedAttribute createDiscriminatorAttribute(Map<DotName, List<AnnotationInstance>> annotations) {
Map<DotName, List<AnnotationInstance>> discriminatorAnnotations = JandexHelper.filterAnnotations(
annotations,
JPADotNames.DISCRIMINATOR_COLUMN,
JPADotNames.DISCRIMINATOR_VALUE,
HibernateDotNames.DISCRIMINATOR_FORMULA,
HibernateDotNames.DISCRIMINATOR_OPTIONS
);
AnnotationInstance discriminatorOptionsAnnotation = JandexHelper.getSingleAnnotation(
annotations, JPADotNames.DISCRIMINATOR_COLUMN
);
String name = DiscriminatorColumnValues.DEFAULT_DISCRIMINATOR_COLUMN_NAME;
Class<?> type = String.class; // string is the discriminator default
if ( discriminatorOptionsAnnotation != null ) {
name = discriminatorOptionsAnnotation.value( "name" ).asString();
DiscriminatorType discriminatorType = Enum.valueOf(
DiscriminatorType.class, discriminatorOptionsAnnotation.value( "discriminatorType" ).asEnum()
);
switch ( discriminatorType ) {
case STRING: {
type = String.class;
break;
}
case CHAR: {
type = Character.class;
break;
}
case INTEGER: {
type = Integer.class;
break;
}
default: {
throw new AnnotationException( "Unsupported discriminator type: " + discriminatorType );
}
}
}
return new MappedAttribute( name, type, discriminatorAnnotations, true );
}
private MappedAttribute(String name, Class<?> type, Map<DotName, List<AnnotationInstance>> annotations, boolean isDiscriminator) {
this.name = name;
this.type = type;
this.annotations = annotations;
this.isDiscriminator = isDiscriminator;
List<AnnotationInstance> idAnnotations = annotations.get( JPADotNames.ID );
isId = idAnnotations != null && !idAnnotations.isEmpty();
AnnotationInstance idAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.ID );
isId = idAnnotation != null;
List<AnnotationInstance> versionAnnotations = annotations.get( JPADotNames.VERSION );
isVersioned = versionAnnotations != null && !versionAnnotations.isEmpty();
AnnotationInstance versionAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.VERSION );
isVersioned = versionAnnotation != null;
List<AnnotationInstance> columnAnnotations = annotations.get( JPADotNames.COLUMN );
if ( columnAnnotations != null && columnAnnotations.size() > 1 ) {
throw new AssertionFailure( "There can only be one @Column annotation per mapped attribute" );
if ( isDiscriminator ) {
columnValues = new DiscriminatorColumnValues( annotations );
}
else {
AnnotationInstance columnAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.COLUMN );
columnValues = new ColumnValues( columnAnnotation );
}
if ( isId ) {
// an id must be unique and cannot be nullable
columnValues.setUnique( true );
columnValues.setNullable( false );
}
AnnotationInstance columnAnnotation = columnAnnotations == null ? null : columnAnnotations.get( 0 );
columnValues = new ColumnValues( columnAnnotation, isId );
}
public final String getName() {
@ -83,6 +142,10 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
return isVersioned;
}
public boolean isDiscriminator() {
return isDiscriminator;
}
/**
* Returns the annotation with the specified name or {@code null}
*

View File

@ -93,11 +93,24 @@ public class JandexHelper {
*
* @return the single annotation defined on the class or {@code null} in case the annotation is not specified at all
*
* @throws org.hibernate.AssertionFailure in case there is
* @throws org.hibernate.AssertionFailure in case there is there is more than one annotation of this type.
*/
public static AnnotationInstance getSingleAnnotation(ClassInfo classInfo, DotName annotationName)
throws AssertionFailure {
List<AnnotationInstance> annotationList = classInfo.annotations().get( annotationName );
return getSingleAnnotation( classInfo.annotations(), annotationName );
}
/**
* @param annotations List of annotation instances keyed against their dot name.
* @param annotationName the annotation to retrieve from map
*
* @return the single annotation of the specified dot name or {@code null} in case the annotation is not specified at all
*
* @throws org.hibernate.AssertionFailure in case there is there is more than one annotation of this type.
*/
public static AnnotationInstance getSingleAnnotation(Map<DotName, List<AnnotationInstance>> annotations, DotName annotationName)
throws AssertionFailure {
List<AnnotationInstance> annotationList = annotations.get( annotationName );
if ( annotationList == null ) {
return null;
}
@ -113,6 +126,20 @@ public class JandexHelper {
}
}
/**
* @param annotations List of annotation instances keyed against their dot name.
* @param annotationNames the annotation names to filter
*
* @return a new map of annotation instances only containing annotations of the specified dot names.
*/
public static Map<DotName, List<AnnotationInstance>> filterAnnotations(Map<DotName, List<AnnotationInstance>> annotations, DotName... annotationNames) {
Map<DotName, List<AnnotationInstance>> filteredAnnotations = new HashMap<DotName, List<AnnotationInstance>>();
for ( DotName name : annotationNames ) {
filteredAnnotations.put( name, annotations.get( name ) );
}
return filteredAnnotations;
}
/**
* Creates a jandex index for the specified classes
*

View File

@ -480,7 +480,6 @@ PrimitiveArray
hibernateMappingBinder.getHibernateXmlBinder().getMetadata(),
hibernateMappingBinder,
entityBinding.getEntity().getOrCreateSingularAttribute( attributeName ),
entityBinding.getMetaAttributes(),
discriminator
)
);

View File

@ -83,7 +83,6 @@ public class HbmSimpleAttributeDomainState extends AbstractHbmAttributeDomainSta
MetadataImpl metadata,
MappingDefaults defaults,
org.hibernate.metamodel.domain.Attribute attribute,
Map<String, MetaAttribute> entityMetaAttributes,
XMLDiscriminator discriminator) {
super(
metadata, defaults, attribute, null, null, null, true

View File

@ -0,0 +1,79 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.source.annotations;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.junit.Test;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
/**
* @author Hardy Ferentschik
*/
public class InheritanceTypeTest extends BaseUnitTestCase {
@Test
public void testNoInheritance() {
MetadataImpl meta = buildMetadata( SingleEntity.class );
EntityBinding entityBinding = getEntityBindingForInnerClass( meta, SingleEntity.class );
assertNull( entityBinding.getEntityDiscriminator() );
}
@Test
public void testDiscriminatorValue() {
MetadataImpl meta = buildMetadata( RootOfSingleTableInheritance.class, SubclassOfSingleTableInheritance.class );
EntityBinding entityBinding = meta.getEntityBinding( SubclassOfSingleTableInheritance.class.getSimpleName() );
assertEquals( "Wrong discriminator value", "foo", entityBinding.getDiscriminatorValue() );
}
private MetadataImpl buildMetadata(Class<?>... classes) {
MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
for ( Class clazz : classes ) {
sources.addAnnotatedClass( clazz );
}
return (MetadataImpl) sources.buildMetadata();
}
private EntityBinding getEntityBindingForInnerClass(MetadataImpl meta, Class<?> clazz) {
return meta.getEntityBinding( this.getClass().getSimpleName() + "$" + clazz.getSimpleName() );
}
@Entity
class SingleEntity {
@Id
@GeneratedValue
private int id;
}
}

View File

@ -0,0 +1,41 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.source.annotations;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author Hardy Ferentschik
*/
@Entity
class RootOfSingleTableInheritance {
@Id
@GeneratedValue
private int id;
}

View File

@ -0,0 +1,37 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.source.annotations;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
/**
* @author Hardy Ferentschik
*/
@Entity
@DiscriminatorValue( "foo" )
public class SubclassOfSingleTableInheritance extends RootOfSingleTableInheritance {
}

View File

@ -29,7 +29,6 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import org.jboss.jandex.ClassInfo;
@ -40,6 +39,7 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.metamodel.binding.InheritanceType;
import org.hibernate.metamodel.source.annotations.util.ConfiguredClassHierarchyBuilder;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.service.ServiceRegistryBuilder;
@ -119,7 +119,7 @@ public class TableNameTest extends BaseUnitTestCase {
@Test
public void testTablePerClassDefaultTableName() {
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Inheritance(strategy = javax.persistence.InheritanceType.TABLE_PER_CLASS)
class A {
@Id
@GeneratedValue
@ -166,7 +166,7 @@ public class TableNameTest extends BaseUnitTestCase {
@Test
public void testJoinedSubclassDefaultTableName() {
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Inheritance(strategy = javax.persistence.InheritanceType.JOINED)
@Table(name = "FOO")
class A {
@Id

View File

@ -31,7 +31,6 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.MappedSuperclass;
import org.jboss.jandex.ClassInfo;
@ -42,6 +41,7 @@ import org.junit.Before;
import org.junit.Test;
import org.hibernate.AnnotationException;
import org.hibernate.metamodel.binding.InheritanceType;
import org.hibernate.metamodel.source.annotations.ConfiguredClass;
import org.hibernate.metamodel.source.annotations.ConfiguredClassHierarchy;
import org.hibernate.service.ServiceRegistryBuilder;
@ -265,7 +265,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Inheritance(strategy = javax.persistence.InheritanceType.JOINED)
class A extends MappedSuperClass {
@Id
String id;
@ -281,20 +281,22 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
);
assertTrue( hierarchies.size() == 1 );
ConfiguredClassHierarchy hierarchy = hierarchies.iterator().next();
assertEquals( "Wrong inheritance type", InheritanceType.JOINED, hierarchy.getInheritanceType() );
assertEquals(
"Wrong inheritance type", InheritanceType.JOINED, hierarchy.getInheritanceType()
);
}
@Test(expected = AnnotationException.class)
public void testMultipleConflictingInheritanceDefinitions() {
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Inheritance(strategy = javax.persistence.InheritanceType.JOINED)
class A {
String id;
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Inheritance(strategy = javax.persistence.InheritanceType.TABLE_PER_CLASS)
class B extends A {
}