Example usage for org.hibernate.mapping Column getLength

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

Introduction

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

Prototype

public int getLength() 

Source Link

Usage

From source file:com.ah.ui.actions.BaseAction.java

public int getAttributeLength(String name) {
    /*- hibernate annotation doesn't work after hibernate upgraded to 3.5.x, use JPA annotation instead
    try {//from  w w  w .ja va2s . co m
       Field field = boClass.getDeclaredField(name);
       Length length = field.getAnnotation(Length.class);
       if (length != null) {
    log.debug("getAttributeLength", "Attribute '" + name
          + "' length: " + length.max());
    return length.max();
       }
    } catch (NoSuchFieldException e) {
       log.error("getAttributeLength", "Attribute '" + name
       + "' does not exist in class '" + boClass.getName() + "'.");
    }*/
    if (boPersistentClass == null) {
        log.error("getAttributeLength", "boPersistentClass has not been initialized.");
        return 0;
    }
    Property property = boPersistentClass.getProperty(name);
    if (property == null) {
        log.error("getAttributeLength",
                "Attribute '" + name + "' does not exist in class '" + boClass.getName() + "'.");
        return 0;
    }
    if (property.getColumnIterator().hasNext()) {
        Column column = (Column) property.getColumnIterator().next();
        log.debug("getAttributeLength", "Attribute '" + name + "' length is: " + column.getLength());
        return column.getLength();
    } else {
        log.error("getAttributeLength",
                "Attribute '" + name + "' in class '" + boClass.getName() + "' does not map to a column.");
        return 0;
    }
}

From source file:com.amalto.core.storage.hibernate.mapping.MDMTableUtils.java

License:Open Source License

public static boolean isIncreaseVarcharColumnLength(Column newColumn, ColumnMetadata oldColumnInfo,
        Dialect dialect) {//  w  w  w .j a v  a  2  s. co  m
    return newColumn.getLength() > oldColumnInfo.getColumnSize();
}

From source file:com.manydesigns.portofino.persistence.hibernate.HibernateConfig.java

License:Open Source License

protected Column createColumn(Mappings mappings, Table tab,
        com.manydesigns.portofino.model.database.Column column) {
    Column col = new Column();
    col.setName(escapeName(column.getColumnName()));
    col.setLength(column.getLength());
    col.setPrecision(column.getLength());
    col.setScale(column.getScale());/*w  w w  .ja va  2s  .  c  om*/
    col.setNullable(column.isNullable());
    String columnType = column.getColumnType();
    int jdbcType = column.getJdbcType();

    col.setSqlTypeCode(jdbcType);
    col.setSqlType(columnType);

    SimpleValue value = new SimpleValue(mappings, tab);
    if (!setHibernateType(value, column, column.getActualJavaType(), jdbcType)) {
        logger.error("Skipping column {}", column.getQualifiedName());
        return null;
    }

    value.addColumn(col);
    tab.addColumn(col);
    mappings.addColumnBinding(column.getColumnName(), col, tab);

    return col;
}

From source file:de.xwic.sandbox.server.installer.modules.UpdateColumnSizes.java

License:Apache License

public void run(InstallationManager manager) throws Exception {

    IDatabaseHandler dbHandler = manager.getDatabaseHandler();

    Configuration cfg = HibernateUtil.getConfiguration();

    for (Iterator<?> it = cfg.getTableMappings(); it.hasNext();) {

        Table table = (Table) it.next();
        List<DbColumn> list = dbHandler.getTableColumns(table.getName());
        // put it into a map for easier access
        Map<String, DbColumn> map = new HashMap<String, DbColumn>();
        for (Iterator<DbColumn> itL = list.iterator(); itL.hasNext();) {
            DbColumn col = itL.next();//  w  w  w .j a v  a 2 s .co m
            map.put(col.getName(), col);
        }

        for (Iterator<?> itC = table.getColumnIterator(); itC.hasNext();) {
            Column column = (Column) itC.next();
            DbColumn dbCol = map.get(column.getName());
            if (dbCol != null && "varchar".equalsIgnoreCase(dbCol.getDataType())
                    && dbCol.getLength() != column.getLength()) {
                if (dbCol.getLength() > column.getLength()) {
                    log.warn("The column '" + column.getName() + "' in table '" + table.getName()
                            + "' is larger (" + dbCol.getLength() + ") then specified (" + column.getLength()
                            + ") in the mapping. The column size is not modified. ");
                } else {
                    log.info("Changing column size for '" + table.getName() + "." + column.getName() + "' from "
                            + dbCol.getLength() + " to " + column.getLength());
                    dbHandler.alterColumn(table.getName(), dbCol.getName(),
                            "VARCHAR(" + column.getLength() + ")");
                }
            }
        }

    }

}

From source file:edu.wustl.common.util.dbManager.HibernateMetaData.java

License:BSD License

/**
 * @param classObj Name of the class./*from  www  .  ja va  2s .co  m*/
 * @param attributeName Name of the attribute.
 * @return The width of the column. Returns width of the column or zero.
 */
public static int getColumnWidth(Class classObj, String attributeName) {
    Iterator it = cfg.getClassMapping(classObj.getName()).getPropertyClosureIterator();
    while (it.hasNext()) {
        Property property = (Property) it.next();

        if (property != null && property.getName().equals(attributeName)) {
            Iterator colIt = property.getColumnIterator();
            while (colIt.hasNext()) {
                Column col = (Column) colIt.next();
                return col.getLength();
            }
        }
    }
    // if attribute is not found than the default width will be 50.
    return 50;
}

From source file:edu.wustl.dao.util.HibernateMetaData.java

License:BSD License

/**
 * This method will be called to obtained column width of attribute field of given class.
 * @param classObj Name of the class./*w ww  .  jav  a2  s.co m*/
 * @param attributeName Name of the attribute.
 * @return The width of the column. Returns width of the column or zero.
 */
public static int getColumnWidth(Class classObj, String attributeName) {
    Iterator iterator = cfg.getClassMapping(classObj.getName()).getPropertyClosureIterator();
    int colLength = 50;
    while (iterator.hasNext()) {
        Property property = (Property) iterator.next();

        if (property != null && property.getName().equals(attributeName)) {
            Iterator colIt = property.getColumnIterator();
            while (colIt.hasNext()) {
                Column col = (Column) colIt.next();
                colLength = col.getLength();
            }
        }
    }
    // if attribute is not found than the default width will be 50.
    return colLength;
}

From source file:net.lshift.hibernate.migrations.SQLStringHelpers.java

License:Apache License

/**
 * Note to the unassuming maintenance developer:
 *
 * What happens with Oracle a dialect is when a varchar2 is larger than 4000,
 * the type name returned for java.sql.Type.VARCHAR is a long, which can be very confusing.
 *
 * I'm not sure that this is significant enough to warrant patching.
 *
 */// w  ww .  j a  v a  2 s  .c om
private static String getTypeName(Dialect dialect, Column col) {
    return dialect.getTypeName(col.getSqlTypeCode(), col.getLength(), col.getPrecision(), col.getScale());
}

From source file:org.beangle.orm.hibernate.tool.HbmGenerator.java

License:Open Source License

@SuppressWarnings("unchecked")
public void gen(String file) throws Exception {
    hbconfig = new OverrideConfiguration();
    hbconfig.getProperties().put(Environment.DIALECT, new Oracle10gDialect());
    ConfigBuilder.build(hbconfig);/*from w  ww .  ja  va2  s .  c om*/
    freemarkerConfig = new freemarker.template.Configuration();
    freemarkerConfig.setTemplateLoader(new ClassTemplateLoader(getClass(), "/"));

    Iterator<PersistentClass> iter = hbconfig.getClassMappings();
    List<PersistentClass> pcs = CollectUtils.newArrayList();
    while (iter.hasNext()) {
        PersistentClass pc = iter.next();
        Class<?> cls = pc.getMappedClass();
        Iterator<Property> pi = pc.getPropertyIterator();
        // For AnnotationBinder don't set column'length and nullable in ,let's we do it.
        while (pi.hasNext()) {
            Property p = pi.next();
            if (p.getColumnSpan() != 1)
                continue;
            Column column = (Column) p.getColumnIterator().next();
            if (column.getLength() == Column.DEFAULT_LENGTH) {
                Size size = findAnnotation(cls, Size.class, p.getName());
                if (null != size)
                    column.setLength(size.max());
            }
            if (column.isNullable()) {
                NotNull notnull = findAnnotation(cls, NotNull.class, p.getName());
                if (null != notnull)
                    column.setNullable(false);
            }
        }
        if (!pc.getClassName().contains(".example."))
            pcs.add(pc);
    }
    Map<String, Object> data = CollectUtils.newHashMap();
    data.put("classes", pcs);
    data.put("generator", this);
    Template freemarkerTemplate = freemarkerConfig.getTemplate("/hbm.ftl");
    FileWriter fw = new FileWriter("/tmp/hibernate.hbm.xml");
    freemarkerTemplate.process(data, fw);
}

From source file:org.broadleafcommerce.openadmin.server.dao.DynamicEntityDaoImpl.java

License:Apache License

protected FieldMetadata getFieldMetadata(String prefix, String propertyName, List<Property> componentProperties,
        SupportedFieldType type, SupportedFieldType secondaryType, Type entityType, Class<?> targetClass,
        FieldPresentationAttributes presentationAttribute, MergedPropertyType mergedPropertyType)
        throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException,
        IllegalAccessException, InvocationTargetException {
    FieldMetadata fieldMetadata = new FieldMetadata();
    fieldMetadata.setFieldType(type);/*w  w  w  .j  a  va  2  s. com*/
    fieldMetadata.setSecondaryType(secondaryType);
    if (entityType != null && !entityType.isCollectionType()) {
        Column column = null;
        for (Property property : componentProperties) {
            if (property.getName().equals(propertyName)) {
                column = (Column) property.getColumnIterator().next();
                break;
            }
        }
        if (column != null) {
            fieldMetadata.setLength(column.getLength());
            fieldMetadata.setScale(column.getScale());
            fieldMetadata.setPrecision(column.getPrecision());
            fieldMetadata.setRequired(!column.isNullable());
            fieldMetadata.setUnique(column.isUnique());
        }
        fieldMetadata.setCollection(false);
    } else {
        fieldMetadata.setCollection(true);
    }
    fieldMetadata.setMutable(true);
    fieldMetadata.setInheritedFromType(targetClass.getName());
    fieldMetadata.setAvailableToTypes(new String[] { targetClass.getName() });
    if (presentationAttribute != null) {
        fieldMetadata.setPresentationAttributes(presentationAttribute);
    }
    fieldMetadata.setMergedPropertyType(mergedPropertyType);
    if (SupportedFieldType.BROADLEAF_ENUMERATION.equals(type)) {
        setupBroadleafEnumeration(presentationAttribute.getBroadleafEnumeration(), fieldMetadata);
    }

    return fieldMetadata;
}

From source file:org.broadleafcommerce.openadmin.server.dao.provider.metadata.DefaultFieldMetadataProvider.java

License:Apache License

@Override
public FieldProviderResponse addMetadataFromMappingData(
        AddMetadataFromMappingDataRequest addMetadataFromMappingDataRequest, FieldMetadata metadata) {
    BasicFieldMetadata fieldMetadata = (BasicFieldMetadata) metadata;
    fieldMetadata.setFieldType(addMetadataFromMappingDataRequest.getType());
    fieldMetadata.setSecondaryType(addMetadataFromMappingDataRequest.getSecondaryType());
    if (addMetadataFromMappingDataRequest.getRequestedEntityType() != null
            && !addMetadataFromMappingDataRequest.getRequestedEntityType().isCollectionType()) {
        Column column = null;
        for (Property property : addMetadataFromMappingDataRequest.getComponentProperties()) {
            if (property.getName().equals(addMetadataFromMappingDataRequest.getPropertyName())) {
                Object columnObject = property.getColumnIterator().next();
                if (columnObject instanceof Column) {
                    column = (Column) columnObject;
                }//from ww w  .j a v a  2s  .com
                break;
            }
        }
        if (column != null) {
            fieldMetadata.setLength(column.getLength());
            fieldMetadata.setScale(column.getScale());
            fieldMetadata.setPrecision(column.getPrecision());
            fieldMetadata.setRequired(!column.isNullable());
            fieldMetadata.setUnique(column.isUnique());
        }
        fieldMetadata.setForeignKeyCollection(false);
    } else {
        fieldMetadata.setForeignKeyCollection(true);
    }
    fieldMetadata.setMutable(true);
    fieldMetadata.setMergedPropertyType(addMetadataFromMappingDataRequest.getMergedPropertyType());
    if (SupportedFieldType.BROADLEAF_ENUMERATION.equals(addMetadataFromMappingDataRequest.getType())) {
        try {
            setupBroadleafEnumeration(fieldMetadata.getBroadleafEnumeration(), fieldMetadata,
                    addMetadataFromMappingDataRequest.getDynamicEntityDao());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return FieldProviderResponse.HANDLED;
}