Example usage for org.hibernate.mapping Value getColumnIterator

List of usage examples for org.hibernate.mapping Value getColumnIterator

Introduction

In this page you can find the example usage for org.hibernate.mapping Value getColumnIterator.

Prototype

public Iterator<Selectable> getColumnIterator();

Source Link

Usage

From source file:net.chrisrichardson.ormunit.hibernate.HibernateAssertUtil.java

License:Apache License

public static void assertColumn(String idColumn, Value identifier) {
    Iterator columnIt = identifier.getColumnIterator();
    Assert.assertTrue(columnIt.hasNext());
    Column column = (Column) columnIt.next();
    Assert.assertFalse(columnIt.hasNext());
    if (!idColumn.equalsIgnoreCase(column.getName()))
        Assert.assertEquals(idColumn, column.getName());
}

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

License:Open Source License

/**
 * Generate sql scripts/*from  w  ww. j a  v a 2s.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.AbstractGrailsDomainBinder.java

License:Apache License

protected void bindComponentProperty(Component component, GrailsDomainClassProperty componentProperty,
        GrailsDomainClassProperty currentGrailsProp, PersistentClass persistentClass, String path, Table table,
        Mappings mappings, String sessionFactoryBeanName) {
    Value value;
    // see if it's a collection type
    CollectionType collectionType = CT.collectionTypeForClass(currentGrailsProp.getType());
    if (collectionType != null) {
        // create collection
        Collection collection = collectionType.create(currentGrailsProp, persistentClass, path, mappings,
                sessionFactoryBeanName);
        mappings.addCollection(collection);
        value = collection;//from ww  w.j  av a  2  s.com
    }
    // work out what type of relationship it is and bind value
    else if (currentGrailsProp.isManyToOne()) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as ManyToOne");

        value = new ManyToOne(mappings, table);
        bindManyToOne(currentGrailsProp, (ManyToOne) value, path, mappings, sessionFactoryBeanName);
    } else if (currentGrailsProp.isOneToOne()) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as OneToOne");

        if (canBindOneToOneWithSingleColumnAndForeignKey(currentGrailsProp)) {
            value = new OneToOne(mappings, table, persistentClass);
            bindOneToOne(currentGrailsProp, (OneToOne) value, path, sessionFactoryBeanName);
        } else {
            value = new ManyToOne(mappings, table);
            bindManyToOne(currentGrailsProp, (ManyToOne) value, path, mappings, sessionFactoryBeanName);
        }
    } else if (currentGrailsProp.isEmbedded()) {
        value = new Component(mappings, persistentClass);
        bindComponent((Component) value, currentGrailsProp, true, mappings, sessionFactoryBeanName);
    } else {
        if (LOG.isDebugEnabled())
            LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                    + "] as SimpleValue");

        value = new SimpleValue(mappings, table);
        if (currentGrailsProp.isEnum()) {
            bindEnumType(currentGrailsProp, (SimpleValue) value, path, sessionFactoryBeanName);
        } else {
            bindSimpleValue(currentGrailsProp, componentProperty, (SimpleValue) value, path, mappings,
                    sessionFactoryBeanName);
        }
    }

    if (value != null) {
        Property persistentProperty = createProperty(value, persistentClass, currentGrailsProp, mappings);
        component.addProperty(persistentProperty);
        if (isComponentPropertyNullable(componentProperty)) {
            final Iterator<?> columnIterator = value.getColumnIterator();
            while (columnIterator.hasNext()) {
                Column c = (Column) columnIterator.next();
                c.setNullable(true);
            }
        }
    }
}

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

License:Apache License

private static void bindComponentProperty(Component component, GrailsDomainClassProperty componentProperty,
        GrailsDomainClassProperty currentGrailsProp, PersistentClass persistentClass, String path, Table table,
        Mappings mappings, String sessionFactoryBeanName) {
    Value value;
    // see if it's a collection type
    CollectionType collectionType = CollectionType.collectionTypeForClass(currentGrailsProp.getType());
    if (collectionType != null) {
        // create collection

        Collection collection = collectionType.create(currentGrailsProp, persistentClass, path, mappings,
                sessionFactoryBeanName);
        mappings.addCollection(collection);
        value = collection;/*from  w  w w.j  a  v a2s . co  m*/
    }
    // work out what type of relationship it is and bind value
    else if (currentGrailsProp.isManyToOne()) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as ManyToOne");

        value = new ManyToOne(mappings, table);
        bindManyToOne(currentGrailsProp, (ManyToOne) value, path, mappings, sessionFactoryBeanName);
    } else if (currentGrailsProp.isOneToOne()) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as OneToOne");

        if (canBindOneToOneWithSingleColumnAndForeignKey(currentGrailsProp)) {
            value = new OneToOne(mappings, table, persistentClass);
            bindOneToOne(currentGrailsProp, (OneToOne) value, path, sessionFactoryBeanName);
        } else {
            value = new ManyToOne(mappings, table);
            bindManyToOne(currentGrailsProp, (ManyToOne) value, path, mappings, sessionFactoryBeanName);
        }
    } else if (currentGrailsProp.isEmbedded()) {
        value = new Component(mappings, persistentClass);
        bindComponent((Component) value, currentGrailsProp, true, mappings, sessionFactoryBeanName);
    } else {
        if (LOG.isDebugEnabled())
            LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                    + "] as SimpleValue");

        value = new SimpleValue(mappings, table);
        if (currentGrailsProp.isEnum()) {
            bindEnumType(currentGrailsProp, (SimpleValue) value, path, sessionFactoryBeanName);
        } else {
            bindSimpleValue(currentGrailsProp, componentProperty, (SimpleValue) value, path, mappings,
                    sessionFactoryBeanName);
        }
    }

    if (value != null) {
        Property persistentProperty = createProperty(value, persistentClass, currentGrailsProp, mappings);
        component.addProperty(persistentProperty);
        if (isComponentPropertyNullable(componentProperty)) {
            final Iterator<?> columnIterator = value.getColumnIterator();
            while (columnIterator.hasNext()) {
                Column c = (Column) columnIterator.next();
                c.setNullable(true);
            }
        }
    }
}

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

