HHH-6171 Extracting state classes from EntityBinder and enabled creation of versioned attribute

This commit is contained in:
Hardy Ferentschik 2011-05-04 14:25:08 +02:00
parent 64fe46444b
commit e8bd7dfd66
10 changed files with 218 additions and 151 deletions

View File

@ -23,9 +23,7 @@
*/
package org.hibernate.metamodel.source.annotations;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
@ -33,16 +31,15 @@ import org.jboss.jandex.AnnotationValue;
import org.hibernate.AssertionFailure;
import org.hibernate.MappingException;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.binding.AttributeBinding;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.binding.HibernateTypeDescriptor;
import org.hibernate.metamodel.binding.SimpleAttributeBinding;
import org.hibernate.metamodel.domain.Attribute;
import org.hibernate.metamodel.domain.Entity;
import org.hibernate.metamodel.domain.Hierarchical;
import org.hibernate.metamodel.relational.Identifier;
import org.hibernate.metamodel.relational.Schema;
import org.hibernate.metamodel.source.annotations.state.domain.AttributeDomainState;
import org.hibernate.metamodel.source.annotations.state.relational.AttributeColumnRelationalState;
import org.hibernate.metamodel.source.annotations.state.relational.AttributeTupleRelationalState;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.internal.MetadataImpl;
@ -161,16 +158,12 @@ public class EntityBinder {
MappedAttribute idAttribute = configuredClass.getMappedProperty( idName );
AnnotationSimpleAttributeDomainState domainState = new AnnotationSimpleAttributeDomainState();
HibernateTypeDescriptor typeDescriptor = new HibernateTypeDescriptor();
typeDescriptor.setTypeName( idAttribute.getType().getName() );
domainState.typeDescriptor = typeDescriptor;
domainState.attribute = entityBinding.getEntity().getOrCreateSingularAttribute( idAttribute.getName() );
AttributeDomainState domainState = new AttributeDomainState( entityBinding, idAttribute );
idBinding.initialize( domainState );
AttributeColumnRelationalState columnRelationsState = new AttributeColumnRelationalState( idAttribute, meta );
AnnotationSimpleAttributeRelationalState relationalState = new AnnotationSimpleAttributeRelationalState();
relationalState.valueStates.add( columnRelationsState );
AttributeTupleRelationalState relationalState = new AttributeTupleRelationalState();
relationalState.addValueState( columnRelationsState );
idBinding.initializeSimpleTupleValue( relationalState );
}
@ -182,21 +175,24 @@ public class EntityBinder {
String attributeName = mappedAttribute.getName();
entityBinding.getEntity().getOrCreateSingularAttribute( attributeName );
SimpleAttributeBinding simpleBinding = entityBinding.makeSimpleAttributeBinding( attributeName );
SimpleAttributeBinding attributeBinding;
AnnotationSimpleAttributeDomainState domainState = new AnnotationSimpleAttributeDomainState();
HibernateTypeDescriptor typeDescriptor = new HibernateTypeDescriptor();
typeDescriptor.setTypeName( mappedAttribute.getType().getName() );
domainState.typeDescriptor = typeDescriptor;
domainState.attribute = entityBinding.getEntity().getOrCreateSingularAttribute( attributeName );
simpleBinding.initialize( domainState );
if ( mappedAttribute.isVersioned() ) {
attributeBinding = entityBinding.makeVersionBinding( attributeName );
}
else {
attributeBinding = entityBinding.makeSimpleAttributeBinding( attributeName );
}
AttributeDomainState domainState = new AttributeDomainState( entityBinding, mappedAttribute );
attributeBinding.initialize( domainState );
AttributeColumnRelationalState columnRelationsState = new AttributeColumnRelationalState(
mappedAttribute, meta
);
AnnotationSimpleAttributeRelationalState relationalState = new AnnotationSimpleAttributeRelationalState();
relationalState.valueStates.add( columnRelationsState );
simpleBinding.initializeSimpleTupleValue( relationalState );
AttributeTupleRelationalState relationalState = new AttributeTupleRelationalState();
relationalState.addValueState( columnRelationsState );
attributeBinding.initializeSimpleTupleValue( relationalState );
}
}
@ -282,100 +278,6 @@ public class EntityBinder {
// does not contain any identifier mappings
NONE
}
public static class AnnotationSimpleAttributeDomainState implements SimpleAttributeBinding.DomainState {
PropertyGeneration propertyGeneration;
HibernateTypeDescriptor typeDescriptor;
Attribute attribute;
@Override
public PropertyGeneration getPropertyGeneration() {
// GeneratedValue generatedValue = property.getAnnotation( GeneratedValue.class );
// String generatorType = generatedValue != null ?
// generatorType( generatedValue.strategy(), mappings ) :
// "assigned";
// String generatorName = generatedValue != null ?
// generatedValue.generator() :
// BinderHelper.ANNOTATION_STRING_DEFAULT;
return propertyGeneration;
}
@Override
public boolean isInsertable() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean isUpdateable() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean isKeyCasadeDeleteEnabled() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getUnsavedValue() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public HibernateTypeDescriptor getHibernateTypeDescriptor() {
return typeDescriptor;
}
@Override
public Attribute getAttribute() {
return attribute;
}
@Override
public boolean isLazy() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getPropertyAccessorName() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean isAlternateUniqueKey() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getCascade() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean isOptimisticLockable() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getNodeName() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public Map<String, org.hibernate.metamodel.domain.MetaAttribute> getMetaAttributes(EntityBinding entityBinding) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}
public static class AnnotationSimpleAttributeRelationalState
implements SimpleAttributeBinding.SimpleTupleRelationalState {
List<AttributeBinding.SingleValueRelationalState> valueStates = new ArrayList<AttributeBinding.SingleValueRelationalState>();
@Override
public List<AttributeBinding.SingleValueRelationalState> getRelationalStates() {
return valueStates;
}
}
}

