package org.hibernate.metamodel.binding;
import java.util.Properties;
import org.hibernate.MappingException;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.domain.SingularAttribute;
import org.hibernate.metamodel.relational.Column;
import org.hibernate.metamodel.relational.Schema;
import org.hibernate.metamodel.relational.SimpleValue;
import org.hibernate.metamodel.source.MetaAttributeContext;
public class BasicAttributeBinding
extends AbstractSingularAttributeBinding
implements KeyValueBinding {
private String unsavedValue;
private PropertyGeneration generation;
private boolean includedInOptimisticLocking;
private boolean forceNonNullable;
private boolean forceUnique;
private boolean keyCascadeDeleteEnabled;
private MetaAttributeContext metaAttributeContext;
BasicAttributeBinding(
AttributeBindingContainer container,
SingularAttribute attribute,
boolean forceNonNullable,
boolean forceUnique) {
super( container, attribute );
this.forceNonNullable = forceNonNullable;
this.forceUnique = forceUnique;
}
@Override
public boolean isAssociation() {
return false;
}
@Override
public String getUnsavedValue() {
return unsavedValue;
}
public void setUnsavedValue(String unsavedValue) {
this.unsavedValue = unsavedValue;
}
@Override
public PropertyGeneration getGeneration() {
return generation;
}
public void setGeneration(PropertyGeneration generation) {
this.generation = generation;
}
public boolean isIncludedInOptimisticLocking() {
return includedInOptimisticLocking;
}
public void setIncludedInOptimisticLocking(boolean includedInOptimisticLocking) {
this.includedInOptimisticLocking = includedInOptimisticLocking;
}
@Override
public boolean isKeyCascadeDeleteEnabled() {
return keyCascadeDeleteEnabled;
}
public void setKeyCascadeDeleteEnabled(boolean keyCascadeDeleteEnabled) {
this.keyCascadeDeleteEnabled = keyCascadeDeleteEnabled;
}
public boolean forceNonNullable() {
return forceNonNullable;
}
public boolean forceUnique() {
return forceUnique;
}
public MetaAttributeContext getMetaAttributeContext() {
return metaAttributeContext;
}
public void setMetaAttributeContext(MetaAttributeContext metaAttributeContext) {
this.metaAttributeContext = metaAttributeContext;
}
IdentifierGenerator createIdentifierGenerator(
IdGenerator idGenerator,
IdentifierGeneratorFactory identifierGeneratorFactory,
Properties properties) {
Properties params = new Properties();
params.putAll( properties );
Schema schema = getValue().getTable().getSchema();
if ( schema != null ) {
if ( schema.getName().getSchema() != null ) {
params.setProperty( PersistentIdentifierGenerator.SCHEMA, schema.getName().getSchema().getName() );
}
if ( schema.getName().getCatalog() != null ) {
params.setProperty( PersistentIdentifierGenerator.CATALOG, schema.getName().getCatalog().getName() );
}
}
params.setProperty( IdentifierGenerator.ENTITY_NAME, getContainer().seekEntityBinding().getEntity().getName() );
String tableName = getValue().getTable().getQualifiedName( identifierGeneratorFactory.getDialect() );
params.setProperty( PersistentIdentifierGenerator.TABLE, tableName );
if ( getSimpleValueSpan() > 1 ) {
throw new MappingException(
"A SimpleAttributeBinding used for an identifier has more than 1 Value: " + getAttribute().getName()
);
}
SimpleValue simpleValue = (SimpleValue) getValue();
if ( !Column.class.isInstance( simpleValue ) ) {
throw new MappingException(
"Cannot create an IdentifierGenerator because the value is not a column: " +
simpleValue.toLoggableString()
);
}
params.setProperty(
PersistentIdentifierGenerator.PK,
( (Column) simpleValue ).getColumnName().encloseInQuotesIfQuoted(
identifierGeneratorFactory.getDialect()
)
);
params.setProperty( PersistentIdentifierGenerator.TABLES, tableName );
params.putAll( idGenerator.getParameters() );
return identifierGeneratorFactory.createIdentifierGenerator(
idGenerator.getStrategy(), getHibernateTypeDescriptor().getResolvedTypeMapping(), params
);
}
}