org.hibernate.mapping.Formula Java Examples

The following examples show how to use org.hibernate.mapping.Formula. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Ejb3Column.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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() );
		}
	}
}
 
Example #2
Source File: Ejb3Column.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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
	);
}
 
Example #3
Source File: RelationalObjectBinder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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() ) );
		}
	}
}
 
Example #4
Source File: ComponentTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 ) );
	}
}
 
Example #5
Source File: Ejb3Column.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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 );
		}
	}
}
 
Example #6
Source File: RelationalObjectBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void bindFormulas(
		MappingDocument sourceDocument,
		List<DerivedValueSource> formulaSources,
		OneToOne oneToOneBinding) {
	for ( DerivedValueSource formulaSource : formulaSources ) {
		oneToOneBinding.addFormula( new Formula( formulaSource.getExpression() ) );
	}
}
 
Example #7
Source File: HbmBinder.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 );
	}
}
 
Example #8
Source File: CompositeElementTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 ) );
	}
}
 
Example #9
Source File: HibernateUtil.java    From unitime with Apache License 2.0 5 votes vote down vote up
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());
                }
            }
        }
    }
}