use of org.hibernate.mapping.Formula in project hibernate-orm by hibernate.
the class RelationalObjectBinder method bindColumnsAndFormulas.
public void bindColumnsAndFormulas(MappingDocument sourceDocument, List<RelationalValueSource> relationalValueSources, SimpleValue simpleValue, boolean areColumnsNullableByDefault, ColumnNamingDelegate columnNamingDelegate) {
for (RelationalValueSource relationalValueSource : relationalValueSources) {
if (ColumnSource.class.isInstance(relationalValueSource)) {
final ColumnSource columnSource = (ColumnSource) relationalValueSource;
bindColumn(sourceDocument, columnSource, simpleValue, areColumnsNullableByDefault, columnNamingDelegate);
} else {
final DerivedValueSource formulaSource = (DerivedValueSource) relationalValueSource;
simpleValue.addFormula(new Formula(formulaSource.getExpression()));
}
}
}
use of org.hibernate.mapping.Formula in project hibernate-orm by hibernate.
the class Ejb3Column method bind.
public void bind() {
if (StringHelper.isNotEmpty(formulaString)) {
LOG.debugf("Binding formula %s", formulaString);
formula = new Formula();
formula.setFormula(formulaString);
} else {
initMappingColumn(logicalColumnName, propertyName, length, precision, scale, nullable, sqlType, unique, true);
if (defaultValue != null) {
mappingColumn.setDefaultValue(defaultValue);
}
if (LOG.isDebugEnabled()) {
LOG.debugf("Binding column: %s", toString());
}
}
}
use of org.hibernate.mapping.Formula in project hibernate-orm by hibernate.
the class CompositeElementTest method afterMetadataBuilt.
@Override
protected void afterMetadataBuilt(Metadata metadata) {
Collection children = metadata.getCollectionBinding(Parent.class.getName() + ".children");
Component childComponents = (Component) children.getElement();
Formula f = (Formula) childComponents.getProperty("bioLength").getValue().getColumnIterator().next();
SQLFunction lengthFunction = metadata.getDatabase().getJdbcEnvironment().getDialect().getFunctions().get("length");
if (lengthFunction != null) {
ArrayList args = new ArrayList();
args.add("bio");
f.setFormula(lengthFunction.render(StandardBasicTypes.INTEGER, args, null));
}
}
use of org.hibernate.mapping.Formula in project hibernate-orm by hibernate.
the class ComponentTest method afterMetadataBuilt.
@Override
protected void afterMetadataBuilt(Metadata metadata) {
// Oracle and Postgres do not have year() functions, so we need to
// redefine the 'User.person.yob' formula
//
// consider temporary until we add the capability to define
// mapping formulas which can use dialect-registered functions...
PersistentClass user = metadata.getEntityBinding(User.class.getName());
org.hibernate.mapping.Property personProperty = user.getProperty("person");
Component component = (Component) personProperty.getValue();
Formula f = (Formula) component.getProperty("yob").getValue().getColumnIterator().next();
SQLFunction yearFunction = metadata.getDatabase().getJdbcEnvironment().getDialect().getFunctions().get("year");
if (yearFunction == null) {
// the dialect not know to support a year() function, so rely on the
// ANSI SQL extract function
f.setFormula("extract( year from dob )");
} else {
List args = new ArrayList();
args.add("dob");
f.setFormula(yearFunction.render(StandardBasicTypes.INTEGER, args, null));
}
}
use of org.hibernate.mapping.Formula in project hibernate-orm by hibernate.
the class Ejb3Column method initMappingColumn.
protected void initMappingColumn(String columnName, String propertyName, int length, int precision, int scale, boolean nullable, String sqlType, boolean unique, boolean applyNamingStrategy) {
if (StringHelper.isNotEmpty(formulaString)) {
this.formula = new Formula();
this.formula.setFormula(formulaString);
} else {
this.mappingColumn = new Column();
redefineColumnName(columnName, propertyName, applyNamingStrategy);
this.mappingColumn.setLength(length);
if (precision > 0) {
// revelent precision
this.mappingColumn.setPrecision(precision);
this.mappingColumn.setScale(scale);
}
this.mappingColumn.setNullable(nullable);
this.mappingColumn.setSqlType(sqlType);
this.mappingColumn.setUnique(unique);
if (writeExpression != null && !writeExpression.matches("[^?]*\\?[^?]*")) {
throw new AnnotationException("@WriteExpression must contain exactly one value placeholder ('?') character: property [" + propertyName + "] and column [" + logicalColumnName + "]");
}
if (readExpression != null) {
this.mappingColumn.setCustomRead(readExpression);
}
if (writeExpression != null) {
this.mappingColumn.setCustomWrite(writeExpression);
}
}
}
Aggregations