Example usage for org.hibernate.mapping Collection isIdentified

List of usage examples for org.hibernate.mapping Collection isIdentified

Introduction

In this page you can find the example usage for org.hibernate.mapping Collection isIdentified.

Prototype

public boolean isIdentified() 

Source Link

Usage

From source file:org.beangle.commons.orm.hibernate.internal.SchemaValidator.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
// borrow from Configuration code
private Iterator<IdentifierGenerator> iterateGenerators(Configuration config, Dialect dialect)
        throws MappingException {

    TreeMap generators = new TreeMap();
    String defaultCatalog = sessionFactoryBean.getHibernateProperties()
            .getProperty(Environment.DEFAULT_CATALOG);
    String defaultSchema = sessionFactoryBean.getHibernateProperties().getProperty(Environment.DEFAULT_SCHEMA);

    for (Iterator<PersistentClass> pcIter = config.getClassMappings(); pcIter.hasNext();) {
        PersistentClass pc = pcIter.next();
        if (!pc.isInherited()) {
            IdentifierGenerator ig = pc.getIdentifier().createIdentifierGenerator(
                    config.getIdentifierGeneratorFactory(), dialect, defaultCatalog, defaultSchema,
                    (RootClass) pc);/* www .ja  v a  2 s.  c  om*/

            if (ig instanceof PersistentIdentifierGenerator) {
                generators.put(((PersistentIdentifierGenerator) ig).generatorKey(), ig);
            } else if (ig instanceof IdentifierGeneratorAggregator) {
                ((IdentifierGeneratorAggregator) ig).registerPersistentGenerators(generators);
            }
        }
    }

    for (Iterator<Collection> collIter = config.getCollectionMappings(); collIter.hasNext();) {
        Collection collection = collIter.next();
        if (collection.isIdentified()) {
            IdentifierGenerator ig = ((IdentifierCollection) collection).getIdentifier()
                    .createIdentifierGenerator(config.getIdentifierGeneratorFactory(), dialect, defaultCatalog,
                            defaultSchema, null);

            if (ig instanceof PersistentIdentifierGenerator) {
                generators.put(((PersistentIdentifierGenerator) ig).generatorKey(), ig);
            }
        }
    }

    return generators.values().iterator();
}

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

License:Open Source License

/**
 * Generate sql scripts// ww  w .  jav  a 2 s  .  c  om
 * 
 * @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.n52.sos.ds.datasource.CustomConfiguration.java

License:Open Source License

/**
 * Copied from// ww  w .  j  a  v a 2  s .c o m
 * {@link org.hibernate.cfg.Configuration#iterateGenerators(Dialect)}.
 */
private Iterator<PersistentIdentifierGenerator> iterateGenerators(final Dialect d, final String c,
        final String s) throws MappingException {
    final TreeMap<Object, PersistentIdentifierGenerator> generators = new TreeMap<Object, PersistentIdentifierGenerator>();
    for (final PersistentClass pc : classes.values()) {
        if (!pc.isInherited()) {
            final IdentifierGenerator ig = pc.getIdentifier()
                    .createIdentifierGenerator(getIdentifierGeneratorFactory(), d, c, s, (RootClass) pc);
            if (ig instanceof PersistentIdentifierGenerator) {
                final PersistentIdentifierGenerator pig = (PersistentIdentifierGenerator) ig;
                generators.put(pig.generatorKey(), pig);
            } else if (ig instanceof IdentifierGeneratorAggregator) {
                ((IdentifierGeneratorAggregator) ig).registerPersistentGenerators(generators);
            }
        }
    }
    for (final Collection collection : collections.values()) {
        if (collection.isIdentified()) {
            final IdentifierGenerator ig = ((IdentifierCollection) collection).getIdentifier()
                    .createIdentifierGenerator(getIdentifierGeneratorFactory(), d, c, s, null);
            if (ig instanceof PersistentIdentifierGenerator) {
                final PersistentIdentifierGenerator pig = (PersistentIdentifierGenerator) ig;
                generators.put(pig.generatorKey(), pig);
            }
        }
    }

    return generators.values().iterator();
}