下面列出了怎么用org.hibernate.mapping.Formula的API类实例代码及写法,或者点击链接到github查看源代码。
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() );
}
}
}
public static Ejb3Column[] buildColumnFromAnnotation(
javax.persistence.Column[] anns,
org.hibernate.annotations.Formula formulaAnn,
Nullability nullability,
PropertyHolder propertyHolder,
PropertyData inferredData,
Map<String, Join> secondaryTables,
MetadataBuildingContext context) {
return buildColumnFromAnnotation(
anns,
formulaAnn,
nullability,
propertyHolder,
inferredData,
null,
secondaryTables,
context
);
}
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() ) );
}
}
}
public void afterConfigurationBuilt(Mappings mappings, Dialect dialect) {
super.afterConfigurationBuilt( mappings, dialect );
// 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 foprmulas which can use dialect-registered functions...
PersistentClass user = mappings.getClass( 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 = ( SQLFunction ) dialect.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( args, null ) );
}
}
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 );
}
}
}
public void bindFormulas(
MappingDocument sourceDocument,
List<DerivedValueSource> formulaSources,
OneToOne oneToOneBinding) {
for ( DerivedValueSource formulaSource : formulaSources ) {
oneToOneBinding.addFormula( new Formula( formulaSource.getExpression() ) );
}
}
private static void bindColumnsOrFormula(Element node, SimpleValue simpleValue, String path,
boolean isNullable, Mappings mappings) {
Attribute formulaNode = node.attribute( "formula" );
if ( formulaNode != null ) {
Formula f = new Formula();
f.setFormula( formulaNode.getText() );
simpleValue.addFormula( f );
}
else {
bindColumns( node, simpleValue, isNullable, true, path, mappings );
}
}
public void afterConfigurationBuilt(Mappings mappings, Dialect dialect) {
super.afterConfigurationBuilt( mappings, dialect );
Collection children = mappings.getCollection( Parent.class.getName() + ".children" );
Component childComponents = ( Component ) children.getElement();
Formula f = ( Formula ) childComponents.getProperty( "bioLength" ).getValue().getColumnIterator().next();
SQLFunction lengthFunction = ( SQLFunction ) dialect.getFunctions().get( "length" );
if ( lengthFunction != null ) {
ArrayList args = new ArrayList();
args.add( "bio" );
f.setFormula( lengthFunction.render( args, null ) );
}
}
public static void fixSchemaInFormulas(Configuration cfg) throws ClassNotFoundException {
cfg.buildMappings();
Class dialect = Class.forName(cfg.getProperty("dialect"));
String schema = cfg.getProperty("default_schema");
for (Iterator i=cfg.getClassMappings();i.hasNext();) {
PersistentClass pc = (PersistentClass)i.next();
for (Iterator j=pc.getPropertyIterator();j.hasNext();) {
Property p = (Property)j.next();
for (Iterator k=p.getColumnIterator();k.hasNext();) {
Selectable c = (Selectable)k.next();
if (c instanceof Formula) {
Formula f = (Formula)c;
boolean updated = false;
if (schema != null && f.getFormula() != null && f.getFormula().indexOf("%SCHEMA%")>=0) {
f.setFormula(f.getFormula().replaceAll("%SCHEMA%", schema));
sLog.debug("Schema updated in "+pc.getClassName()+"."+p.getName()+" to "+f.getFormula());
}
if (f.getFormula()!=null && (f.getFormula().indexOf("%TRUE%")>=0 || f.getFormula().indexOf("%FALSE%")>=0)) {
if (isPostgress(dialect)) {
f.setFormula(f.getFormula().replaceAll("%TRUE%", "'t'").replaceAll("%FALSE%", "'f'"));
} else {
f.setFormula(f.getFormula().replaceAll("%TRUE%", "1").replaceAll("%FALSE%", "0"));
}
}
if (updated)
sLog.debug("Schema updated in "+pc.getClassName()+"."+p.getName()+" to "+f.getFormula());
}
}
}
}
}