Example usage for org.hibernate.mapping Column setComment

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

Introduction

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

Prototype

public void setComment(String comment) 

Source Link

Usage

From source file:com.vecna.maven.hibernate.HibernateDocMojo.java

License:Apache License

/**
 * set a comment on Hibernate columns/*from w  ww  .j a  v  a2  s . co m*/
 */
private void setComment(String comment, Iterator<Column> columnIterator) {
    while (columnIterator.hasNext()) {
        Column column = columnIterator.next();
        if (encryptedTypeRegex != null && column.getValue() instanceof SimpleValue) {
            String typeName = ((SimpleValue) column.getValue()).getTypeName();
            if (typeName != null && typeName.matches(encryptedTypeRegex)) {
                comment += " [encrypted]";
            }
        }
        column.setComment(comment);
    }
}

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

License:Open Source License

/**
 * Generate sql scripts/*  w w  w.  java 2 s.c o  m*/
 * 
 * @param fileName
 * @param packageName
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public void gen(String fileName, String packageName) throws Exception {
    configuration = ConfigBuilder.build(new OverrideConfiguration());
    mapping = configuration.buildMapping();
    defaultCatalog = configuration.getProperties().getProperty(Environment.DEFAULT_CATALOG);
    defaultSchema = configuration.getProperties().getProperty(Environment.DEFAULT_SCHEMA);
    configuration.getProperties().put(Environment.DIALECT, dialect);
    // 1. first process class mapping
    Iterator<PersistentClass> iterpc = configuration.getClassMappings();
    while (iterpc.hasNext()) {
        PersistentClass pc = iterpc.next();
        Class<?> clazz = pc.getMappedClass();
        if (isNotBlank(packageName) && !clazz.getPackage().getName().startsWith(packageName))
            continue;
        // add comment to table and column
        pc.getTable().setComment(messages.get(clazz, clazz.getSimpleName()));
        commentProperty(clazz, pc.getTable(), pc.getIdentifierProperty());
        commentProperties(clazz, pc.getTable(), pc.getPropertyIterator());
        // generator sequence sql
        if (pc instanceof RootClass) {
            IdentifierGenerator ig = pc.getIdentifier().createIdentifierGenerator(
                    configuration.getIdentifierGeneratorFactory(), dialect, defaultCatalog, defaultSchema,
                    (RootClass) pc);
            if (ig instanceof PersistentIdentifierGenerator) {
                String[] lines = ((PersistentIdentifierGenerator) ig).sqlCreateStrings(dialect);
                sequences.addAll(Arrays.asList(lines));
            }
        }
        // generater table sql
        generateTableSql(pc.getTable());
    }

    // 2. process collection mapping
    Iterator<Collection> itercm = configuration.getCollectionMappings();
    while (itercm.hasNext()) {
        Collection col = itercm.next();
        if (isNotBlank(packageName) && !col.getRole().startsWith(packageName))
            continue;
        // collection sequences
        if (col.isIdentified()) {
            IdentifierGenerator ig = ((IdentifierCollection) col).getIdentifier().createIdentifierGenerator(
                    configuration.getIdentifierGeneratorFactory(), dialect, defaultCatalog, defaultSchema,
                    null);

            if (ig instanceof PersistentIdentifierGenerator) {
                String[] lines = ((PersistentIdentifierGenerator) ig).sqlCreateStrings(dialect);
                sequences.addAll(Arrays.asList(lines));
            }
        }
        // collection table
        if (!col.isOneToMany()) {
            Table table = col.getCollectionTable();
            String owner = col.getTable().getComment();
            Class<?> ownerClass = col.getOwner().getMappedClass();
            // resolved nested compoent name in collection's role
            String colName = substringAfter(col.getRole(), col.getOwnerEntityName() + ".");
            if (colName.contains("."))
                ownerClass = getPropertyType(col.getOwner(), substringBeforeLast(colName, "."));
            table.setComment(owner + "-" + messages.get(ownerClass, substringAfterLast(col.getRole(), ".")));

            Column keyColumn = table.getColumn((Column) col.getKey().getColumnIterator().next());
            if (null != keyColumn)
                keyColumn.setComment(owner + " ID");

            if (col instanceof IndexedCollection) {
                IndexedCollection idxCol = (IndexedCollection) col;
                Value idx = idxCol.getIndex();
                if (idx instanceof ToOne)
                    commentToOne((ToOne) idx, (Column) idx.getColumnIterator().next());
            }
            if (col.getElement() instanceof ManyToOne) {
                Column valueColumn = (Column) col.getElement().getColumnIterator().next();
                commentToOne((ManyToOne) col.getElement(), valueColumn);
            } else if (col.getElement() instanceof Component) {
                Component cp = (Component) col.getElement();
                commentProperties(cp.getComponentClass(), table, cp.getPropertyIterator());
            }
            generateTableSql(col.getCollectionTable());
        }
    }
    Set<String> commentSet = CollectUtils.newHashSet(comments);
    comments.clear();
    comments.addAll(commentSet);
    // 3. export to files
    for (String key : files.keySet()) {
        List<List<String>> sqls = files.get(key);
        FileWriter writer = new FileWriter(fileName + "/" + key, false);
        writes(writer, sqls);
        writer.flush();
        writer.close();
    }
}

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