View File

@ -43,6 +43,7 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
private final Map<DotName, List<AnnotationInstance>> annotations;
private final ColumnValues columnValues;
private final boolean isId;
private final boolean isVersioned;
MappedAttribute(String name, Class<?> type, Map<DotName, List<AnnotationInstance>> annotations) {
this.name = name;
@ -52,6 +53,9 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
List<AnnotationInstance> idAnnotations = annotations.get( JPADotNames.ID );
isId = idAnnotations != null && !idAnnotations.isEmpty();
List<AnnotationInstance> versionAnnotations = annotations.get( JPADotNames.VERSION );
isVersioned = versionAnnotations != null && !versionAnnotations.isEmpty();
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" );
@ -76,6 +80,10 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
return isId;
}
public boolean isVersioned() {
return isVersioned;
}
public final List<AnnotationInstance> annotations(DotName annotationDotName) {
if ( annotations.containsKey( annotationDotName ) ) {
return annotations.get( annotationDotName );

View File

@ -0,0 +1,109 @@
package org.hibernate.metamodel.source.annotations.state.domain;
import java.util.Map;
import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.binding.HibernateTypeDescriptor;
import org.hibernate.metamodel.binding.SimpleAttributeBinding;
import org.hibernate.metamodel.domain.Attribute;
import org.hibernate.metamodel.domain.Entity;
import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.source.annotations.MappedAttribute;
/**
* @author Hardy Ferentschik
*/
public class AttributeDomainState implements SimpleAttributeBinding.DomainState {
private final PropertyGeneration propertyGeneration = null;
private final HibernateTypeDescriptor typeDescriptor;
private final Attribute attribute;
public AttributeDomainState(EntityBinding entityBinding, MappedAttribute mappedAttribute) {
typeDescriptor = new HibernateTypeDescriptor();
typeDescriptor.setTypeName( mappedAttribute.getType().getName() );
Entity entity = entityBinding.getEntity();
attribute = entity.getOrCreateSingularAttribute( mappedAttribute.getName() );
}
@Override
public PropertyGeneration getPropertyGeneration() {
// GeneratedValue generatedValue = property.getAnnotation( GeneratedValue.class );
// String generatorType = generatedValue != null ?
// generatorType( generatedValue.strategy(), mappings ) :
// "assigned";
// String generatorName = generatedValue != null ?
// generatedValue.generator() :
// BinderHelper.ANNOTATION_STRING_DEFAULT;
return propertyGeneration;
}
@Override
public boolean isInsertable() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean isUpdateable() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean isKeyCasadeDeleteEnabled() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getUnsavedValue() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public HibernateTypeDescriptor getHibernateTypeDescriptor() {
return typeDescriptor;
}
@Override
public Attribute getAttribute() {
return attribute;
}
@Override
public boolean isLazy() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getPropertyAccessorName() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean isAlternateUniqueKey() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getCascade() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean isOptimisticLockable() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getNodeName() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public Map<String, MetaAttribute> getMetaAttributes(EntityBinding entityBinding) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.source.annotations;
package org.hibernate.metamodel.source.annotations.state.relational;
import java.util.ArrayList;
import java.util.Arrays;
@ -36,6 +36,9 @@ import org.hibernate.AssertionFailure;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.metamodel.binding.SimpleAttributeBinding;
import org.hibernate.metamodel.relational.Size;
import org.hibernate.metamodel.source.annotations.ColumnValues;
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.source.annotations.MappedAttribute;
import org.hibernate.metamodel.source.internal.MetadataImpl;
/**
@ -58,7 +61,7 @@ public class AttributeColumnRelationalState implements SimpleAttributeBinding.Co
private Set<String> uniqueKeys = new HashSet<String>();
private Set<String> indexes = new HashSet<String>();
AttributeColumnRelationalState(MappedAttribute attribute, MetadataImpl meta) {
public AttributeColumnRelationalState(MappedAttribute attribute, MetadataImpl meta) {
ColumnValues columnValues = attribute.getColumnValues();
namingStrategy = meta.getNamingStrategy();
columnName = columnValues.getName().isEmpty() ? attribute.getName() : columnValues.getName();

View File

@ -0,0 +1,48 @@
/*
* 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.state.relational;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.metamodel.binding.AttributeBinding;
import org.hibernate.metamodel.binding.SimpleAttributeBinding;
/**
* @author Hardy Ferentschik
*/
public class AttributeTupleRelationalState implements SimpleAttributeBinding.SimpleTupleRelationalState {
List<AttributeBinding.SingleValueRelationalState> valueStates = new ArrayList<AttributeBinding.SingleValueRelationalState>();
public void addValueState(AttributeBinding.SingleValueRelationalState state) {
valueStates.add( state );
}
@Override
public List<AttributeBinding.SingleValueRelationalState> getRelationalStates() {
return valueStates;
}
}

View File

@ -37,13 +37,12 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLCacheElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLCompositeId;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLId;
import org.hibernate.metamodel.source.util.MappingHelper;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
* TODO : javadoc
*
* @author Steve Ebersole
*/
class RootEntityBinder extends AbstractEntityBinder {
RootEntityBinder(HibernateMappingBinder hibernateMappingBinder, XMLClass xmlClazz) {
@ -67,7 +66,7 @@ class RootEntityBinder extends AbstractEntityBinder {
}
if ( xmlClazz.getPolymorphism() != null ) {
entityBinding.setExplicitPolymorphism( "explicit".equals( xmlClazz.getPolymorphism() ) );
entityBinding.setExplicitPolymorphism( "explicit".equals( xmlClazz.getPolymorphism() ) );
}
if ( xmlClazz.getRowid() != null ) {
@ -141,7 +140,7 @@ class RootEntityBinder extends AbstractEntityBinder {
SimpleAttributeBinding idBinding = entityBinding.makeSimplePrimaryKeyAttributeBinding( attributeName );
bindSimpleAttribute( id, idBinding, entityBinding, attributeName );
if ( ! Column.class.isInstance( idBinding.getValue() ) ) {
if ( !Column.class.isInstance( idBinding.getValue() ) ) {
// this should never ever happen..
throw new MappingException( "Unanticipated situation" );
}
@ -237,9 +236,14 @@ class RootEntityBinder extends AbstractEntityBinder {
SimpleAttributeBinding discriminatorBinding = entityBinding.makeEntityDiscriminatorBinding( RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME );
// Handle the relational portion of the binding...
bindSimpleAttribute( xmlEntityClazz.getDiscriminator(), discriminatorBinding, entityBinding, RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME );
bindSimpleAttribute(
xmlEntityClazz.getDiscriminator(),
discriminatorBinding,
entityBinding,
RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME
);
entityBinding.getEntityDiscriminator().setForced(xmlEntityClazz.getDiscriminator().isForce());
entityBinding.getEntityDiscriminator().setForced( xmlEntityClazz.getDiscriminator().isForce() );
}
private void bindVersion(XMLClass xmlEntityClazz,
@ -249,9 +253,12 @@ class RootEntityBinder extends AbstractEntityBinder {
}
boolean isVersion = xmlEntityClazz.getVersion() != null;
String explicitName = isVersion ? xmlEntityClazz.getVersion().getName() : xmlEntityClazz.getTimestamp().getName();
String explicitName = isVersion ? xmlEntityClazz.getVersion().getName() : xmlEntityClazz.getTimestamp()
.getName();
if ( explicitName == null ) {
throw new MappingException( "Mising property name for version/timestamp mapping [" + entityBinding.getEntity().getName() + "]" );
throw new MappingException(
"Missing property name for version/timestamp mapping [" + entityBinding.getEntity().getName() + "]"
);
}
SimpleAttributeBinding versionBinding = entityBinding.makeVersionBinding( explicitName );
if ( isVersion ) {

View File

@ -67,10 +67,10 @@ public abstract class AbstractBasicBindingTests extends BaseUnitTestCase {
@Test
public void testSimpleEntityMapping() {
checkSimpleEntityMaping( buildSimpleEntityBinding() );
checkSimpleEntityMapping( buildSimpleEntityBinding() );
}
protected void checkSimpleEntityMaping(EntityBinding entityBinding) {
protected void checkSimpleEntityMapping(EntityBinding entityBinding) {
assertNotNull( entityBinding );
assertNotNull( entityBinding.getEntityIdentifier() );
assertNotNull( entityBinding.getEntityIdentifier().getValueBinding() );
@ -116,7 +116,7 @@ public abstract class AbstractBasicBindingTests extends BaseUnitTestCase {
MetadataImplementor metadata = buildMetadataWithManyToOne();
EntityBinding entityWithManyToOneBinding = metadata.getEntityBinding( EntityWithManyToOne.class.getName() );
EntityBinding simpleEntityBinding = metadata.getEntityBinding( SimpleEntity.class.getName() );
checkSimpleEntityMaping( simpleEntityBinding );
checkSimpleEntityMapping( simpleEntityBinding );
assertTrue(
1 == simpleEntityBinding.getAttributeBinding( "id" ).getEntityReferencingAttributeBindings().size()

View File

@ -37,19 +37,6 @@ import org.hibernate.testing.FailureExpected;
* @author Hardy Ferentschik
*/
public class BasicAnnotationBindingTests extends AbstractBasicBindingTests {
//@FailureExpected(jiraKey = "HHH-5672", message = "Work in progress")
@Test
public void testSimpleEntityMapping() {
super.testSimpleEntityMapping();
}
@FailureExpected(jiraKey = "HHH-5672", message = "Work in progress")
@Test
public void testSimpleVersionedEntityMapping() {
super.testSimpleVersionedEntityMapping();
}
@FailureExpected(jiraKey = "HHH-6172", message = "Work in progress")
@Test
public void testEntityWithManyToOneMapping() {

View File

@ -25,14 +25,11 @@ package org.hibernate.metamodel.binding;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @author Steve Ebersole
*/
@Entity
@Table(schema = "foo")
public class SimpleEntity {
@Id
private Long id;

View File

@ -23,11 +23,15 @@
*/
package org.hibernate.metamodel.binding;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;
/**
* TODO : javadoc
*
* @author Steve Ebersole
* @author Hardy Ferentschik
*/
@Entity
public class SimpleVersionedEntity {
private Long id;
private String name;
@ -40,6 +44,7 @@ public class SimpleVersionedEntity {
this.name = name;
}
@Id
public Long getId() {
return id;
}
@ -56,6 +61,7 @@ public class SimpleVersionedEntity {
this.name = name;
}
@Version
public long getVersion() {
return version;
}