HHH-6174 Creating schema and (primary) table

This commit is contained in:
Hardy Ferentschik 2011-05-03 10:07:29 +02:00
parent e7c26b28a6
commit 95802a0e93
11 changed files with 273 additions and 89 deletions

View File

@ -44,6 +44,7 @@ import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.MethodInfo;
@ -74,12 +75,12 @@ public class ConfiguredClass {
private final InheritanceType inheritanceType;
private final boolean hasOwnTable;
private final String primaryTableName;
private final AnnotationInstance tableAnnotation;
//private final AnnotationInstance tableAnnotation;
private final boolean isMappedSuperClass;
private final boolean isEmbeddable;
private final Map<String, MappedProperty> mappedProperties;
private final Map<String, MappedAttribute> mappedAttributes;
public ConfiguredClass(ClassInfo info,
ConfiguredClass parent,
@ -104,10 +105,6 @@ public class ConfiguredClass {
);
isEmbeddable = embeddableAnnotation != null;
tableAnnotation = JandexHelper.getSingleAnnotation(
classInfo, JPADotNames.TABLE
);
// todo think about how exactly to handle embeddables regarding access type etc
classAccessType = determineClassAccessType();
@ -115,14 +112,14 @@ public class ConfiguredClass {
hasOwnTable = definesItsOwnTable();
primaryTableName = determinePrimaryTableName();
List<MappedProperty> properties = collectMappedProperties( resolvedType );
List<MappedAttribute> properties = collectMappedProperties( resolvedType );
// make sure the properties are ordered by property name
Collections.sort( properties );
Map<String, MappedProperty> tmpMap = new LinkedHashMap<String, MappedProperty>();
for ( MappedProperty property : properties ) {
Map<String, MappedAttribute> tmpMap = new LinkedHashMap<String, MappedAttribute>();
for ( MappedAttribute property : properties ) {
tmpMap.put( property.getName(), property );
}
mappedProperties = Collections.unmodifiableMap( tmpMap );
mappedAttributes = Collections.unmodifiableMap( tmpMap );
}
public String getName() {
@ -161,12 +158,12 @@ public class ConfiguredClass {
return primaryTableName;
}
public Iterable<MappedProperty> getMappedProperties() {
return mappedProperties.values();
public Iterable<MappedAttribute> getMappedAttributes() {
return mappedAttributes.values();
}
public MappedProperty getMappedProperty(String propertyName) {
return mappedProperties.get( propertyName );
public MappedAttribute getMappedProperty(String propertyName) {
return mappedAttributes.get( propertyName );
}
@Override
@ -174,7 +171,7 @@ public class ConfiguredClass {
final StringBuilder sb = new StringBuilder();
sb.append( "ConfiguredClass" );
sb.append( "{clazz=" ).append( clazz );
sb.append( ", mappedProperties=" ).append( mappedProperties );
sb.append( ", mappedAttributes=" ).append( mappedAttributes );
sb.append( ", classAccessType=" ).append( classAccessType );
sb.append( ", isRoot=" ).append( isRoot );
sb.append( ", inheritanceType=" ).append( inheritanceType );
@ -197,7 +194,7 @@ public class ConfiguredClass {
/**
* @return A list of the persistent properties of this configured class
*/
private List<MappedProperty> collectMappedProperties(ResolvedTypeWithMembers resolvedTypes) {
private List<MappedAttribute> collectMappedProperties(ResolvedTypeWithMembers resolvedTypes) {
// create sets of transient field and method names
Set<String> transientFieldNames = new HashSet<String>();
Set<String> transientMethodNames = new HashSet<String>();
@ -216,7 +213,7 @@ public class ConfiguredClass {
throw new AssertionFailure( "Unable to resolve types for " + clazz.getName() );
}
List<MappedProperty> properties = new ArrayList<MappedProperty>();
List<MappedAttribute> properties = new ArrayList<MappedAttribute>();
Set<String> explicitlyConfiguredMemberNames = createExplicitlyConfiguredAccessProperties(
properties, resolvedType
);
@ -265,7 +262,7 @@ public class ConfiguredClass {
*
* @return the property names of the explicitly configured class names in a set
*/
private Set<String> createExplicitlyConfiguredAccessProperties(List<MappedProperty> mappedProperties, ResolvedTypeWithMembers resolvedMembers) {
private Set<String> createExplicitlyConfiguredAccessProperties(List<MappedAttribute> mappedProperties, ResolvedTypeWithMembers resolvedMembers) {
Set<String> explicitAccessMembers = new HashSet<String>();
List<AnnotationInstance> accessAnnotations = classInfo.annotations().get( JPADotNames.ACCESS );
@ -350,8 +347,8 @@ public class ConfiguredClass {
return explicitAccessMembers;
}
private MappedProperty createMappedProperty(Member member, ResolvedTypeWithMembers resolvedType) {
String name = ReflectionHelper.getPropertyName( member );
private MappedAttribute createMappedProperty(Member member, ResolvedTypeWithMembers resolvedType) {
final String name = ReflectionHelper.getPropertyName( member );
ResolvedMember[] resolvedMembers;
if ( member instanceof Field ) {
resolvedMembers = resolvedType.getMemberFields();
@ -359,8 +356,11 @@ public class ConfiguredClass {
else {
resolvedMembers = resolvedType.getMemberMethods();
}
Type type = findResolvedType( member.getName(), resolvedMembers );
return new MappedProperty( name, (Class) type );
final Type type = findResolvedType( member.getName(), resolvedMembers );
final Map<DotName, List<AnnotationInstance>> annotations = JandexHelper.getMemberAnnotations(
classInfo, member.getName()
);
return new MappedAttribute( name, (Class) type, annotations );
}
private Type findResolvedType(String name, ResolvedMember[] resolvedMembers) {
@ -417,6 +417,9 @@ public class ConfiguredClass {
String tableName = null;
if ( hasOwnTable() ) {
tableName = clazz.getSimpleName();
AnnotationInstance tableAnnotation = JandexHelper.getSingleAnnotation(
classInfo, JPADotNames.TABLE
);
if ( tableAnnotation != null ) {
AnnotationValue value = tableAnnotation.value( "name" );
String tmp = value == null ? null : value.asString();

View File

@ -190,7 +190,7 @@ public class ConfiguredClassHierarchy implements Iterable<ConfiguredClass> {
private AccessType throwIdNotFoundAnnotationException(List<ClassInfo> classes) {
StringBuilder builder = new StringBuilder();
builder.append( "Unable to find Id property for class hierarchy " );
builder.append( "Unable to determine identifier attribute for class hierarchy " );
builder.append( hierarchyListString( classes ) );
throw new AnnotationException( builder.toString() );
}

View File

@ -29,6 +29,7 @@ import java.util.Map;
import java.util.Set;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.hibernate.AssertionFailure;
import org.hibernate.MappingException;
@ -43,6 +44,7 @@ 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.relational.Size;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
@ -54,15 +56,17 @@ import org.hibernate.metamodel.source.internal.MetadataImpl;
* @author Hardy Ferentschik
*/
public class EntityBinder {
private final ConfiguredClass entity;
private final ConfiguredClass configuredClass;
private final MetadataImpl meta;
private final Schema.Name schemaName;
public EntityBinder(MetadataImpl metadata, ConfiguredClass configuredClass) {
this.entity = configuredClass;
this.configuredClass = configuredClass;
this.meta = metadata;
EntityBinding entityBinding = new EntityBinding();
bindJpaEntityAnnotation( entityBinding );
bindHibernateEntityAnnotation( entityBinding ); // optional hibernate specific @org.hibernate.annotations.Entity
schemaName = createSchemaName();
bindTable( entityBinding );
if ( configuredClass.isRoot() ) {
@ -71,19 +75,48 @@ public class EntityBinder {
meta.addEntity( entityBinding );
}
private Schema.Name createSchemaName() {
String schema = "";
String catalog = "";
AnnotationInstance tableAnnotation = JandexHelper.getSingleAnnotation(
configuredClass.getClassInfo(), JPADotNames.TABLE
);
if ( tableAnnotation != null ) {
AnnotationValue schemaValue = tableAnnotation.value( "schema" );
AnnotationValue catalogValue = tableAnnotation.value( "catalog" );
schema = schemaValue != null ? schemaValue.asString() : "";
catalog = catalogValue != null ? catalogValue.asString() : "";
}
return new Schema.Name( schema, catalog );
}
private void bindTable(EntityBinding entityBinding) {
final Schema schema = meta.getDatabase().getSchema( schemaName );
final Identifier tableName = Identifier.toIdentifier( configuredClass.getPrimaryTableName() );
org.hibernate.metamodel.relational.Table table = schema.getTable( tableName );
if ( table == null ) {
table = schema.createTable( tableName );
}
entityBinding.setBaseTable( table );
// this.schemaName = new Schema.Name(
// ( entityClazz.getSchema() == null ?
// hibernateMappingBinder.getDefaultSchemaName() :
// entityClazz.getSchema() ),
// ( entityClazz.getCatalog() == null ?
// hibernateMappingBinder.getDefaultCatalogName() :
// entityClazz.getCatalog() )
// );
AnnotationInstance hibernateTableAnnotation = JandexHelper.getSingleAnnotation(
configuredClass.getClassInfo(), HibernateDotNames.TABLE
);
if ( hibernateTableAnnotation != null && hibernateTableAnnotation.value( "comment" ) != null ) {
table.addComment( hibernateTableAnnotation.value( "comment" ).asString().trim() );
}
// todo map rest of Hibernate @Table attributes
final Schema schema = meta.getDatabase().getSchema( null );
AnnotationInstance checkAnnotation = JandexHelper.getSingleAnnotation(
configuredClass.getClassInfo(), HibernateDotNames.CHECK
);
if ( checkAnnotation != null ) {
table.addCheckConstraint( checkAnnotation.value( "constraints" ).asString() );
}
}
private void bindId(EntityBinding entityBinding) {
@ -107,11 +140,11 @@ public class EntityBinder {
private void bindJpaEntityAnnotation(EntityBinding entityBinding) {
AnnotationInstance jpaEntityAnnotation = JandexHelper.getSingleAnnotation(
entity.getClassInfo(), JPADotNames.ENTITY
configuredClass.getClassInfo(), JPADotNames.ENTITY
);
String name;
if ( jpaEntityAnnotation.value( "name" ) == null ) {
name = StringHelper.unqualify( entity.getName() );
name = StringHelper.unqualify( configuredClass.getName() );
}
else {
name = jpaEntityAnnotation.value( "name" ).asString();
@ -121,26 +154,26 @@ public class EntityBinder {
private void bindSingleIdAnnotation(EntityBinding entityBinding) {
AnnotationInstance idAnnotation = JandexHelper.getSingleAnnotation(
entity.getClassInfo(), JPADotNames.ID
configuredClass.getClassInfo(), JPADotNames.ID
);
String idName = JandexHelper.getPropertyName( idAnnotation.target() );
entityBinding.getEntity().getOrCreateSingularAttribute( idName );
SimpleAttributeBinding idBinding = entityBinding.makeSimplePrimaryKeyAttributeBinding( idName );
MappedProperty idProperty = entity.getMappedProperty( idName );
MappedAttribute idAttribute = configuredClass.getMappedProperty( idName );
AnnotationSimpleAttributeDomainState domainState = new AnnotationSimpleAttributeDomainState();
HibernateTypeDescriptor typeDescriptor = new HibernateTypeDescriptor();
typeDescriptor.setTypeName( idProperty.getType().getName() );
typeDescriptor.setTypeName( idAttribute.getType().getName() );
domainState.typeDescriptor = typeDescriptor;
domainState.attribute = entityBinding.getEntity().getOrCreateSingularAttribute( idProperty.getName() );
domainState.attribute = entityBinding.getEntity().getOrCreateSingularAttribute( idAttribute.getName() );
idBinding.initialize( domainState );
AnnotationColumnRelationalState columnRelationsState = new AnnotationColumnRelationalState();
columnRelationsState.namingStrategy = meta.getNamingStrategy();
columnRelationsState.columnName = idProperty.getColumnName();
columnRelationsState.columnName = idAttribute.getColumnName();
columnRelationsState.unique = true;
columnRelationsState.nullable = false;
@ -151,7 +184,7 @@ public class EntityBinder {
private void bindHibernateEntityAnnotation(EntityBinding entityBinding) {
AnnotationInstance hibernateEntityAnnotation = JandexHelper.getSingleAnnotation(
entity.getClassInfo(), HibernateDotNames.ENTITY
configuredClass.getClassInfo(), HibernateDotNames.ENTITY
);
// if ( hibAnn != null ) {
// dynamicInsert = hibAnn.dynamicInsert();
@ -173,7 +206,7 @@ public class EntityBinder {
}
private Hierarchical getSuperType() {
ConfiguredClass parent = entity.getParent();
ConfiguredClass parent = configuredClass.getParent();
if ( parent == null ) {
return null;
}
@ -181,7 +214,7 @@ public class EntityBinder {
EntityBinding parentBinding = meta.getEntityBinding( parent.getName() );
if ( parentBinding == null ) {
throw new AssertionFailure(
"Parent entity " + parent.getName() + " of entity " + entity.getName() + "not yet created!"
"Parent entity " + parent.getName() + " of entity " + configuredClass.getName() + "not yet created!"
);
}
@ -189,14 +222,14 @@ public class EntityBinder {
}
private IdType determineIdType() {
List<AnnotationInstance> idAnnotations = entity.getClassInfo().annotations().get( JPADotNames.ENTITY );
List<AnnotationInstance> embeddedIdAnnotations = entity.getClassInfo()
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 " + entity.getName() + "."
"@EmbeddedId and @Id cannot be used together. Check the configuration for " + configuredClass.getName() + "."
);
}

View File

@ -23,34 +23,52 @@
*/
package org.hibernate.metamodel.source.annotations;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.DotName;
/**
* Represent a mapped property (explicitly or implicitly mapped).
* Represent a mapped attribute (explicitly or implicitly mapped).
*
* @author Hardy Ferentschik
*/
public class MappedProperty implements Comparable<MappedProperty> {
public class MappedAttribute implements Comparable<MappedAttribute> {
private final String name;
private final Class<?> type;
private final Map<DotName, List<AnnotationInstance>> annotations;
MappedProperty(String name, Class<?> type) {
MappedAttribute(String name, Class<?> type, Map<DotName, List<AnnotationInstance>> annotations) {
this.name = name;
this.type = type;
this.annotations = annotations;
}
public String getName() {
final public String getName() {
return name;
}
public String getColumnName() {
final public String getColumnName() {
return name;
}
public Class<?> getType() {
final public Class<?> getType() {
return type;
}
public final List<AnnotationInstance> annotations(DotName annotationDotName) {
if ( annotations.containsKey( annotationDotName ) ) {
return annotations.get( annotationDotName );
}
else {
return Collections.emptyList();
}
}
@Override
public int compareTo(MappedProperty mappedProperty) {
public int compareTo(MappedAttribute mappedProperty) {
return name.compareTo( mappedProperty.getName() );
}

View File

@ -26,7 +26,10 @@ package org.hibernate.metamodel.source.annotations.util;
import java.beans.Introspector;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
@ -37,7 +40,6 @@ import org.jboss.jandex.Index;
import org.jboss.jandex.Indexer;
import org.jboss.jandex.MethodInfo;
import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.service.classloading.spi.ClassLoaderService;
@ -145,6 +147,46 @@ public class JandexHelper {
}
return indexer.complete();
}
public static Map<DotName, List<AnnotationInstance>> getMemberAnnotations(ClassInfo classInfo, String name) {
if ( classInfo == null ) {
throw new IllegalArgumentException( "classInfo cannot be null" );
}
if ( name == null ) {
throw new IllegalArgumentException( "name cannot be null" );
}
Map<DotName, List<AnnotationInstance>> annotations = new HashMap<DotName, List<AnnotationInstance>>();
for ( List<AnnotationInstance> annotationList : classInfo.annotations().values() ) {
for ( AnnotationInstance instance : annotationList ) {
String targetName = null;
if ( instance.target() instanceof FieldInfo ) {
targetName = ( (FieldInfo) instance.target() ).name();
}
else if ( instance.target() instanceof MethodInfo ) {
targetName = ( (MethodInfo) instance.target() ).name();
}
if ( targetName != null && name.equals( targetName ) ) {
addAnnotationToMap( instance, annotations );
}
}
}
return annotations;
}
private static void addAnnotationToMap(AnnotationInstance instance, Map<DotName, List<AnnotationInstance>> annotations) {
DotName dotName = instance.name();
List<AnnotationInstance> list;
if ( annotations.containsKey( dotName ) ) {
list = annotations.get( dotName );
}
else {
list = new ArrayList<AnnotationInstance>();
annotations.put( dotName, list );
}
list.add( instance );
}
}

View File

@ -60,7 +60,7 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column
/* package-protected */
HbmColumnRelationalState(XMLColumnElement columnElement,
HbmSimpleValueRelationalStateContainer container) {
HbmSimpleValueRelationalStateContainer container) {
this.container = container;
this.explicitColumnName = columnElement.getName();
this.size = createSize( columnElement.getLength(), columnElement.getScale(), columnElement.getPrecision() );
@ -70,8 +70,8 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column
this.defaultColumnValue = columnElement.getDefault();
this.sqlType = columnElement.getSqlType();
this.customWrite = columnElement.getWrite();
if ( customWrite != null && ! customWrite.matches("[^?]*\\?[^?]*") ) {
throw new MappingException("write expression must contain exactly one value placeholder ('?') character");
if ( customWrite != null && !customWrite.matches( "[^?]*\\?[^?]*" ) ) {
throw new MappingException( "write expression must contain exactly one value placeholder ('?') character" );
}
this.customRead = columnElement.getRead();
this.comment = columnElement.getComment() == null ? null : columnElement.getComment().trim();
@ -82,7 +82,7 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column
}
HbmColumnRelationalState(XMLPropertyElement property,
HbmSimpleValueRelationalStateContainer container) {
HbmSimpleValueRelationalStateContainer container) {
this.container = container;
this.explicitColumnName = property.getName();
this.size = createSize( property.getLength(), property.getScale(), property.getPrecision() );
@ -101,11 +101,11 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column
}
HbmColumnRelationalState(XMLManyToOneElement manyToOne,
HbmSimpleValueRelationalStateContainer container) {
HbmSimpleValueRelationalStateContainer container) {
this.container = container;
this.explicitColumnName = manyToOne.getName();
this.size = new Size();
this.isNullable =! MappingHelper.getBooleanValue( manyToOne.isNotNull(), false );
this.isNullable = !MappingHelper.getBooleanValue( manyToOne.isNotNull(), false );
this.isUnique = manyToOne.isUnique();
this.checkCondition = null;
this.defaultColumnValue = null;
@ -120,8 +120,8 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column
}
HbmColumnRelationalState(XMLId id,
HbmSimpleValueRelationalStateContainer container) {
if ( id.getColumn() != null && ! id.getColumn().isEmpty() ) {
HbmSimpleValueRelationalStateContainer container) {
if ( id.getColumn() != null && !id.getColumn().isEmpty() ) {
throw new IllegalArgumentException( "This method should not be called with non-empty id.getColumnElement()" );
}
this.container = container;
@ -140,9 +140,11 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column
}
HbmColumnRelationalState(XMLDiscriminator discriminator,
HbmSimpleValueRelationalStateContainer container) {
if ( discriminator.getColumn() != null ) {
throw new IllegalArgumentException( "This method should not be called with null discriminator.getColumnElement()" );
HbmSimpleValueRelationalStateContainer container) {
if ( discriminator.getColumn() != null ) {
throw new IllegalArgumentException(
"This method should not be called with null discriminator.getColumnElement()"
);
}
this.container = container;
this.explicitColumnName = null;
@ -160,11 +162,13 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column
}
HbmColumnRelationalState(XMLVersion version,
HbmSimpleValueRelationalStateContainer container) {
HbmSimpleValueRelationalStateContainer container) {
this.container = container;
this.explicitColumnName = version.getColumnAttribute();
if ( version.getColumn() != null && ! version.getColumn().isEmpty() ) {
throw new IllegalArgumentException( "This method should not be called with non-empty version.getColumnElement()" );
if ( version.getColumn() != null && !version.getColumn().isEmpty() ) {
throw new IllegalArgumentException(
"This method should not be called with non-empty version.getColumnElement()"
);
}
// TODO: should set default
this.size = new Size();
@ -181,7 +185,7 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column
}
HbmColumnRelationalState(XMLTimestamp timestamp,
HbmSimpleValueRelationalStateContainer container) {
HbmSimpleValueRelationalStateContainer container) {
this.container = container;
this.explicitColumnName = timestamp.getColumn();
// TODO: should set default
@ -201,12 +205,15 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column
public NamingStrategy getNamingStrategy() {
return container.getNamingStrategy();
}
public String getExplicitColumnName() {
return explicitColumnName;
}
public Size getSize() {
return size;
}
protected static Size createSize(String length, String scale, String precision) {
// TODO: should this set defaults if length, scale, precision is not specified?
Size size = new Size();
@ -222,6 +229,7 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column
// TODO: is there an attribute for lobMultiplier?
return size;
}
public boolean isNullable() {
return isNullable;
}
@ -233,24 +241,31 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column
public String getCheckCondition() {
return checkCondition;
}
public String getDefault() {
return defaultColumnValue;
}
public String getSqlType() {
return sqlType;
}
public String getCustomWriteFragment() {
return customWrite;
}
public String getCustomReadFragment() {
return customRead;
}
public String getComment() {
return comment;
}
public Set<String> getUniqueKeys() {
return uniqueKeys;
}
public Set<String> getIndexes() {
return indexes;
}

View File

@ -38,7 +38,7 @@ import org.hibernate.testing.FailureExpected;
*/
public class BasicAnnotationBindingTests extends AbstractBasicBindingTests {
@FailureExpected(jiraKey = "HHH-5672", message = "Work in progress")
//@FailureExpected(jiraKey = "HHH-5672", message = "Work in progress")
@Test
public void testSimpleEntityMapping() {
super.testSimpleEntityMapping();

View File

@ -25,11 +25,14 @@ 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

@ -59,12 +59,12 @@ import static org.junit.Assert.assertFalse;
public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
private BasicServiceRegistryImpl serviceRegistry;
private ClassLoaderService service;
private ClassLoaderService classLoaderService;
@Before
public void setUp() {
serviceRegistry = (BasicServiceRegistryImpl) new ServiceRegistryBuilder().buildServiceRegistry();
service = serviceRegistry.getService( ClassLoaderService.class );
classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
}
@After
@ -74,7 +74,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
@Test
public void testSingleEntity() {
Index index = JandexHelper.indexForClass( service, Foo.class );
Index index = JandexHelper.indexForClass( classLoaderService, Foo.class );
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry
);
@ -88,7 +88,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
@Test
public void testSimpleInheritance() {
Index index = JandexHelper.indexForClass( service, B.class, A.class );
Index index = JandexHelper.indexForClass( classLoaderService, B.class, A.class );
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry
);
@ -104,7 +104,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
@Test
public void testMultipleHierarchies() {
Index index = JandexHelper.indexForClass( service, B.class, A.class, Foo.class );
Index index = JandexHelper.indexForClass( classLoaderService, B.class, A.class, Foo.class );
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry
);
@ -130,7 +130,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
}
Index index = JandexHelper.indexForClass(
service, MappedSubClass.class, MappedSuperClass.class, UnmappedSubClass.class
classLoaderService, MappedSubClass.class, MappedSuperClass.class, UnmappedSubClass.class
);
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry
@ -154,7 +154,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class EntityAndMappedSuperClass {
}
Index index = JandexHelper.indexForClass( service, EntityAndMappedSuperClass.class );
Index index = JandexHelper.indexForClass( classLoaderService, EntityAndMappedSuperClass.class );
ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry );
}
@ -165,7 +165,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class EntityAndEmbeddable {
}
Index index = JandexHelper.indexForClass( service, EntityAndEmbeddable.class );
Index index = JandexHelper.indexForClass( classLoaderService, EntityAndEmbeddable.class );
ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry );
}
@ -181,7 +181,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class B extends A {
}
Index index = JandexHelper.indexForClass( service, B.class, A.class );
Index index = JandexHelper.indexForClass( classLoaderService, B.class, A.class );
ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry );
}
@ -197,7 +197,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class B extends A {
}
Index index = JandexHelper.indexForClass( service, B.class, A.class );
Index index = JandexHelper.indexForClass( classLoaderService, B.class, A.class );
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry
);
@ -226,7 +226,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class B extends A {
}
Index index = JandexHelper.indexForClass( service, B.class, A.class );
Index index = JandexHelper.indexForClass( classLoaderService, B.class, A.class );
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry
);
@ -247,7 +247,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class B extends A {
}
Index index = JandexHelper.indexForClass( service, B.class, A.class );
Index index = JandexHelper.indexForClass( classLoaderService, B.class, A.class );
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry
);
@ -275,7 +275,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class B extends A {
}
Index index = JandexHelper.indexForClass( service, B.class, MappedSuperClass.class, A.class );
Index index = JandexHelper.indexForClass( classLoaderService, B.class, MappedSuperClass.class, A.class );
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, serviceRegistry
);
@ -298,7 +298,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase {
class B extends A {
}
Index index = JandexHelper.indexForClass( service, B.class, A.class );
Index index = JandexHelper.indexForClass( classLoaderService, B.class, A.class );
ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry );
}

View File

@ -40,7 +40,7 @@ import org.junit.Test;
import org.hibernate.metamodel.source.annotations.ConfiguredClass;
import org.hibernate.metamodel.source.annotations.ConfiguredClassHierarchy;
import org.hibernate.metamodel.source.annotations.MappedProperty;
import org.hibernate.metamodel.source.annotations.MappedAttribute;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.internal.BasicServiceRegistryImpl;
@ -80,7 +80,7 @@ public class GenericTypeDiscoveryTest extends BaseUnitTestCase {
ConfiguredClass configuredClass = iter.next();
ClassInfo info = configuredClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( Stuff.class.getName() ), info.name() );
MappedProperty property = configuredClass.getMappedProperty( "value" );
MappedAttribute property = configuredClass.getMappedProperty( "value" );
assertEquals( Price.class, property.getType() );
assertTrue( iter.hasNext() );
@ -88,7 +88,7 @@ public class GenericTypeDiscoveryTest extends BaseUnitTestCase {
info = configuredClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( PricedStuff.class.getName() ), info.name() );
assertFalse(
"PricedStuff should not mapped properties", configuredClass.getMappedProperties().iterator().hasNext()
"PricedStuff should not mapped properties", configuredClass.getMappedAttributes().iterator().hasNext()
);
assertTrue( iter.hasNext() );
@ -105,7 +105,7 @@ public class GenericTypeDiscoveryTest extends BaseUnitTestCase {
configuredClass = iter.next();
info = configuredClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( Paper.class.getName() ), info.name() );
assertFalse( "Paper should not mapped properties", configuredClass.getMappedProperties().iterator().hasNext() );
assertFalse( "Paper should not mapped properties", configuredClass.getMappedAttributes().iterator().hasNext() );
assertFalse( iter.hasNext() );
}

View File

@ -0,0 +1,70 @@
package org.hibernate.metamodel.source.annotations.util;
import java.util.List;
import java.util.Map;
import javax.persistence.Basic;
import javax.persistence.Column;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.internal.BasicServiceRegistryImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static junit.framework.Assert.assertTrue;
/**
* @author Hardy Ferentschik
*/
public class JandexHelperTest extends BaseUnitTestCase {
private BasicServiceRegistryImpl serviceRegistry;
private ClassLoaderService classLoaderService;
@Before
public void setUp() {
serviceRegistry = (BasicServiceRegistryImpl) new ServiceRegistryBuilder().buildServiceRegistry();
classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
}
@After
public void tearDown() {
serviceRegistry.destroy();
}
@Test
public void testGetMemberAnnotations() {
class Foo {
@Column
@Basic
private String bar;
private String fubar;
}
Index index = JandexHelper.indexForClass( classLoaderService, Foo.class );
ClassInfo classInfo = index.getClassByName( DotName.createSimple( Foo.class.getName() ) );
Map<DotName, List<AnnotationInstance>> memberAnnotations = JandexHelper.getMemberAnnotations(
classInfo, "bar"
);
assertTrue(
"property bar should defines @Column annotation",
memberAnnotations.containsKey( DotName.createSimple( Column.class.getName() ) )
);
assertTrue(
"property bar should defines @Basic annotation",
memberAnnotations.containsKey( DotName.createSimple( Basic.class.getName() ) )
);
memberAnnotations = JandexHelper.getMemberAnnotations( classInfo, "fubar" );
assertTrue( "there should be no annotations in fubar", memberAnnotations.isEmpty() );
}
}