License:Open Source License

private void commentToOne(ToOne toOne, Column column) {
    String entityName = toOne.getReferencedEntityName();
    PersistentClass referClass = configuration.getClassMapping(entityName);
    if (null != referClass) {
        column.setComment(referClass.getTable().getComment() + " ID");
    }//w w w .  j  a va 2s  . c o m
}

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

License:Open Source License

@SuppressWarnings("unchecked")
private void commentProperty(Class<?> clazz, Table table, Property p) {
    if (null == p)
        return;//from  w w  w .  ja  va  2s  .  co  m
    if (p.getColumnSpan() == 1) {
        Column column = (Column) p.getColumnIterator().next();
        if (isForeignColumn(table, column)) {
            column.setComment(messages.get(clazz, p.getName()) + " ID");
        } else {
            column.setComment(messages.get(clazz, p.getName()));
        }
    } else if (p.getColumnSpan() > 1) {
        Component pc = ((Component) p.getValue());
        Class<?> columnOwnerClass = pc.getComponentClass();
        commentProperties(columnOwnerClass, table, pc.getPropertyIterator());
    }
}

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

License:Apache License

/**
 * Binds a Column instance to the Hibernate meta model
 *
 * @param property The Grails domain class property
 * @param parentProperty/*from   w  w w  .j  a  v a 2 s.c o  m*/
 * @param column     The column to bind
 * @param path
 * @param table      The table name
 * @param sessionFactoryBeanName  the session factory bean name
 */
protected void bindColumn(GrailsDomainClassProperty property, GrailsDomainClassProperty parentProperty,
        Column column, ColumnConfig cc, String path, Table table, String sessionFactoryBeanName) {

    if (cc != null) {
        column.setComment(cc.getComment());
        column.setDefaultValue(cc.getDefaultValue());
        column.setCustomRead(cc.getRead());
        column.setCustomWrite(cc.getWrite());
    }

    Class<?> userType = getUserType(property);
    String columnName = getColumnNameForPropertyAndPath(property, path, cc, sessionFactoryBeanName);
    if ((property.isAssociation() || property.isBasicCollectionType()) && userType == null) {
        // Only use conventional naming when the column has not been explicitly mapped.
        if (column.getName() == null) {
            column.setName(columnName);
        }
        if (property.isManyToMany()) {
            column.setNullable(false);
        } else if (property.isOneToOne() && property.isBidirectional() && !property.isOwningSide()) {
            if (property.getOtherSide().isHasOne()) {
                column.setNullable(false);
            } else {
                column.setNullable(true);
            }
        } else if ((property.isManyToOne() || property.isOneToOne()) && property.isCircular()) {
            column.setNullable(true);
        } else {
            column.setNullable(property.isOptional());
        }
    } else {
        column.setName(columnName);
        column.setNullable(property.isOptional() || (parentProperty != null && parentProperty.isOptional()));

        // Use the constraints for this property to more accurately define
        // the column's length, precision, and scale
        ConstrainedProperty constrainedProperty = getConstrainedProperty(property);
        if (constrainedProperty != null) {
            if (String.class.isAssignableFrom(property.getType())
                    || byte[].class.isAssignableFrom(property.getType())) {
                bindStringColumnConstraints(column, constrainedProperty);
            }

            if (Number.class.isAssignableFrom(property.getType())) {
                bindNumericColumnConstraints(column, constrainedProperty, cc);
            }
        }
    }

    handleUniqueConstraint(property, column, path, table, columnName, sessionFactoryBeanName);

    bindIndex(columnName, column, cc, table);

    if (!property.getDomainClass().isRoot()) {
        Mapping mapping = getMapping(property.getDomainClass());
        if (mapping == null || mapping.getTablePerHierarchy()) {
            if (LOG.isDebugEnabled())
                LOG.debug("[GrailsDomainBinder] Sub class property [" + property.getName()
                        + "] for column name [" + column.getName() + "] set to nullable");
            column.setNullable(true);
        } else {
            column.setNullable(property.isOptional());
        }
    }

    if (LOG.isDebugEnabled())
        LOG.debug("[GrailsDomainBinder] bound property [" + property.getName() + "] to column name ["
                + column.getName() + "] in table [" + table.getName() + "]");
}

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

