HHH-6261
This commit is contained in:
parent
625d781af1
commit
3ff1b440c2
|
@ -23,6 +23,12 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.metamodel.binding;
|
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.hibernate.internal.CoreMessageLogger;
|
||||||
|
|
||||||
import org.jboss.logging.Logger;
|
import org.jboss.logging.Logger;
|
||||||
|
@ -38,6 +44,9 @@ public class EntityIdentifier {
|
||||||
|
|
||||||
private final EntityBinding entityBinding;
|
private final EntityBinding entityBinding;
|
||||||
private AttributeBinding attributeBinding;
|
private AttributeBinding attributeBinding;
|
||||||
|
private IdentifierGenerator identifierGenerator;
|
||||||
|
private IdGenerator idGenerator;
|
||||||
|
private GenerationType generationType;
|
||||||
// todo : generator, mappers, etc
|
// todo : generator, mappers, etc
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,4 +70,23 @@ public class EntityIdentifier {
|
||||||
}
|
}
|
||||||
this.attributeBinding = attributeBinding;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,7 @@ import org.hibernate.metamodel.source.annotations.util.JandexHelper;
|
||||||
import org.hibernate.metamodel.source.annotations.util.ReflectionHelper;
|
import org.hibernate.metamodel.source.annotations.util.ReflectionHelper;
|
||||||
import org.hibernate.service.ServiceRegistry;
|
import org.hibernate.service.ServiceRegistry;
|
||||||
import org.hibernate.service.classloading.spi.ClassLoaderService;
|
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.
|
* Represents an entity, mapped superclass or component configured via annotations/xml.
|
||||||
|
|
|
@ -26,19 +26,24 @@ package org.hibernate.metamodel.source.annotations.entity;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
|
||||||
import org.jboss.jandex.AnnotationInstance;
|
import org.jboss.jandex.AnnotationInstance;
|
||||||
import org.jboss.jandex.AnnotationValue;
|
import org.jboss.jandex.AnnotationValue;
|
||||||
import org.jboss.jandex.DotName;
|
import org.jboss.jandex.DotName;
|
||||||
|
|
||||||
import org.hibernate.AnnotationException;
|
import org.hibernate.AnnotationException;
|
||||||
import org.hibernate.AssertionFailure;
|
import org.hibernate.AssertionFailure;
|
||||||
|
import org.hibernate.MappingException;
|
||||||
import org.hibernate.annotations.CacheConcurrencyStrategy;
|
import org.hibernate.annotations.CacheConcurrencyStrategy;
|
||||||
import org.hibernate.annotations.OptimisticLockType;
|
import org.hibernate.annotations.OptimisticLockType;
|
||||||
import org.hibernate.annotations.PolymorphismType;
|
import org.hibernate.annotations.PolymorphismType;
|
||||||
import org.hibernate.cache.spi.RegionFactory;
|
import org.hibernate.cache.spi.RegionFactory;
|
||||||
import org.hibernate.cache.spi.access.AccessType;
|
import org.hibernate.cache.spi.access.AccessType;
|
||||||
|
import org.hibernate.internal.util.StringHelper;
|
||||||
import org.hibernate.metamodel.binding.Caching;
|
import org.hibernate.metamodel.binding.Caching;
|
||||||
import org.hibernate.metamodel.binding.EntityBinding;
|
import org.hibernate.metamodel.binding.EntityBinding;
|
||||||
|
import org.hibernate.metamodel.binding.IdGenerator;
|
||||||
import org.hibernate.metamodel.binding.EntityDiscriminator;
|
import org.hibernate.metamodel.binding.EntityDiscriminator;
|
||||||
import org.hibernate.metamodel.binding.ManyToOneAttributeBinding;
|
import org.hibernate.metamodel.binding.ManyToOneAttributeBinding;
|
||||||
import org.hibernate.metamodel.binding.SimpleAttributeBinding;
|
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.ManyToOneRelationalStateImpl;
|
||||||
import org.hibernate.metamodel.source.annotations.entity.state.relational.TupleRelationalStateImpl;
|
import org.hibernate.metamodel.source.annotations.entity.state.relational.TupleRelationalStateImpl;
|
||||||
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.spi.MetadataImplementor;
|
import org.hibernate.metamodel.source.spi.MetadataImplementor;
|
||||||
import org.hibernate.service.classloading.spi.ClassLoaderService;
|
import org.hibernate.service.classloading.spi.ClassLoaderService;
|
||||||
|
|
||||||
|
@ -311,6 +317,7 @@ public class EntityBinder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void bindJpaEntityAnnotation(EntityBinding entityBinding) {
|
private void bindJpaEntityAnnotation(EntityBinding entityBinding) {
|
||||||
AnnotationInstance jpaEntityAnnotation = JandexHelper.getSingleAnnotation(
|
AnnotationInstance jpaEntityAnnotation = JandexHelper.getSingleAnnotation(
|
||||||
configuredClass.getClassInfo(), JPADotNames.ENTITY
|
configuredClass.getClassInfo(), JPADotNames.ENTITY
|
||||||
|
@ -341,6 +348,46 @@ public class EntityBinder {
|
||||||
SimpleAttributeBinding attributeBinding = entityBinding.makeSimpleIdAttributeBinding( idName );
|
SimpleAttributeBinding attributeBinding = entityBinding.makeSimpleIdAttributeBinding( idName );
|
||||||
attributeBinding.initialize( new AttributeBindingStateImpl( (SimpleAttribute) idAttribute ) );
|
attributeBinding.initialize( new AttributeBindingStateImpl( (SimpleAttribute) idAttribute ) );
|
||||||
attributeBinding.initialize( new ColumnRelationalStateImpl( (SimpleAttribute) idAttribute, meta ) );
|
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) {
|
private void bindAttributes(EntityBinding entityBinding) {
|
||||||
|
|
|
@ -95,20 +95,21 @@ public class JandexHelper {
|
||||||
return ( (FieldInfo) target ).name();
|
return ( (FieldInfo) target ).name();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
String methodName = ( (MethodInfo) target ).name();
|
final String methodName = ( (MethodInfo) target ).name();
|
||||||
|
String propertyName;
|
||||||
if ( methodName.startsWith( "is" ) ) {
|
if ( methodName.startsWith( "is" ) ) {
|
||||||
methodName = Introspector.decapitalize( methodName.substring( 2 ) );
|
propertyName = Introspector.decapitalize( methodName.substring( 2 ) );
|
||||||
}
|
}
|
||||||
else if ( methodName.startsWith( "has" ) ) {
|
else if ( methodName.startsWith( "has" ) ) {
|
||||||
methodName = Introspector.decapitalize( methodName.substring( 3 ) );
|
propertyName = Introspector.decapitalize( methodName.substring( 3 ) );
|
||||||
}
|
}
|
||||||
else if ( methodName.startsWith( "get" ) ) {
|
else if ( methodName.startsWith( "get" ) ) {
|
||||||
methodName = Introspector.decapitalize( methodName.substring( 3 ) );
|
propertyName = Introspector.decapitalize( methodName.substring( 3 ) );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new AssertionFailure( "Expected a method following the Java Bean notation" );
|
throw new AssertionFailure( "Expected a method following the Java Bean notation" );
|
||||||
}
|
}
|
||||||
return methodName;
|
return propertyName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue