HHH-6161 Working w/ domain and relational state objects

This commit is contained in:
Hardy Ferentschik 2011-04-28 18:09:34 +02:00
parent 3ec1928653
commit 000b647e43
10 changed files with 165 additions and 44 deletions

View File

@ -77,7 +77,7 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
} }
public void initialize(DomainState state) { public void initialize(DomainState state) {
hibernateTypeDescriptor.intialize( state.getHibernateTypeDescriptor() ); hibernateTypeDescriptor.initialize( state.getHibernateTypeDescriptor() );
attribute = state.getAttribute(); attribute = state.getAttribute();
isLazy = state.isLazy(); isLazy = state.isLazy();
propertyAccessorName = state.getPropertyAccessorName(); propertyAccessorName = state.getPropertyAccessorName();

View File

@ -23,11 +23,7 @@
*/ */
package org.hibernate.metamodel.binding; package org.hibernate.metamodel.binding;
import org.dom4j.Element;
import org.hibernate.metamodel.relational.Value; import org.hibernate.metamodel.relational.Value;
import org.hibernate.metamodel.source.hbm.HbmHelper;
import org.hibernate.metamodel.source.util.DomHelper;
/** /**
* TODO : javadoc * TODO : javadoc
@ -52,7 +48,7 @@ public class CollectionElement {
} }
public void initialize(DomainState state) { public void initialize(DomainState state) {
hibernateTypeDescriptor.intialize( state.getHibernateTypeDescriptor() ); hibernateTypeDescriptor.initialize( state.getHibernateTypeDescriptor() );
nodeName = state.getNodeName(); nodeName = state.getNodeName();
} }
} }

View File

@ -53,7 +53,7 @@ public class HibernateTypeDescriptor {
this.explicitType = explicitType; this.explicitType = explicitType;
} }
public void intialize(HibernateTypeDescriptor hibernateTypeDescriptor) { public void initialize(HibernateTypeDescriptor hibernateTypeDescriptor) {
typeName = hibernateTypeDescriptor.typeName; typeName = hibernateTypeDescriptor.typeName;
explicitType = hibernateTypeDescriptor.explicitType; explicitType = hibernateTypeDescriptor.explicitType;
typeParameters = hibernateTypeDescriptor.typeParameters; typeParameters = hibernateTypeDescriptor.typeParameters;

View File

@ -23,7 +23,6 @@
*/ */
package org.hibernate.metamodel.binding; package org.hibernate.metamodel.binding;
import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import org.hibernate.MappingException; import org.hibernate.MappingException;
@ -35,7 +34,6 @@ import org.hibernate.metamodel.relational.SimpleValue;
import org.hibernate.metamodel.relational.Size; import org.hibernate.metamodel.relational.Size;
import org.hibernate.metamodel.relational.TableSpecification; import org.hibernate.metamodel.relational.TableSpecification;
import org.hibernate.metamodel.relational.Tuple; import org.hibernate.metamodel.relational.Tuple;
import org.hibernate.metamodel.relational.Value;
/** /**
* TODO : javadoc * TODO : javadoc
@ -47,20 +45,34 @@ public class SimpleAttributeBinding extends SingularAttributeBinding {
public PropertyGeneration getPropertyGeneration(); public PropertyGeneration getPropertyGeneration();
} }
public static interface SingleValueRelationalState {} public static interface SingleValueRelationalState {
}
public static interface ColumnRelationalState extends SingleValueRelationalState { public static interface ColumnRelationalState extends SingleValueRelationalState {
NamingStrategy getNamingStrategy(); NamingStrategy getNamingStrategy();
String getExplicitColumnName(); String getExplicitColumnName();
boolean isUnique(); boolean isUnique();
Size getSize(); Size getSize();
boolean isNullable(); boolean isNullable();
String getCheckCondition(); String getCheckCondition();
String getDefault(); String getDefault();
String getSqlType(); String getSqlType();
String getCustomWriteFragment(); String getCustomWriteFragment();
String getCustomReadFragment(); String getCustomReadFragment();
String getComment(); String getComment();
Set<String> getUniqueKeys(); Set<String> getUniqueKeys();
Set<String> getIndexes(); Set<String> getIndexes();
} }
@ -90,7 +102,8 @@ public class SimpleAttributeBinding extends SingularAttributeBinding {
private Column createColumn(ColumnRelationalState state) { private Column createColumn(ColumnRelationalState state) {
final String explicitName = state.getExplicitColumnName(); final String explicitName = state.getExplicitColumnName();
final String logicalColumnName = state.getNamingStrategy().logicalColumnName( explicitName, getAttribute().getName() ); final String logicalColumnName = state.getNamingStrategy()
.logicalColumnName( explicitName, getAttribute().getName() );
final TableSpecification table = getEntityBinding().getBaseTable(); final TableSpecification table = getEntityBinding().getBaseTable();
final String columnName = final String columnName =
explicitName == null ? explicitName == null ?
@ -100,8 +113,8 @@ public class SimpleAttributeBinding extends SingularAttributeBinding {
// mappings.addColumnBinding( logicalColumnName, column, table ); // mappings.addColumnBinding( logicalColumnName, column, table );
Column columnValue = table.createColumn( columnName ); Column columnValue = table.createColumn( columnName );
columnValue.getSize().initialize( state.getSize() ); columnValue.getSize().initialize( state.getSize() );
columnValue.setNullable( ! forceNonNullable() && state.isNullable() ); columnValue.setNullable( !forceNonNullable() && state.isNullable() );
columnValue.setUnique( ! forceUnique() && state.isUnique() ); columnValue.setUnique( !forceUnique() && state.isUnique() );
columnValue.setCheckCondition( state.getCheckCondition() ); columnValue.setCheckCondition( state.getCheckCondition() );
columnValue.setDefaultValue( state.getDefault() ); columnValue.setDefaultValue( state.getDefault() );
columnValue.setSqlType( state.getSqlType() ); columnValue.setSqlType( state.getSqlType() );
@ -138,7 +151,7 @@ public class SimpleAttributeBinding extends SingularAttributeBinding {
setValue( createSingleValue( state.getSingleValueRelationalStates().iterator().next() ) ); setValue( createSingleValue( state.getSingleValueRelationalStates().iterator().next() ) );
} }
else { else {
Tuple tuple = getEntityBinding().getBaseTable().createTuple( "[" + getAttribute().getName() + "]" ); Tuple tuple = getEntityBinding().getBaseTable().createTuple( "[" + getAttribute().getName() + "]" );
for ( SingleValueRelationalState singleValueState : state.getSingleValueRelationalStates() ) { for ( SingleValueRelationalState singleValueState : state.getSingleValueRelationalStates() ) {
tuple.addValue( createSingleValue( singleValueState ) ); tuple.addValue( createSingleValue( singleValueState ) );
} }

View File

@ -30,7 +30,9 @@ import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.persistence.AccessType; import javax.persistence.AccessType;
@ -64,7 +66,7 @@ public class ConfiguredClass {
private final AccessType classAccessType; private final AccessType classAccessType;
private final AccessType hierarchyAccessType; private final AccessType hierarchyAccessType;
private final boolean isMappedSuperClass; private final boolean isMappedSuperClass;
private final List<MappedProperty> mappedProperties; private final Map<String, MappedProperty> mappedProperties;
public ConfiguredClass(ClassInfo info, ConfiguredClass parent, AccessType hierarchyAccessType, ServiceRegistry serviceRegistry, ResolvedTypeWithMembers resolvedType) { public ConfiguredClass(ClassInfo info, ConfiguredClass parent, AccessType hierarchyAccessType, ServiceRegistry serviceRegistry, ResolvedTypeWithMembers resolvedType) {
this.classInfo = info; this.classInfo = info;
@ -78,10 +80,14 @@ public class ConfiguredClass {
isMappedSuperClass = mappedSuperClassAnnotation != null; isMappedSuperClass = mappedSuperClassAnnotation != null;
classAccessType = determineClassAccessType(); classAccessType = determineClassAccessType();
List<MappedProperty> tmpProperties = collectMappedProperties( resolvedType ); List<MappedProperty> properties = collectMappedProperties( resolvedType );
// make sure the properties are ordered by property name // make sure the properties are ordered by property name
Collections.sort( tmpProperties ); Collections.sort( properties );
mappedProperties = Collections.unmodifiableList( tmpProperties ); Map<String, MappedProperty> tmpMap = new LinkedHashMap<String, MappedProperty>();
for ( MappedProperty property : properties ) {
tmpMap.put( property.getName(), property );
}
mappedProperties = Collections.unmodifiableMap( tmpMap );
} }
public String getName() { public String getName() {
@ -104,8 +110,12 @@ public class ConfiguredClass {
return isMappedSuperClass; return isMappedSuperClass;
} }
public List<MappedProperty> getMappedProperties() { public Iterable<MappedProperty> getMappedProperties() {
return mappedProperties; return mappedProperties.values();
}
public MappedProperty getMappedProperty(String propertyName) {
return mappedProperties.get( propertyName );
} }
@Override @Override
@ -298,7 +308,7 @@ public class ConfiguredClass {
resolvedMembers = resolvedType.getMemberMethods(); resolvedMembers = resolvedType.getMemberMethods();
} }
Type type = findResolvedType( member.getName(), resolvedMembers ); Type type = findResolvedType( member.getName(), resolvedMembers );
return new MappedProperty( name, type ); return new MappedProperty( name, (Class) type );
} }
private Type findResolvedType(String name, ResolvedMember[] resolvedMembers) { private Type findResolvedType(String name, ResolvedMember[] resolvedMembers) {

View File

@ -26,11 +26,13 @@ package org.hibernate.metamodel.source.annotations;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationInstance;
import org.hibernate.AssertionFailure; import org.hibernate.AssertionFailure;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.mapping.PropertyGeneration; import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.binding.EntityBinding;
@ -39,6 +41,8 @@ import org.hibernate.metamodel.binding.SimpleAttributeBinding;
import org.hibernate.metamodel.domain.Attribute; import org.hibernate.metamodel.domain.Attribute;
import org.hibernate.metamodel.domain.Entity; import org.hibernate.metamodel.domain.Entity;
import org.hibernate.metamodel.domain.Hierarchical; import org.hibernate.metamodel.domain.Hierarchical;
import org.hibernate.metamodel.relational.Schema;
import org.hibernate.metamodel.relational.Size;
import org.hibernate.metamodel.source.annotations.util.JandexHelper; import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.internal.MetadataImpl; import org.hibernate.metamodel.source.internal.MetadataImpl;
@ -56,14 +60,19 @@ public class EntityBinder {
this.meta = metadata; this.meta = metadata;
EntityBinding entityBinding = new EntityBinding(); EntityBinding entityBinding = new EntityBinding();
bindJpaEntityAnnotation( entityBinding ); bindJpaEntityAnnotation( entityBinding );
// we also have to take care of an optional Hibernate specific @Id bindHibernateEntityAnnotation( entityBinding ); // optional hibernate specific @org.hibernate.annotations.Entity
bindHibernateEntityAnnotation( entityBinding ); bindTable( entityBinding );
if ( configuredClass.isRoot() ) { if ( configuredClass.isRoot() ) {
bindId( entityBinding ); bindId( entityBinding );
} }
meta.addEntity( entityBinding ); meta.addEntity( entityBinding );
} }
private void bindTable(EntityBinding entityBinding) {
final Schema schema = meta.getDatabase().getSchema( null );
}
private void bindId(EntityBinding entityBinding) { private void bindId(EntityBinding entityBinding) {
switch ( determineIdType() ) { switch ( determineIdType() ) {
case SIMPLE: { case SIMPLE: {
@ -106,12 +115,25 @@ public class EntityBinder {
entityBinding.getEntity().getOrCreateSingularAttribute( idName ); entityBinding.getEntity().getOrCreateSingularAttribute( idName );
SimpleAttributeBinding idBinding = entityBinding.makeSimplePrimaryKeyAttributeBinding( idName ); SimpleAttributeBinding idBinding = entityBinding.makeSimplePrimaryKeyAttributeBinding( idName );
MappedProperty idProperty = entity.getMappedProperty( idName );
AnnotationSimpleAttributeDomainState domainState = new AnnotationSimpleAttributeDomainState(); AnnotationSimpleAttributeDomainState domainState = new AnnotationSimpleAttributeDomainState();
//domainState.propertyGeneration = HibernateTypeDescriptor typeDescriptor = new HibernateTypeDescriptor();
typeDescriptor.setTypeName( idProperty.getType().getName() );
domainState.typeDescriptor = typeDescriptor;
domainState.attribute = entityBinding.getEntity().getOrCreateSingularAttribute( idProperty.getName() );
idBinding.initialize( domainState ); idBinding.initialize( domainState );
idBinding.initializeTupleValue( new AnnotationSimpleAttributeRelationalState() );
AnnotationColumnRelationalState columnRelationsState = new AnnotationColumnRelationalState();
columnRelationsState.namingStrategy = meta.getNamingStrategy();
columnRelationsState.columnName = idProperty.getColumnName();
columnRelationsState.unique = true;
columnRelationsState.nullable = false;
AnnotationSimpleAttributeRelationalState relationalState = new AnnotationSimpleAttributeRelationalState();
relationalState.valueStates.add( columnRelationsState );
idBinding.initializeTupleValue( relationalState );
} }
private void bindHibernateEntityAnnotation(EntityBinding entityBinding) { private void bindHibernateEntityAnnotation(EntityBinding entityBinding) {
@ -199,6 +221,8 @@ public class EntityBinder {
public static class AnnotationSimpleAttributeDomainState implements SimpleAttributeBinding.DomainState { public static class AnnotationSimpleAttributeDomainState implements SimpleAttributeBinding.DomainState {
PropertyGeneration propertyGeneration; PropertyGeneration propertyGeneration;
HibernateTypeDescriptor typeDescriptor;
Attribute attribute;
@Override @Override
public PropertyGeneration getPropertyGeneration() { public PropertyGeneration getPropertyGeneration() {
@ -235,12 +259,12 @@ public class EntityBinder {
@Override @Override
public HibernateTypeDescriptor getHibernateTypeDescriptor() { public HibernateTypeDescriptor getHibernateTypeDescriptor() {
return null; //To change body of implemented methods use File | Settings | File Templates. return typeDescriptor;
} }
@Override @Override
public Attribute getAttribute() { public Attribute getAttribute() {
return null; //To change body of implemented methods use File | Settings | File Templates. return attribute;
} }
@Override @Override
@ -281,9 +305,84 @@ public class EntityBinder {
public static class AnnotationSimpleAttributeRelationalState public static class AnnotationSimpleAttributeRelationalState
implements SimpleAttributeBinding.TupleRelationalState { implements SimpleAttributeBinding.TupleRelationalState {
LinkedHashSet<SimpleAttributeBinding.SingleValueRelationalState> valueStates = new LinkedHashSet<SimpleAttributeBinding.SingleValueRelationalState>();
@Override @Override
public LinkedHashSet<SimpleAttributeBinding.SingleValueRelationalState> getSingleValueRelationalStates() { public LinkedHashSet<SimpleAttributeBinding.SingleValueRelationalState> getSingleValueRelationalStates() {
return valueStates;
}
}
public static class AnnotationColumnRelationalState
implements SimpleAttributeBinding.ColumnRelationalState {
NamingStrategy namingStrategy;
String columnName;
boolean unique;
boolean nullable;
@Override
public NamingStrategy getNamingStrategy() {
return namingStrategy;
}
@Override
public String getExplicitColumnName() {
return columnName;
}
@Override
public boolean isUnique() {
return unique;
}
@Override
public Size getSize() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean isNullable() {
return nullable;
}
@Override
public String getCheckCondition() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getDefault() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getSqlType() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getCustomWriteFragment() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getCustomReadFragment() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getComment() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public Set<String> getUniqueKeys() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public Set<String> getIndexes() {
return null; //To change body of implemented methods use File | Settings | File Templates. return null; //To change body of implemented methods use File | Settings | File Templates.
} }
} }

View File

@ -25,6 +25,7 @@ package org.hibernate.metamodel.source.annotations;
import org.jboss.jandex.DotName; import org.jboss.jandex.DotName;
import org.hibernate.annotations.Check;
import org.hibernate.annotations.Entity; import org.hibernate.annotations.Entity;
import org.hibernate.annotations.FetchProfile; import org.hibernate.annotations.FetchProfile;
import org.hibernate.annotations.FetchProfiles; import org.hibernate.annotations.FetchProfiles;
@ -38,6 +39,8 @@ public interface HibernateDotNames {
public static final DotName ENTITY = DotName.createSimple( Entity.class.getName() ); public static final DotName ENTITY = DotName.createSimple( Entity.class.getName() );
public static final DotName FETCH_PROFILES = DotName.createSimple( FetchProfiles.class.getName() ); public static final DotName FETCH_PROFILES = DotName.createSimple( FetchProfiles.class.getName() );
public static final DotName FETCH_PROFILE = DotName.createSimple( FetchProfile.class.getName() ); public static final DotName FETCH_PROFILE = DotName.createSimple( FetchProfile.class.getName() );
public static final DotName CHECK = DotName.createSimple( Check.class.getName() );
} }

View File

@ -29,6 +29,7 @@ import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Inheritance; import javax.persistence.Inheritance;
import javax.persistence.MappedSuperclass; import javax.persistence.MappedSuperclass;
import javax.persistence.Table;
import javax.persistence.Transient; import javax.persistence.Transient;
import org.jboss.jandex.DotName; import org.jboss.jandex.DotName;
@ -47,6 +48,8 @@ public interface JPADotNames {
public static final DotName EMBEDDED_ID = DotName.createSimple( EmbeddedId.class.getName() ); public static final DotName EMBEDDED_ID = DotName.createSimple( EmbeddedId.class.getName() );
public static final DotName ACCESS = DotName.createSimple( Access.class.getName() ); public static final DotName ACCESS = DotName.createSimple( Access.class.getName() );
public static final DotName TRANSIENT = DotName.createSimple( Transient.class.getName() ); public static final DotName TRANSIENT = DotName.createSimple( Transient.class.getName() );
public static final DotName TABLE = DotName.createSimple( Table.class.getName() );
} }

View File

@ -23,8 +23,6 @@
*/ */
package org.hibernate.metamodel.source.annotations; package org.hibernate.metamodel.source.annotations;
import java.lang.reflect.Type;
/** /**
* Represent a mapped property (explicitly or implicitly mapped). * Represent a mapped property (explicitly or implicitly mapped).
* *
@ -32,9 +30,9 @@ import java.lang.reflect.Type;
*/ */
public class MappedProperty implements Comparable<MappedProperty> { public class MappedProperty implements Comparable<MappedProperty> {
private final String name; private final String name;
private final Type type; private final Class<?> type;
MappedProperty(String name, Type type) { MappedProperty(String name, Class<?> type) {
this.name = name; this.name = name;
this.type = type; this.type = type;
} }
@ -43,7 +41,11 @@ public class MappedProperty implements Comparable<MappedProperty> {
return name; return name;
} }
public Type getType() { public String getColumnName() {
return name;
}
public Class<?> getType() {
return type; return type;
} }

View File

@ -24,7 +24,6 @@
package org.hibernate.metamodel.source.annotations.util; package org.hibernate.metamodel.source.annotations.util;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Set; import java.util.Set;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
@ -81,36 +80,32 @@ public class GenericTypeDiscoveryTest extends BaseUnitTestCase {
ConfiguredClass configuredClass = iter.next(); ConfiguredClass configuredClass = iter.next();
ClassInfo info = configuredClass.getClassInfo(); ClassInfo info = configuredClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( Stuff.class.getName() ), info.name() ); assertEquals( "wrong class", DotName.createSimple( Stuff.class.getName() ), info.name() );
List<MappedProperty> mappedProperties = configuredClass.getMappedProperties(); MappedProperty property = configuredClass.getMappedProperty( "value" );
assertTrue( "Stuff should have one mapped property", mappedProperties.size() == 1 );
MappedProperty property = mappedProperties.get( 0 );
assertEquals( Price.class, property.getType() ); assertEquals( Price.class, property.getType() );
assertTrue( iter.hasNext() ); assertTrue( iter.hasNext() );
configuredClass = iter.next(); configuredClass = iter.next();
info = configuredClass.getClassInfo(); info = configuredClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( PricedStuff.class.getName() ), info.name() ); assertEquals( "wrong class", DotName.createSimple( PricedStuff.class.getName() ), info.name() );
mappedProperties = configuredClass.getMappedProperties(); assertFalse(
assertTrue( "PricedStuff should not mapped properties", mappedProperties.size() == 0 ); "PricedStuff should not mapped properties", configuredClass.getMappedProperties().iterator().hasNext()
);
assertTrue( iter.hasNext() ); assertTrue( iter.hasNext() );
configuredClass = iter.next(); configuredClass = iter.next();
info = configuredClass.getClassInfo(); info = configuredClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( Item.class.getName() ), info.name() ); assertEquals( "wrong class", DotName.createSimple( Item.class.getName() ), info.name() );
mappedProperties = configuredClass.getMappedProperties();
assertTrue( "Item should have 4 mapped properties", mappedProperties.size() == 4 );
// properties are alphabetically ordered! // properties are alphabetically ordered!
property = mappedProperties.get( 2 ); property = configuredClass.getMappedProperty( "owner" );
assertEquals( SomeGuy.class, property.getType() ); assertEquals( SomeGuy.class, property.getType() );
property = mappedProperties.get( 3 ); property = configuredClass.getMappedProperty( "type" );
assertEquals( PaperType.class, property.getType() ); assertEquals( PaperType.class, property.getType() );
assertTrue( iter.hasNext() ); assertTrue( iter.hasNext() );
configuredClass = iter.next(); configuredClass = iter.next();
info = configuredClass.getClassInfo(); info = configuredClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( Paper.class.getName() ), info.name() ); assertEquals( "wrong class", DotName.createSimple( Paper.class.getName() ), info.name() );
mappedProperties = configuredClass.getMappedProperties(); assertFalse( "Paper should not mapped properties", configuredClass.getMappedProperties().iterator().hasNext() );
assertTrue( "Paper should not mapped properties", mappedProperties.size() == 0 );
assertFalse( iter.hasNext() ); assertFalse( iter.hasNext() );
} }