License:Apache License

/**
 * Binds a Column instance to the Hibernate meta model
 *
 * @param property The Grails domain class property
 * @param parentProperty//  w w w . j  a  v  a2s. c  om
 * @param column     The column to bind
 * @param path
 * @param table      The table name
 * @param sessionFactoryBeanName  the session factory bean name
 */
protected void bindColumn(PersistentProperty property, PersistentProperty parentProperty, Column column,
        ColumnConfig cc, String path, Table table, String sessionFactoryBeanName) {

    if (cc != null) {
        column.setComment(cc.getComment());
        column.setDefaultValue(cc.getDefaultValue());
        column.setCustomRead(cc.getRead());
        column.setCustomWrite(cc.getWrite());
    }

    Class<?> userType = getUserType(property);
    String columnName = getColumnNameForPropertyAndPath(property, path, cc, sessionFactoryBeanName);
    if ((property instanceof Association) && userType == null) {
        Association association = (Association) property;
        // Only use conventional naming when the column has not been explicitly mapped.
        if (column.getName() == null) {
            column.setName(columnName);
        }
        if (property instanceof ManyToMany) {
            column.setNullable(false);
        } else if (property instanceof org.grails.datastore.mapping.model.types.OneToOne
                && association.isBidirectional() && !association.isOwningSide()) {
            if (isHasOne(((Association) property).getInverseSide())) {
                column.setNullable(false);
            } else {
                column.setNullable(true);
            }
        } else if ((property instanceof ToOne) && association.isCircular()) {
            column.setNullable(true);
        } else {
            column.setNullable(property.isNullable());
        }
    } else {
        column.setName(columnName);
        column.setNullable(property.isNullable() || (parentProperty != null && parentProperty.isNullable()));

        // Use the constraints for this property to more accurately define
        // the column's length, precision, and scale
        if (String.class.isAssignableFrom(property.getType())
                || byte[].class.isAssignableFrom(property.getType())) {
            bindStringColumnConstraints(column, property);
        }

        if (Number.class.isAssignableFrom(property.getType())) {
            bindNumericColumnConstraints(column, property, cc);
        }
    }

    handleUniqueConstraint(property, column, path, table, columnName, sessionFactoryBeanName);

    bindIndex(columnName, column, cc, table);

    final PersistentEntity owner = property.getOwner();
    if (!owner.isRoot()) {
        Mapping mapping = getMapping(owner);
        if (mapping == null || mapping.getTablePerHierarchy()) {
            if (LOG.isDebugEnabled())
                LOG.debug("[GrailsDomainBinder] Sub class property [" + property.getName()
                        + "] for column name [" + column.getName() + "] set to nullable");
            column.setNullable(true);
        } else {
            column.setNullable(property.isNullable());
        }
    }

    if (LOG.isDebugEnabled())
        LOG.debug("[GrailsDomainBinder] bound property [" + property.getName() + "] to column name ["
                + column.getName() + "] in table [" + table.getName() + "]");
}