Example usage for org.hibernate.mapping Column DEFAULT_SCALE

List of usage examples for org.hibernate.mapping Column DEFAULT_SCALE

Introduction

In this page you can find the example usage for org.hibernate.mapping Column DEFAULT_SCALE.

Prototype

int DEFAULT_SCALE

To view the source code for org.hibernate.mapping Column DEFAULT_SCALE.

Click Source Link

Usage

From source file:com.amalto.core.storage.hibernate.LiquibaseSchemaAdapter.java

License:Open Source License

private String getColumnType(FieldMetadata current, String columnDataType) {
    int hibernateTypeCode = 0;
    TypeMetadata type = MetadataUtils.getSuperConcreteType(current.getType());

    Object currentLength = CommonUtil.getSuperTypeMaxLength(current.getType(), current.getType());
    Object currentTotalDigits = current.getType().getData(MetadataRepository.DATA_TOTAL_DIGITS);
    Object currentFractionDigits = current.getType().getData(MetadataRepository.DATA_FRACTION_DIGITS);

    int length = currentLength == null ? Column.DEFAULT_LENGTH : Integer.parseInt(currentLength.toString());
    int precision = currentTotalDigits == null ? Column.DEFAULT_PRECISION
            : Integer.parseInt(currentTotalDigits.toString());
    int scale = currentFractionDigits == null ? Column.DEFAULT_SCALE
            : Integer.parseInt(currentFractionDigits.toString());

    if (type.getName().equals("string")) { //$NON-NLS-1$
        hibernateTypeCode = java.sql.Types.VARCHAR;
    } else if (type.getName().equals("int") || type.getName().equals("short") //$NON-NLS-1$ //$NON-NLS-2$
            || type.getName().equals("long") || type.getName().equals("integer")) { //$NON-NLS-1$ //$NON-NLS-2$
        hibernateTypeCode = java.sql.Types.INTEGER;
    } else if (type.getName().equals("boolean")) { //$NON-NLS-1$
        hibernateTypeCode = java.sql.Types.BOOLEAN;
    } else if (type.getName().equals("date") || type.getName().equals("datetime")) { //$NON-NLS-1$ //$NON-NLS-2$
        hibernateTypeCode = java.sql.Types.TIMESTAMP;
    } else if (type.getName().equals("double") || type.getName().equals("float")) { //$NON-NLS-1$ //$NON-NLS-2$
        hibernateTypeCode = java.sql.Types.DOUBLE;
    } else if (type.getName().equals("decimal")) {
        hibernateTypeCode = java.sql.Types.NUMERIC;
    }/*from w w w .  ja va2s . com*/
    columnDataType = dialect.getTypeName(hibernateTypeCode, length, precision, scale);

    return columnDataType;
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

private String[] getColumnTypeForPropertyType(EntityType<?> entityType, String attributeName,
        SessionFactoryImplementor sfi, Type propertyType) {
    if (propertyType instanceof org.hibernate.type.EntityType) {
        propertyType = ((org.hibernate.type.EntityType) propertyType).getIdentifierOrUniqueKeyType(sfi);
    }//  w  w  w  . j ava2  s .co  m

    long length = Column.DEFAULT_LENGTH;
    int precision = Column.DEFAULT_PRECISION;
    int scale = Column.DEFAULT_SCALE;

    try {
        Method m = Type.class.getMethod("dictatedSizes", Mapping.class);
        Object size = ((Object[]) m.invoke(propertyType, sfi))[0];
        length = (long) size.getClass().getMethod("getLength").invoke(size);
        precision = (int) size.getClass().getMethod("getPrecision").invoke(size);
        scale = (int) size.getClass().getMethod("getScale").invoke(size);
    } catch (Exception ex) {
        LOG.fine("Could not determine the column type of the attribute: " + attributeName + " of the entity: "
                + entityType.getName());
    }

    return new String[] {
            sfi.getDialect().getTypeName(propertyType.sqlTypes(sfi)[0], length, precision, scale) };
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

/**
 * Interrogates the specified constraints looking for any constraints that would limit the
 * precision and/or scale of the property's value.  If such constraints exist, this method adjusts
 * the precision and/or scale of the column accordingly.
 *
 * @param column              the column that corresponds to the property
 * @param constrainedProperty the property's constraints
 * @param cc the column configuration/*from  w w  w .  ja v a  2 s  .  com*/
 */
protected void bindNumericColumnConstraints(Column column, ConstrainedProperty constrainedProperty,
        ColumnConfig cc) {
    int scale = Column.DEFAULT_SCALE;
    int precision = Column.DEFAULT_PRECISION;

    if (cc != null && cc.getScale() > -1) {
        column.setScale(cc.getScale());
    } else if (constrainedProperty.getScale() != null) {
        scale = constrainedProperty.getScale().intValue();
        column.setScale(scale);
    }

    if (cc != null && cc.getPrecision() > -1) {
        column.setPrecision(cc.getPrecision());
    } else {

        Comparable<?> minConstraintValue = constrainedProperty.getMin();
        Comparable<?> maxConstraintValue = constrainedProperty.getMax();

        int minConstraintValueLength = 0;
        if ((minConstraintValue != null) && (minConstraintValue instanceof Number)) {
            minConstraintValueLength = Math.max(countDigits((Number) minConstraintValue),
                    countDigits(((Number) minConstraintValue).longValue()) + scale);
        }
        int maxConstraintValueLength = 0;
        if ((maxConstraintValue != null) && (maxConstraintValue instanceof Number)) {
            maxConstraintValueLength = Math.max(countDigits((Number) maxConstraintValue),
                    countDigits(((Number) maxConstraintValue).longValue()) + scale);
        }

        if (minConstraintValueLength > 0 && maxConstraintValueLength > 0) {
            // If both of min and max constraints are setted we could use
            // maximum digits number in it as precision
            precision = Math.max(minConstraintValueLength, maxConstraintValueLength);
        } else {
            // Overwise we should also use default precision
            precision = DefaultGroovyMethods
                    .max(new Integer[] { precision, minConstraintValueLength, maxConstraintValueLength });
        }

        column.setPrecision(precision);
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

/**
 * Interrogates the specified constraints looking for any constraints that would limit the
 * precision and/or scale of the property's value.  If such constraints exist, this method adjusts
 * the precision and/or scale of the column accordingly.
 *
 * @param column              the column that corresponds to the property
 * @param constrainedProperty the property's constraints
 *///  w  w  w  .j  a  va2 s  .  c o m
protected static void bindNumericColumnConstraints(Column column, ConstrainedProperty constrainedProperty) {
    int scale = Column.DEFAULT_SCALE;
    int precision = Column.DEFAULT_PRECISION;

    if (constrainedProperty.getScale() != null) {
        scale = constrainedProperty.getScale().intValue();
        column.setScale(scale);
    }

    Comparable<?> minConstraintValue = constrainedProperty.getMin();
    Comparable<?> maxConstraintValue = constrainedProperty.getMax();

    int minConstraintValueLength = 0;
    if ((minConstraintValue != null) && (minConstraintValue instanceof Number)) {
        minConstraintValueLength = Math.max(countDigits((Number) minConstraintValue),
                countDigits(new Long(((Number) minConstraintValue).longValue())) + scale);
    }
    int maxConstraintValueLength = 0;
    if ((maxConstraintValue != null) && (maxConstraintValue instanceof Number)) {
        maxConstraintValueLength = Math.max(countDigits((Number) maxConstraintValue),
                countDigits(new Long(((Number) maxConstraintValue).longValue())) + scale);
    }

    if (minConstraintValueLength > 0 && maxConstraintValueLength > 0) {
        // If both of min and max constraints are setted we could use
        // maximum digits number in it as precision
        precision = NumberUtils.max(new int[] { minConstraintValueLength, maxConstraintValueLength });
    } else {
        // Overwise we should also use default precision
        precision = NumberUtils
                .max(new int[] { precision, minConstraintValueLength, maxConstraintValueLength });
    }

    column.setPrecision(precision);
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinderTests.java

License:Apache License

/**
 * @see GrailsDomainBinder#bindNumericColumnConstraints(Column, ConstrainedProperty)
 */// w w w.j  a va  2s.  c om
public void testBindNumericColumnConstraints() {
    ConstrainedProperty constrainedProperty = getConstrainedBigDecimalProperty();
    // maxSize and minSize constraint has the number with the most digits
    constrainedProperty.applyConstraint(ConstrainedProperty.MAX_SIZE_CONSTRAINT, new Integer(123));
    constrainedProperty.applyConstraint(ConstrainedProperty.MIN_SIZE_CONSTRAINT, new Integer(0));
    assertColumnPrecisionAndScale(constrainedProperty, Column.DEFAULT_PRECISION, Column.DEFAULT_SCALE);

    // Verify that the correct precision is set when the max constraint has the number with the most digits
    constrainedProperty = getConstrainedBigDecimalProperty();
    constrainedProperty.applyConstraint(ConstrainedProperty.MAX_CONSTRAINT, new BigDecimal("123.45"));
    constrainedProperty.applyConstraint(ConstrainedProperty.MIN_CONSTRAINT, new BigDecimal("0"));
    assertColumnPrecisionAndScale(constrainedProperty, 5, Column.DEFAULT_SCALE);

    // Verify that the correct precision is set when the minSize constraint has the number with the most digits
    constrainedProperty = getConstrainedBigDecimalProperty();
    constrainedProperty.applyConstraint(ConstrainedProperty.MAX_CONSTRAINT, new BigDecimal("123"));
    constrainedProperty.applyConstraint(ConstrainedProperty.MIN_CONSTRAINT, new BigDecimal("-123.45"));
    assertColumnPrecisionAndScale(constrainedProperty, 5, Column.DEFAULT_SCALE);

    // Verify that the correct precision is set when the high value of a floating point range constraint has the number with the most digits
    constrainedProperty = getConstrainedBigDecimalProperty();
    constrainedProperty.applyConstraint(ConstrainedProperty.RANGE_CONSTRAINT,
            new ObjectRange(new BigDecimal("0"), new BigDecimal("123.45")));
    assertColumnPrecisionAndScale(constrainedProperty, 5, Column.DEFAULT_SCALE);

    // Verify that the correct precision is set when the low value of a floating point range constraint has the number with the most digits
    constrainedProperty = getConstrainedBigDecimalProperty();
    constrainedProperty.applyConstraint(ConstrainedProperty.RANGE_CONSTRAINT,
            new ObjectRange(new BigDecimal("-123.45"), new BigDecimal("123")));
    assertColumnPrecisionAndScale(constrainedProperty, 5, Column.DEFAULT_SCALE);

    // Verify that the correct scale is set when the scale constraint is specified in isolation
    constrainedProperty = getConstrainedBigDecimalProperty();
    constrainedProperty.applyConstraint(ConstrainedProperty.SCALE_CONSTRAINT, new Integer(4));
    assertColumnPrecisionAndScale(constrainedProperty, Column.DEFAULT_PRECISION, 4);

    // Verify that the precision is set correctly for a floating point number with a min/max constraint and a scale...
    //  1) where the min/max constraint includes fewer decimal places than the scale constraint
    constrainedProperty = getConstrainedBigDecimalProperty();
    constrainedProperty.applyConstraint(ConstrainedProperty.MAX_CONSTRAINT, new BigDecimal("123.45"));
    constrainedProperty.applyConstraint(ConstrainedProperty.MIN_CONSTRAINT, new BigDecimal("0"));
    constrainedProperty.applyConstraint(ConstrainedProperty.SCALE_CONSTRAINT, new Integer(3));
    assertColumnPrecisionAndScale(constrainedProperty, 6, 3); // precision (6) = number of integer digits in max constraint ("123.45") + scale (3)

    //  2) where the min/max constraint includes more decimal places than the scale constraint
    constrainedProperty = getConstrainedBigDecimalProperty();
    constrainedProperty.applyConstraint(ConstrainedProperty.MAX_CONSTRAINT, new BigDecimal("123.4567"));
    constrainedProperty.applyConstraint(ConstrainedProperty.MIN_CONSTRAINT, new BigDecimal("0"));
    constrainedProperty.applyConstraint(ConstrainedProperty.SCALE_CONSTRAINT, new Integer(3));
    assertColumnPrecisionAndScale(constrainedProperty, 7, 3); // precision (7) = number of digits in max constraint ("123.4567") 

    // Verify that the correct precision is set when the only one of 'min' and 'max' constraint specified
    constrainedProperty = getConstrainedBigDecimalProperty();
    constrainedProperty.applyConstraint(ConstrainedProperty.MAX_CONSTRAINT, new BigDecimal("123.4567"));
    assertColumnPrecisionAndScale(constrainedProperty, Column.DEFAULT_PRECISION, Column.DEFAULT_SCALE);
    constrainedProperty = getConstrainedBigDecimalProperty();
    constrainedProperty.applyConstraint(ConstrainedProperty.MAX_CONSTRAINT,
            new BigDecimal("12345678901234567890.4567"));
    assertColumnPrecisionAndScale(constrainedProperty, 24, Column.DEFAULT_SCALE);
    constrainedProperty = getConstrainedBigDecimalProperty();
    constrainedProperty.applyConstraint(ConstrainedProperty.MIN_CONSTRAINT, new BigDecimal("-123.4567"));
    assertColumnPrecisionAndScale(constrainedProperty, Column.DEFAULT_PRECISION, Column.DEFAULT_SCALE);
    constrainedProperty = getConstrainedBigDecimalProperty();
    constrainedProperty.applyConstraint(ConstrainedProperty.MIN_CONSTRAINT,
            new BigDecimal("-12345678901234567890.4567"));
    assertColumnPrecisionAndScale(constrainedProperty, 24, Column.DEFAULT_SCALE);
}

From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

/**
 * Interrogates the specified constraints looking for any constraints that would limit the
 * precision and/or scale of the property's value.  If such constraints exist, this method adjusts
 * the precision and/or scale of the column accordingly.
 *  @param column              the column that corresponds to the property
 * @param property the property's constraints
 * @param cc the column configuration/*  www.  j av a 2  s  . c  o  m*/
 */
protected void bindNumericColumnConstraints(Column column, PersistentProperty property, ColumnConfig cc) {
    int scale = Column.DEFAULT_SCALE;
    int precision = Column.DEFAULT_PRECISION;

    PropertyConfig constrainedProperty = (PropertyConfig) property.getMapping().getMappedForm();
    if (cc != null && cc.getScale() > -1) {
        column.setScale(cc.getScale());
    } else if (constrainedProperty.getScale() > -1) {
        scale = constrainedProperty.getScale();
        column.setScale(scale);
    }

    if (cc != null && cc.getPrecision() > -1) {
        column.setPrecision(cc.getPrecision());
    } else {

        Comparable<?> minConstraintValue = constrainedProperty.getMin();
        Comparable<?> maxConstraintValue = constrainedProperty.getMax();

        int minConstraintValueLength = 0;
        if ((minConstraintValue != null) && (minConstraintValue instanceof Number)) {
            minConstraintValueLength = Math.max(countDigits((Number) minConstraintValue),
                    countDigits(((Number) minConstraintValue).longValue()) + scale);
        }
        int maxConstraintValueLength = 0;
        if ((maxConstraintValue != null) && (maxConstraintValue instanceof Number)) {
            maxConstraintValueLength = Math.max(countDigits((Number) maxConstraintValue),
                    countDigits(((Number) maxConstraintValue).longValue()) + scale);
        }

        if (minConstraintValueLength > 0 && maxConstraintValueLength > 0) {
            // If both of min and max constraints are setted we could use
            // maximum digits number in it as precision
            precision = Math.max(minConstraintValueLength, maxConstraintValueLength);
        } else {
            // Overwise we should also use default precision
            precision = DefaultGroovyMethods
                    .max(new Integer[] { precision, minConstraintValueLength, maxConstraintValueLength });
        }

        column.setPrecision(precision);
    }
}