HHH-6488 Formatting and commenting

This commit is contained in:
Hardy Ferentschik 2011-07-27 13:31:25 +02:00
parent 462d2adb69
commit 605c9e30a2
5 changed files with 95 additions and 21 deletions

View File

@ -25,24 +25,17 @@ package org.hibernate.metamodel.binding;
import java.util.Properties; import java.util.Properties;
import org.jboss.logging.Logger; import org.hibernate.AssertionFailure;
import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.factory.IdentifierGeneratorFactory; import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.internal.CoreMessageLogger;
/** /**
* Binds the entity identifier. * Binds the entity identifier.
* *
* @author Steve Ebersole * @author Steve Ebersole
* @author Hardy Ferentschik
*/ */
public class EntityIdentifier { public class EntityIdentifier {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
EntityIdentifier.class.getName()
);
private final EntityBinding entityBinding; private final EntityBinding entityBinding;
private SimpleSingularAttributeBinding attributeBinding; private SimpleSingularAttributeBinding attributeBinding;
private IdentifierGenerator identifierGenerator; private IdentifierGenerator identifierGenerator;
@ -65,9 +58,12 @@ public class EntityIdentifier {
public void setValueBinding(SimpleSingularAttributeBinding attributeBinding) { public void setValueBinding(SimpleSingularAttributeBinding attributeBinding) {
if ( this.attributeBinding != null ) { if ( this.attributeBinding != null ) {
// todo : error? or just log? For now throw exception and see what happens. Easier to see whether this throw new AssertionFailure(
// method gets called multiple times String.format(
LOG.entityIdentifierValueBindingExists( entityBinding.getEntity().getName() ); "Identifier value binding already existed for %s",
entityBinding.getEntity().getName()
)
);
} }
this.attributeBinding = attributeBinding; this.attributeBinding = attributeBinding;
} }
@ -77,13 +73,15 @@ public class EntityIdentifier {
} }
public boolean isEmbedded() { public boolean isEmbedded() {
return attributeBinding.getSimpleValueSpan()>1; return attributeBinding.getSimpleValueSpan() > 1;
} }
public boolean isIdentifierMapper() { public boolean isIdentifierMapper() {
return isIdentifierMapper; return isIdentifierMapper;
} }
// todo do we really need this createIdentifierGenerator and how do we make sure the getter is not called too early
// maybe some sort of visitor pattern here!? (HF)
public IdentifierGenerator createIdentifierGenerator(IdentifierGeneratorFactory factory, Properties properties) { public IdentifierGenerator createIdentifierGenerator(IdentifierGeneratorFactory factory, Properties properties) {
if ( idGenerator != null ) { if ( idGenerator != null ) {
identifierGenerator = attributeBinding.createIdentifierGenerator( idGenerator, factory, properties ); identifierGenerator = attributeBinding.createIdentifierGenerator( idGenerator, factory, properties );

View File

@ -121,7 +121,6 @@ public class SimpleSingularAttributeBinding
this.metaAttributeContext = metaAttributeContext; this.metaAttributeContext = metaAttributeContext;
} }
/* package-protected */
IdentifierGenerator createIdentifierGenerator( IdentifierGenerator createIdentifierGenerator(
IdGenerator idGenerator, IdGenerator idGenerator,
IdentifierGeneratorFactory identifierGeneratorFactory, IdentifierGeneratorFactory identifierGeneratorFactory,
@ -138,14 +137,14 @@ public class SimpleSingularAttributeBinding
params.setProperty( PersistentIdentifierGenerator.SCHEMA, schema.getName().getSchema().getName() ); params.setProperty( PersistentIdentifierGenerator.SCHEMA, schema.getName().getSchema().getName() );
} }
if ( schema.getName().getCatalog() != null ) { if ( schema.getName().getCatalog() != null ) {
params.setProperty(PersistentIdentifierGenerator.CATALOG, schema.getName().getCatalog().getName() ); params.setProperty( PersistentIdentifierGenerator.CATALOG, schema.getName().getCatalog().getName() );
} }
} }
// TODO: not sure how this works for collection IDs... // TODO: not sure how this works for collection IDs...
//pass the entity-name, if not a collection-id //pass the entity-name, if not a collection-id
//if ( rootClass!=null) { //if ( rootClass!=null) {
params.setProperty( IdentifierGenerator.ENTITY_NAME, getEntityBinding().getEntity().getName() ); params.setProperty( IdentifierGenerator.ENTITY_NAME, getEntityBinding().getEntity().getName() );
//} //}
//init the table here instead of earlier, so that we can get a quoted table name //init the table here instead of earlier, so that we can get a quoted table name
@ -156,10 +155,12 @@ public class SimpleSingularAttributeBinding
//pass the column name (a generated id almost always has a single column) //pass the column name (a generated id almost always has a single column)
if ( getSimpleValueSpan() > 1 ) { if ( getSimpleValueSpan() > 1 ) {
throw new MappingException( "A SimpleAttributeBinding used for an identifier has a more than 1 Value: " + getAttribute().getName() ); throw new MappingException(
"A SimpleAttributeBinding used for an identifier has more than 1 Value: " + getAttribute().getName()
);
} }
SimpleValue simpleValue = (SimpleValue) getValue(); SimpleValue simpleValue = (SimpleValue) getValue();
if ( ! Column.class.isInstance( simpleValue ) ) { if ( !Column.class.isInstance( simpleValue ) ) {
throw new MappingException( throw new MappingException(
"Cannot create an IdentifierGenerator because the value is not a column: " + "Cannot create an IdentifierGenerator because the value is not a column: " +
simpleValue.toLoggableString() simpleValue.toLoggableString()
@ -167,7 +168,7 @@ public class SimpleSingularAttributeBinding
} }
params.setProperty( params.setProperty(
PersistentIdentifierGenerator.PK, PersistentIdentifierGenerator.PK,
( ( Column ) simpleValue ).getColumnName().encloseInQuotesIfQuoted( ( (Column) simpleValue ).getColumnName().encloseInQuotesIfQuoted(
identifierGeneratorFactory.getDialect() identifierGeneratorFactory.getDialect()
) )
); );
@ -184,7 +185,7 @@ public class SimpleSingularAttributeBinding
// params.setProperty( PersistentIdentifierGenerator.TABLES, tables.toString() ); // params.setProperty( PersistentIdentifierGenerator.TABLES, tables.toString() );
//} //}
//else { //else {
params.setProperty( PersistentIdentifierGenerator.TABLES, tableName ); params.setProperty( PersistentIdentifierGenerator.TABLES, tableName );
//} //}
params.putAll( idGenerator.getParameters() ); params.putAll( idGenerator.getParameters() );

View File

@ -0,0 +1,12 @@
package org.hibernate.metamodel.source.annotations;
/**
* @author Hardy Ferentschik
*/
public class TypeEnumConversionHelper {
private TypeEnumConversionHelper() {
}
}

View File

@ -27,8 +27,8 @@ import org.hibernate.AssertionFailure;
import org.hibernate.EntityMode; import org.hibernate.EntityMode;
import org.hibernate.engine.OptimisticLockStyle; import org.hibernate.engine.OptimisticLockStyle;
import org.hibernate.metamodel.binding.Caching; import org.hibernate.metamodel.binding.Caching;
import org.hibernate.metamodel.source.annotations.attribute.DiscriminatorSourceImpl;
import org.hibernate.metamodel.source.annotations.attribute.BasicAttribute; import org.hibernate.metamodel.source.annotations.attribute.BasicAttribute;
import org.hibernate.metamodel.source.annotations.attribute.DiscriminatorSourceImpl;
import org.hibernate.metamodel.source.annotations.attribute.SimpleIdentifierSourceImpl; import org.hibernate.metamodel.source.annotations.attribute.SimpleIdentifierSourceImpl;
import org.hibernate.metamodel.source.annotations.attribute.SingularAttributeSourceImpl; import org.hibernate.metamodel.source.annotations.attribute.SingularAttributeSourceImpl;
import org.hibernate.metamodel.source.binder.DiscriminatorSource; import org.hibernate.metamodel.source.binder.DiscriminatorSource;

View File

@ -0,0 +1,63 @@
package org.hibernate.metamodel.source.annotations.entity;
import java.util.Iterator;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.SecondaryTable;
import org.junit.Test;
import org.hibernate.AssertionFailure;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.relational.SimpleValue;
import org.hibernate.metamodel.relational.Table;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
/**
* @author Hardy Ferentschik
*/
public class IdentifierGeneratorTest extends BaseAnnotationBindingTestCase {
@Entity
@SecondaryTable(name = "SECOND_TABLE")
class EntityWithSecondaryTable {
@Id
private long id;
@Column(table = "SECOND_TABLE")
private String name;
}
@Test
@Resources(annotatedClasses = EntityWithSecondaryTable.class)
public void testSecondaryTableExists() {
EntityBinding binding = getEntityBinding( EntityWithSecondaryTable.class );
Table table = (Table) binding.getTable( "SECOND_TABLE" );
assertEquals( "The secondary table should exist", "SECOND_TABLE", table.getTableName().getName() );
Iterator<SimpleValue> valueIterator = table.values().iterator();
assertTrue( valueIterator.hasNext() );
org.hibernate.metamodel.relational.Column column = (org.hibernate.metamodel.relational.Column) valueIterator.next();
assertEquals( "Wrong column name", "name", column.getColumnName().getName() );
assertFalse( valueIterator.hasNext() );
}
@Test
@Resources(annotatedClasses = EntityWithSecondaryTable.class)
public void testRetrievingUnknownTable() {
EntityBinding binding = getEntityBinding( EntityWithSecondaryTable.class );
try {
binding.getTable( "FOO" );
fail();
}
catch ( AssertionFailure e ) {
assertTrue( e.getMessage().startsWith( "Unable to find table" ) );
}
}
}