This commit is contained in:
Strong Liu 2011-05-30 20:00:38 +08:00 committed by Hardy Ferentschik
parent 625d781af1
commit 3ff1b440c2
4 changed files with 82 additions and 5 deletions

View File

@ -23,6 +23,12 @@
*/
package org.hibernate.metamodel.binding;
import java.util.Properties;
import javax.persistence.GenerationType;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.internal.CoreMessageLogger;
import org.jboss.logging.Logger;
@ -38,6 +44,9 @@ public class EntityIdentifier {
private final EntityBinding entityBinding;
private AttributeBinding attributeBinding;
private IdentifierGenerator identifierGenerator;
private IdGenerator idGenerator;
private GenerationType generationType;
// todo : generator, mappers, etc
/**
@ -61,4 +70,23 @@ public class EntityIdentifier {
}
this.attributeBinding = attributeBinding;
}
public void setIdGenerator(IdGenerator idGenerator) {
this.idGenerator = idGenerator;
}
public void setGenerationType(GenerationType generationType) {
this.generationType = generationType;
}
public IdentifierGenerator createIdentifierGenerator(IdentifierGeneratorFactory factory) {
Properties props = new Properties();
props.putAll( idGenerator.getParameters() );
identifierGenerator = factory.createIdentifierGenerator( idGenerator.getStrategy(), null, props );
return identifierGenerator;
}
public IdentifierGenerator getIdentifierGenerator() {
return identifierGenerator;
}
}

View File

@ -57,6 +57,7 @@ import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.annotations.util.ReflectionHelper;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.metamodel.source.annotations.entity.AssociationType;
/**
* Represents an entity, mapped superclass or component configured via annotations/xml.

View File

@ -26,19 +26,24 @@ package org.hibernate.metamodel.source.annotations.entity;
import java.util.List;
import java.util.Map;
import javax.persistence.GenerationType;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.DotName;
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;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.binding.Caching;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.binding.IdGenerator;
import org.hibernate.metamodel.binding.EntityDiscriminator;
import org.hibernate.metamodel.binding.ManyToOneAttributeBinding;
import org.hibernate.metamodel.binding.SimpleAttributeBinding;
@ -58,6 +63,7 @@ import org.hibernate.metamodel.source.annotations.entity.state.relational.Column
import org.hibernate.metamodel.source.annotations.entity.state.relational.ManyToOneRelationalStateImpl;
import org.hibernate.metamodel.source.annotations.entity.state.relational.TupleRelationalStateImpl;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.hibernate.service.classloading.spi.ClassLoaderService;
@ -311,6 +317,7 @@ public class EntityBinder {
}
}
private void bindJpaEntityAnnotation(EntityBinding entityBinding) {
AnnotationInstance jpaEntityAnnotation = JandexHelper.getSingleAnnotation(
configuredClass.getClassInfo(), JPADotNames.ENTITY
@ -341,6 +348,46 @@ public class EntityBinder {
SimpleAttributeBinding attributeBinding = entityBinding.makeSimpleIdAttributeBinding( idName );
attributeBinding.initialize( new AttributeBindingStateImpl( (SimpleAttribute) idAttribute ) );
attributeBinding.initialize( new ColumnRelationalStateImpl( (SimpleAttribute) idAttribute, meta ) );
}
private void bindSingleIdGeneratedValue(EntityBinding entityBinding,String idPropertyName){
AnnotationInstance generatedValueAnn = JandexHelper.getSingleAnnotation(
configuredClass.getClassInfo(), JPADotNames.GENERATED_VALUE
);
if ( generatedValueAnn == null ) {
return;
}
String idName = JandexHelper.getPropertyName( generatedValueAnn.target() );
if ( !idPropertyName.equals( idName ) ) {
throw new AssertionFailure(
String.format(
"Attribute[%s.%s] with @GeneratedValue doesn't have a @Id.",
configuredClass.getName(),
idPropertyName
)
);
}
String generator = JandexHelper.getValueAsString( generatedValueAnn, "generator" );
if ( StringHelper.isNotEmpty( generator ) ) {
IdGenerator idGenerator = ( (MetadataImpl) meta ).getIdGenerator( generator );
if ( idGenerator == null ) {
throw new MappingException(
String.format(
"@GeneratedValue on %s.%s refering an undefined generator [%s]",
configuredClass.getName(),
idName,
generator
)
);
}
entityBinding.getEntityIdentifier().setIdGenerator( idGenerator );
}
GenerationType strategy = JandexHelper.getValueAsEnum( generatedValueAnn, "strategy", GenerationType.class );
entityBinding.getEntityIdentifier().setGenerationType( strategy );
}
private void bindAttributes(EntityBinding entityBinding) {

View File

@ -95,20 +95,21 @@ public class JandexHelper {
return ( (FieldInfo) target ).name();
}
else {
String methodName = ( (MethodInfo) target ).name();
final String methodName = ( (MethodInfo) target ).name();
String propertyName;
if ( methodName.startsWith( "is" ) ) {
methodName = Introspector.decapitalize( methodName.substring( 2 ) );
propertyName = Introspector.decapitalize( methodName.substring( 2 ) );
}
else if ( methodName.startsWith( "has" ) ) {
methodName = Introspector.decapitalize( methodName.substring( 3 ) );
propertyName = Introspector.decapitalize( methodName.substring( 3 ) );
}
else if ( methodName.startsWith( "get" ) ) {
methodName = Introspector.decapitalize( methodName.substring( 3 ) );
propertyName = Introspector.decapitalize( methodName.substring( 3 ) );
}
else {
throw new AssertionFailure( "Expected a method following the Java Bean notation" );
}
return methodName;
return propertyName;
}
}