HHH-6278 Added some comments and renamed method in EntityClass - getPrimaryTableName to getClassNameForTable

This commit is contained in:
Hardy Ferentschik 2011-07-07 11:51:02 +02:00
parent f84ab09e57
commit b600f027a4
5 changed files with 106 additions and 113 deletions

View File

@ -399,44 +399,49 @@ private Caching createCachingForCacheableAnnotation(EntityBindingStateImpl entit
return new Caching( region, defaultAccessType, true );
}
private Table createTable() {
String schmaName = null;
String catalogName = null;
String tableName = null;
AnnotationInstance tableAnnotation = JandexHelper.getSingleAnnotation(
entityClass.getClassInfo(), JPADotNames.TABLE
);
if ( tableAnnotation != null ) {
schmaName = JandexHelper.getValueAsString( tableAnnotation, "schema" );
catalogName = JandexHelper.getValueAsString( tableAnnotation, "catalog" );
tableName = JandexHelper.getValueAsString( tableAnnotation, "name" );
}
private Table createTable() {
String schemaName = null;
String catalogName = null;
String tableName = null;
// is there an explicit @Table annotation?
AnnotationInstance tableAnnotation = JandexHelper.getSingleAnnotation(
entityClass.getClassInfo(), JPADotNames.TABLE
);
if ( tableAnnotation != null ) {
schemaName = JandexHelper.getValueAsString( tableAnnotation, "schema" );
catalogName = JandexHelper.getValueAsString( tableAnnotation, "catalog" );
String explicitTableName = JandexHelper.getValueAsString( tableAnnotation, "name" );
if ( StringHelper.isNotEmpty( explicitTableName ) ) {
tableName = meta.getNamingStrategy().tableName( explicitTableName );
}
}
if ( StringHelper.isEmpty( tableName ) ) {
tableName = meta.getNamingStrategy().classToTableName( entityClass.getPrimaryTableName() );
// no explicit table name given, let's use the entity name as table name (taking inheritance into consideration
if ( StringHelper.isEmpty( tableName ) ) {
tableName = meta.getNamingStrategy().classToTableName( entityClass.getClassNameForTable() );
}
}
else {
tableName = meta.getNamingStrategy().tableName( tableName );
}
if ( meta.isGloballyQuotedIdentifiers() ) {
schmaName = StringHelper.quote( schmaName );
catalogName = StringHelper.quote( catalogName );
tableName = StringHelper.quote( tableName );
}
final Identifier tableNameIdentifier = Identifier.toIdentifier( tableName );
final Schema schema = meta.getDatabase().getSchema( new Schema.Name( schmaName, catalogName ) );
Table table = schema.getTable( tableNameIdentifier );
if ( table == null ) {
table = schema.createTable( tableNameIdentifier );
}
return table;
}
// check whether the names should be globally quoted
if ( meta.isGloballyQuotedIdentifiers() ) {
schemaName = StringHelper.quote( schemaName );
catalogName = StringHelper.quote( catalogName );
tableName = StringHelper.quote( tableName );
}
// last, but not least create the metamodel relational objects
final Identifier tableNameIdentifier = Identifier.toIdentifier( tableName );
final Schema schema = meta.getDatabase().getSchema( new Schema.Name( schemaName, catalogName ) );
Table table = schema.getTable( tableNameIdentifier );
if ( table == null ) {
table = schema.createTable( tableNameIdentifier );
}
return table;
}
private void bindTable(EntityBinding entityBinding) {
Table table = createTable();
Table table = createTable();
entityBinding.setBaseTable( table );
AnnotationInstance checkAnnotation = JandexHelper.getSingleAnnotation(
@ -466,7 +471,6 @@ private void bindId(EntityBinding entityBinding) {
}
}
private void bindJpaEntityAnnotation(EntityBindingStateImpl entityBindingState) {
AnnotationInstance jpaEntityAnnotation = JandexHelper.getSingleAnnotation(
entityClass.getClassInfo(), JPADotNames.ENTITY
@ -587,9 +591,9 @@ private void bindSingleIdGeneratedValue(EntityBinding entityBinding, String idPr
GenerationType.class
);
String strategy = IdGeneratorBinder.generatorType(
generationType,
meta.getOptions().useNewIdentifierGenerators()
);
generationType,
meta.getOptions().useNewIdentifierGenerators()
);
if ( idGenerator != null && !strategy.equals( idGenerator.getStrategy() ) ) {
//todo how to ?
throw new MappingException(

View File

@ -28,7 +28,6 @@
import javax.persistence.AccessType;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
@ -37,7 +36,6 @@
import org.hibernate.metamodel.binding.InheritanceType;
import org.hibernate.metamodel.source.annotations.AnnotationBindingContext;
import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
/**
* Represents an entity or mapped superclass configured via annotations/xml.
@ -48,7 +46,7 @@ public class EntityClass extends ConfiguredClass {
private final AccessType hierarchyAccessType;
private final InheritanceType inheritanceType;
private final boolean hasOwnTable;
private final String primaryTableName;
private final String entityBasedTableName;
private final IdType idType;
private final EntityClass jpaEntityParent;
@ -64,7 +62,7 @@ public EntityClass(ClassInfo classInfo,
this.idType = determineIdType();
this.jpaEntityParent = findJpaEntitySuperClass();
this.hasOwnTable = definesItsOwnTable();
this.primaryTableName = determinePrimaryTableName();
this.entityBasedTableName = determineEntityBasedTableName();
}
/**
@ -94,9 +92,9 @@ public IdType getIdType() {
public boolean hasOwnTable() {
return hasOwnTable;
}
//todo change a better method name
public String getPrimaryTableName() {
return primaryTableName;
public String getClassNameForTable() {
return entityBasedTableName;
}
@Override
@ -107,7 +105,7 @@ public String toString() {
sb.append( ", hierarchyAccessType=" ).append( hierarchyAccessType );
sb.append( ", inheritanceType=" ).append( inheritanceType );
sb.append( ", hasOwnTable=" ).append( hasOwnTable );
sb.append( ", primaryTableName='" ).append( primaryTableName ).append( '\'' );
sb.append( ", primaryTableName='" ).append( entityBasedTableName ).append( '\'' );
sb.append( ", idType=" ).append( idType );
sb.append( '}' );
return sb.toString();
@ -120,12 +118,7 @@ private boolean definesItsOwnTable() {
}
if ( InheritanceType.SINGLE_TABLE.equals( inheritanceType ) ) {
if ( isEntityRoot() ) {
return true;
}
else {
return false;
}
return isEntityRoot();
}
return true;
}
@ -141,14 +134,13 @@ private EntityClass findJpaEntitySuperClass() {
return null;
}
private String determinePrimaryTableName() {
private String determineEntityBasedTableName() {
String tableName = null;
if ( hasOwnTable() ) {
tableName = getConfiguredClass().getSimpleName();
}
else if ( getParent() != null
&& !getParent().getConfiguredClassType().equals( ConfiguredClassType.MAPPED_SUPERCLASS ) ) {
tableName = ( (EntityClass) getParent() ).getPrimaryTableName();
else if ( jpaEntityParent != null ) {
tableName = jpaEntityParent.getClassNameForTable();
}
return tableName;
}

View File

@ -42,17 +42,17 @@ public abstract class BaseAnnotationBindingTestCase extends BaseUnitTestCase {
public void tearDown() {
sources = null;
meta = null;
}
}
public void buildMetadataSources(String ormPath, Class<?>... classes) {
sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
if(ormPath!=null){
sources.addResource( ormPath );
}
for ( Class clazz : classes ) {
sources.addAnnotatedClass( clazz );
}
}
public void buildMetadataSources(String ormPath, Class<?>... classes) {
sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
if ( ormPath != null ) {
sources.addResource( ormPath );
}
for ( Class clazz : classes ) {
sources.addAnnotatedClass( clazz );
}
}
public void buildMetadataSources(Class<?>... classes) {
buildMetadataSources( null, classes );
@ -71,7 +71,6 @@ public EntityBinding getRootEntityBinding(Class<?> clazz) {
}
return meta.getRootEntityBinding( clazz.getName() );
}
}

View File

@ -15,55 +15,53 @@
* @author Strong Liu
*/
public class QuotedIdentifierTests extends BaseAnnotationBindingTestCase {
String ormPath = "org/hibernate/metamodel/source/annotations/xml/orm-quote-identifier.xml";
String ormPath = "org/hibernate/metamodel/source/annotations/xml/orm-quote-identifier.xml";
@Test
public void testDelimitedIdentifiers() {
buildMetadataSources( ormPath, Item.class, Item2.class, Item3.class, Item4.class );
EntityBinding item = getEntityBinding( Item.class );
assertIdentifierEquals( "`Item`",item );
@Test
public void testDelimitedIdentifiers() {
buildMetadataSources( ormPath, Item.class, Item2.class, Item3.class, Item4.class );
EntityBinding item = getEntityBinding( Item.class );
assertIdentifierEquals( "`Item`", item );
item = getEntityBinding( Item2.class );
assertIdentifierEquals( "`TABLE_ITEM2`",item );
item = getEntityBinding( Item2.class );
assertIdentifierEquals( "`TABLE_ITEM2`", item );
item = getEntityBinding( Item3.class );
assertIdentifierEquals( "`TABLE_ITEM3`",item );
item = getEntityBinding( Item3.class );
assertIdentifierEquals( "`TABLE_ITEM3`", item );
item = getEntityBinding( Item4.class );
assertIdentifierEquals( "`TABLE_ITEM4`",item );
item = getEntityBinding( Item4.class );
assertIdentifierEquals( "`TABLE_ITEM4`", item );
}
private void assertIdentifierEquals(String expected, EntityBinding realValue) {
org.hibernate.metamodel.relational.Table table = (org.hibernate.metamodel.relational.Table) realValue.getBaseTable();
assertEquals( Identifier.toIdentifier( expected ), table.getTableName() );
}
}
@Entity
private static class Item {
@Id
Long id;
}
private void assertIdentifierEquals(String expected, EntityBinding realValue) {
org.hibernate.metamodel.relational.Table table = (org.hibernate.metamodel.relational.Table) realValue.getBaseTable();
assertEquals( Identifier.toIdentifier( expected ), table.getTableName() );
}
@Entity
@Table(name = "TABLE_ITEM2")
private static class Item2 {
@Id
Long id;
}
@Entity
private static class Item {
@Id
Long id;
}
@Entity
@Table(name = "`TABLE_ITEM3`")
private static class Item3 {
@Id
Long id;
}
@Entity
@Table(name = "TABLE_ITEM2")
private static class Item2 {
@Id
Long id;
}
@Entity
@Table(name = "`TABLE_ITEM3`")
private static class Item3 {
@Id
Long id;
}
@Entity
@Table(name = "\"TABLE_ITEM4\"")
private static class Item4 {
@Id
Long id;
}
@Entity
@Table(name = "\"TABLE_ITEM4\"")
private static class Item4 {
@Id
Long id;
}
}

View File

@ -100,7 +100,7 @@ class B extends A {
"wrong inheritance type", InheritanceType.SINGLE_TABLE, entityClass.getInheritanceType()
);
Assert.assertEquals(
"wrong table name", "A", entityClass.getPrimaryTableName()
"wrong table name", "A", entityClass.getClassNameForTable()
);
assertTrue( iter.hasNext() );
@ -112,7 +112,7 @@ class B extends A {
"wrong inheritance type", InheritanceType.SINGLE_TABLE, entityClass.getInheritanceType()
);
Assert.assertEquals(
"wrong table name", "A", entityClass.getPrimaryTableName()
"wrong table name", "A", entityClass.getClassNameForTable()
);
assertFalse( iter.hasNext() );
@ -148,7 +148,7 @@ class B extends A {
"wrong inheritance type", InheritanceType.TABLE_PER_CLASS, entityClass.getInheritanceType()
);
Assert.assertEquals(
"wrong table name", "A", entityClass.getPrimaryTableName()
"wrong table name", "A", entityClass.getClassNameForTable()
);
assertTrue( iter.hasNext() );
@ -160,7 +160,7 @@ class B extends A {
"wrong inheritance type", InheritanceType.TABLE_PER_CLASS, entityClass.getInheritanceType()
);
Assert.assertEquals(
"wrong table name", "B", entityClass.getPrimaryTableName()
"wrong table name", "B", entityClass.getClassNameForTable()
);
assertFalse( iter.hasNext() );
@ -197,7 +197,7 @@ class B extends A {
"wrong inheritance type", InheritanceType.JOINED, entityClass.getInheritanceType()
);
Assert.assertEquals(
"wrong table name", "A", entityClass.getPrimaryTableName()
"wrong table name", "A", entityClass.getClassNameForTable()
);
assertTrue( iter.hasNext() );
@ -209,7 +209,7 @@ class B extends A {
"wrong inheritance type", InheritanceType.JOINED, entityClass.getInheritanceType()
);
Assert.assertEquals(
"wrong table name", "B", entityClass.getPrimaryTableName()
"wrong table name", "B", entityClass.getClassNameForTable()
);
assertFalse( iter.hasNext() );