Example usage for org.hibernate.mapping Table getColumn

List of usage examples for org.hibernate.mapping Table getColumn

Introduction

In this page you can find the example usage for org.hibernate.mapping Table getColumn.

Prototype

public Column getColumn(int n) 

Source Link

Usage

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

License:Open Source License

/**
 * Generate sql scripts/*from w ww . ja  v  a 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.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinderTests.java

License:Apache License

public void testUniqueConstraintGeneration() {
    DefaultGrailsDomainConfiguration config = getDomainConfig(UNIQUE_PROPERTIES);
    assertEquals("Tables created", 1, getTableCount(config));
    List expectedKeyColumns1 = Arrays
            .asList(new Column[] { new Column("camel_cased"), new Column("group"), new Column("login") });
    List expectedKeyColumns2 = Arrays.asList(new Column[] { new Column("camel_cased"), new Column("group") });
    Table mapping = (Table) config.getTableMappings().next();
    int cnt = 0;/*  w  w w . ja va 2s  .  c om*/
    boolean found1 = false, found2 = false;
    for (Iterator i = mapping.getUniqueKeyIterator(); i.hasNext();) {
        UniqueKey key = (UniqueKey) i.next();
        List keyColumns = key.getColumns();
        if (keyColumns.equals(expectedKeyColumns1)) {
            found1 = true;
        }
        if (keyColumns.equals(expectedKeyColumns2)) {
            found2 = true;
        }
        cnt++;
    }
    assertEquals(2, cnt);
    assertEquals(true, mapping.getColumn(new Column("employeeID")).isUnique());
    assertEquals(true, found1);
    assertEquals(true, found2);
}

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

License:Apache License

private void assertColumnNotNullable(String tablename, String columnName,
        DefaultGrailsDomainConfiguration config) {
    Table table = getTableMapping(tablename, config);
    assertTrue(table.getName() + "." + columnName + " is not nullable",
            !table.getColumn(new Column(columnName)).isNullable());
}

From source file:org.eclipse.emf.teneo.hibernate.HbDataStore.java

License:Open Source License

/** Checks if a certain column already exists in a class */
private Column checkColumnExists(Table table, Column searchCol) {
    for (int i = 0; i < table.getColumnSpan(); i++) {
        final Column column = table.getColumn(i);
        if (stripQuotes(column.getName()).equalsIgnoreCase(searchCol.getName())) {
            return column;
        }/*from w w  w. j a  v  a  2 s .c o  m*/
    }
    table.addColumn(searchCol);
    return searchCol;
}

From source file:org.openeos.tools.hibernate.hbm2java.UnoReverseEngineeringStrategy.java

License:Apache License

@Override
public boolean isForeignKeyCollectionInverse(String name, TableIdentifier foreignKeyTable, List columns,
        TableIdentifier foreignKeyReferencedTable, List referencedColumns) {

    Table fkTable = runtimeInfo.getTable(foreignKeyTable);
    if (fkTable == null) {
        return true; // we don't know better
    }// w w w.j a v  a  2  s  .c  o  m

    if (isManyToManyTable(fkTable)) {
        // if the reference column is the first one then we are inverse.
        Column column = fkTable.getColumn(0);
        Column fkColumn = (Column) referencedColumns.get(0);
        if (fkColumn.equals(column)) {
            return true;
        } else {
            return false;
        }
    }
    return true;

}