License:Apache License

protected void bindComponentProperty(Component component, PersistentProperty componentProperty,
        PersistentProperty currentGrailsProp, PersistentClass persistentClass, String path, Table table,
        Mappings mappings, String sessionFactoryBeanName) {
    Value value;
    // see if it's a collection type
    CollectionType collectionType = CT.collectionTypeForClass(currentGrailsProp.getType());
    if (collectionType != null) {
        // create collection
        Collection collection = collectionType.create((ToMany) currentGrailsProp, persistentClass, path,
                mappings, sessionFactoryBeanName);
        mappings.addCollection(collection);
        value = collection;//  w w w.  j a v  a 2 s  . c  o m
    }
    // work out what type of relationship it is and bind value
    else if (currentGrailsProp instanceof org.grails.datastore.mapping.model.types.ManyToOne) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as ManyToOne");

        value = new ManyToOne(mappings, table);
        bindManyToOne((Association) currentGrailsProp, (ManyToOne) value, path, mappings,
                sessionFactoryBeanName);
    } else if (currentGrailsProp instanceof org.grails.datastore.mapping.model.types.OneToOne) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as OneToOne");

        if (canBindOneToOneWithSingleColumnAndForeignKey((Association) currentGrailsProp)) {
            value = new OneToOne(mappings, table, persistentClass);
            bindOneToOne((org.grails.datastore.mapping.model.types.OneToOne) currentGrailsProp,
                    (OneToOne) value, path, sessionFactoryBeanName);
        } else {
            value = new ManyToOne(mappings, table);
            bindManyToOne((Association) currentGrailsProp, (ManyToOne) value, path, mappings,
                    sessionFactoryBeanName);
        }
    } else if (currentGrailsProp instanceof Embedded) {
        value = new Component(mappings, persistentClass);
        bindComponent((Component) value, (Embedded) currentGrailsProp, true, mappings, sessionFactoryBeanName);
    } else {
        if (LOG.isDebugEnabled())
            LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                    + "] as SimpleValue");

        value = new SimpleValue(mappings, table);
        if (currentGrailsProp.getType().isEnum()) {
            bindEnumType(currentGrailsProp, (SimpleValue) value, path, sessionFactoryBeanName);
        } else {
            bindSimpleValue(currentGrailsProp, componentProperty, (SimpleValue) value, path, mappings,
                    sessionFactoryBeanName);
        }
    }

    if (value != null) {
        Property persistentProperty = createProperty(value, persistentClass, currentGrailsProp, mappings);
        component.addProperty(persistentProperty);
        if (isComponentPropertyNullable(componentProperty)) {
            final Iterator<?> columnIterator = value.getColumnIterator();
            while (columnIterator.hasNext()) {
                Column c = (Column) columnIterator.next();
                c.setNullable(true);
            }
        }
    }
}

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

License:Apache License

protected void bindComponentProperty(Component component, PersistentProperty componentProperty,
        PersistentProperty currentGrailsProp, PersistentClass persistentClass, String path, Table table,
        InFlightMetadataCollector mappings, String sessionFactoryBeanName) {
    Value value;
    // see if it's a collection type
    CollectionType collectionType = CT.collectionTypeForClass(currentGrailsProp.getType());
    if (collectionType != null) {
        // create collection
        Collection collection = collectionType.create((ToMany) currentGrailsProp, persistentClass, path,
                mappings, sessionFactoryBeanName);
        mappings.addCollectionBinding(collection);
        value = collection;/*from   w  w  w . j  a va 2  s  . com*/
    }
    // work out what type of relationship it is and bind value
    else if (currentGrailsProp instanceof org.grails.datastore.mapping.model.types.ManyToOne) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as ManyToOne");

        value = new ManyToOne(mappings, table);
        bindManyToOne((Association) currentGrailsProp, (ManyToOne) value, path, mappings,
                sessionFactoryBeanName);
    } else if (currentGrailsProp instanceof org.grails.datastore.mapping.model.types.OneToOne) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as OneToOne");

        if (canBindOneToOneWithSingleColumnAndForeignKey((Association) currentGrailsProp)) {
            value = new OneToOne(mappings, table, persistentClass);
            bindOneToOne((org.grails.datastore.mapping.model.types.OneToOne) currentGrailsProp,
                    (OneToOne) value, path, sessionFactoryBeanName);
        } else {
            value = new ManyToOne(mappings, table);
            bindManyToOne((Association) currentGrailsProp, (ManyToOne) value, path, mappings,
                    sessionFactoryBeanName);
        }
    } else if (currentGrailsProp instanceof Embedded) {
        value = new Component(mappings, persistentClass);
        bindComponent((Component) value, (Embedded) currentGrailsProp, true, mappings, sessionFactoryBeanName);
    } else {
        if (LOG.isDebugEnabled())
            LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                    + "] as SimpleValue");

        value = new SimpleValue(mappings, table);
        if (currentGrailsProp.getType().isEnum()) {
            bindEnumType(currentGrailsProp, (SimpleValue) value, path, sessionFactoryBeanName);
        } else {
            bindSimpleValue(currentGrailsProp, componentProperty, (SimpleValue) value, path, mappings,
                    sessionFactoryBeanName);
        }
    }

    if (value != null) {
        Property persistentProperty = createProperty(value, persistentClass, currentGrailsProp, mappings);
        component.addProperty(persistentProperty);
        if (isComponentPropertyNullable(componentProperty)) {
            final Iterator<?> columnIterator = value.getColumnIterator();
            while (columnIterator.hasNext()) {
                Column c = (Column) columnIterator.next();
                c.setNullable(true);
            }
        }
    }
}