Example usage for org.hibernate.mapping RootClass getTable

List of usage examples for org.hibernate.mapping RootClass getTable

Introduction

In this page you can find the example usage for org.hibernate.mapping RootClass getTable.

Prototype

@Override
    public Table getTable() 

Source Link

Usage

From source file:com.enonic.cms.store.hibernate.cache.invalidation.InvalidationRulesBuilder.java

License:Open Source License

/**
 * Build the root class./*from  www .j av a2  s  .  co  m*/
 */
private void createTableRule(RootClass mapping) {
    addTableRule(mapping.getTable().getName(), mapping.getClassName());
}

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

License:Open Source License

protected void createM2O(Configuration config, Mappings mappings, ForeignKey relationship) {
    com.manydesigns.portofino.model.database.Table manyMDTable = relationship.getFromTable();
    com.manydesigns.portofino.model.database.Table oneMDTable = relationship.getToTable();
    String manyMDQualifiedTableName = manyMDTable.getActualEntityName();
    String oneMDQualifiedTableName = oneMDTable.getActualEntityName();

    RootClass clazz = (RootClass) mappings.getClass(manyMDQualifiedTableName);
    if (clazz == null) {
        logger.error("Cannot find table '{}' as 'many' side of foreign key '{}'. Skipping relationship.",
                manyMDQualifiedTableName, relationship.getName());
        return;/* w w w .  j  a v  a  2  s .co  m*/
    }

    Table tab = clazz.getTable();
    List<String> columnNames = new ArrayList<String>();

    for (Reference ref : relationship.getReferences()) {
        if (ref.getActualFromColumn() == null) {
            logger.error("Missing from column {}, skipping relationship", ref.getFromColumn());
            return;
        }
        columnNames.add(ref.getFromColumn());
    }

    ManyToOne m2o = new ManyToOne(mappings, tab);
    m2o.setLazy(LAZY);
    final HashMap<String, PersistentClass> persistentClasses = new HashMap<String, PersistentClass>();
    persistentClasses.put(oneMDQualifiedTableName, config.getClassMapping(oneMDQualifiedTableName));
    m2o.setReferencedEntityName(oneMDQualifiedTableName);
    m2o.createPropertyRefConstraints(persistentClasses);

    PersistentClass manyClass = config.getClassMapping(manyMDQualifiedTableName);
    for (String columnName : columnNames) {
        Column col = new Column();
        col.setName(escapeName(columnName));
        //Recupero la colonna precedentemente associata alla tabella:
        //essa ha uno uniqueIdentifier generato al momento dell'associazione alla tabella;
        //questo viene utilizzato per disambiguare l'alias della colonna nelle query
        //SQL generate da Hibernate.
        col = manyClass.getTable().getColumn(col);
        if (col == null) {
            logger.error("Column not found in 'many' entity {}: {}, " + "skipping relationship",
                    manyClass.getEntityName(), columnName);
            return;
        }
        m2o.addColumn(col);
    }

    Property prop = new Property();
    prop.setName(relationship.getActualOnePropertyName());
    //prop.setNodeName(relationship.getActualOnePropertyName());
    prop.setValue(m2o);
    prop.setCascade("none"); //TODO era "all", capire
    prop.setInsertable(false);
    prop.setUpdateable(false);
    clazz.addProperty(prop);
}

From source file:com.tomitribe.reveng.codegen.FreemarkerObject.java

License:Apache License

public String annotate(final BasicPOJOClass pojo, final Object pObj, final Object rootObj,
        final Object toolObj) {
    // public String annotate(final Object pojo, final Property p, final
    // RootClass root, final Cfg2HbmTool tool) {

    if (BasicPOJOClass.class.isInstance(pojo)) {

    } else {//w  ww  .  j a  va 2 s  .co  m
        // System.out.println(ob.class.getName());
    }

    RootClass root = null;
    try {
        root = (RootClass) rootObj;
    } catch (final Exception e) {
        // TODO Auto-generated catch block
        // e.printStackTrace();
        return "";
    }

    final Property p = (Property) pObj;
    final Cfg2HbmTool tool = (Cfg2HbmTool) toolObj;

    final Table table = root.getTable();

    Iterator it = table.getColumnIterator();

    Column column = null;
    Column c;
    String name;
    while (it.hasNext()) {
        c = (Column) it.next();

        name = c.getName().replace("_", "").toLowerCase();

        if (name.equals(p.getName().toLowerCase())) {
            column = c;
            break;
        }
    }

    if (null != column) {

        System.out.print("FreemarkerObject.annotate: " + table.getName() + " - " + p.getName() + " - ");

        it = table.getIndexIterator();

        org.hibernate.mapping.Index index;
        while (it.hasNext()) {

            final Object next = it.next();

            if (org.hibernate.mapping.Index.class.isInstance(next)) {

                index = org.hibernate.mapping.Index.class.cast(next);

                if (index.containsColumn(column)) {
                    System.out.print(index.getName());

                    return String.format("\n@Index(name = \"%1$s\", columnNames = {\"%2$s\"})",
                            index.getName().toLowerCase(), column.getName());
                }
            }
        }

        System.out.println();
    } else {
        System.out.println("FreemarkerObject.annotate: " + table.getName() + " - " + p.getName() + " - "
                + p.getNodeName() + " - " + p.getPropertyAccessorName());
    }

    return "";
}

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

License:Apache License

/**
 * Binds a root class (one with no super classes) to the runtime meta model
 * based on the supplied Grails domain class
 *
 * @param domainClass The Grails domain class
 * @param mappings    The Hibernate Mappings object
 * @param sessionFactoryBeanName  the session factory bean name
 *//*from  w w w  .j a  v a  2  s .  c  om*/
public void bindRoot(GrailsDomainClass domainClass, Mappings mappings, String sessionFactoryBeanName) {
    if (mappings.getClass(domainClass.getFullName()) != null) {
        LOG.info("[GrailsDomainBinder] Class [" + domainClass.getFullName()
                + "] is already mapped, skipping.. ");
        return;
    }

    RootClass root = new RootClass();
    root.setAbstract(Modifier.isAbstract(domainClass.getClazz().getModifiers()));
    if (!domainClass.hasSubClasses()) {
        root.setPolymorphic(false);
    }
    bindClass(domainClass, root, mappings);

    Mapping m = getMapping(domainClass);

    bindRootPersistentClassCommonValues(domainClass, root, mappings, sessionFactoryBeanName);

    if (!domainClass.getSubClasses().isEmpty()) {
        boolean tablePerSubclass = m != null && !m.getTablePerHierarchy();
        if (!tablePerSubclass) {
            // if the root class has children create a discriminator property
            bindDiscriminatorProperty(root.getTable(), root, mappings);
        }
        // bind the sub classes
        bindSubClasses(domainClass, root, mappings, sessionFactoryBeanName);
    }

    if (root.getEntityPersisterClass() == null) {
        root.setEntityPersisterClass(getGroovyAwareSingleTableEntityPersisterClass());
    }
    mappings.addClass(root);
}

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

License:Apache License

protected void bindRootPersistentClassCommonValues(GrailsDomainClass domainClass, RootClass root,
        Mappings mappings, String sessionFactoryBeanName) {

    // get the schema and catalog names from the configuration
    Mapping m = getMapping(domainClass.getClazz());
    String schema = mappings.getSchemaName();
    String catalog = mappings.getCatalogName();

    if (m != null) {
        configureDerivedProperties(domainClass, m);
        CacheConfig cc = m.getCache();//www  . j  a  va2 s.  c o  m
        if (cc != null && cc.getEnabled()) {
            root.setCacheConcurrencyStrategy(cc.getUsage());
            if ("read-only".equals(cc.getUsage())) {
                root.setMutable(false);
            }
            root.setLazyPropertiesCacheable(!"non-lazy".equals(cc.getInclude()));
        }

        Integer bs = m.getBatchSize();
        if (bs != null) {
            root.setBatchSize(bs);
        }

        if (m.getDynamicUpdate()) {
            root.setDynamicUpdate(true);
        }
        if (m.getDynamicInsert()) {
            root.setDynamicInsert(true);
        }
    }

    final boolean hasTableDefinition = m != null && m.getTable() != null;
    if (hasTableDefinition && m.getTable().getSchema() != null) {
        schema = m.getTable().getSchema();
    }
    if (hasTableDefinition && m.getTable().getCatalog() != null) {
        catalog = m.getTable().getCatalog();
    }

    final boolean isAbstract = m != null && !m.getTablePerHierarchy() && m.isTablePerConcreteClass()
            && root.isAbstract();
    // create the table
    Table table = mappings.addTable(schema, catalog, getTableName(domainClass, sessionFactoryBeanName), null,
            isAbstract);
    root.setTable(table);

    if (LOG.isDebugEnabled()) {
        LOG.debug("[GrailsDomainBinder] Mapping Grails domain class: " + domainClass.getFullName() + " -> "
                + root.getTable().getName());
    }

    bindIdentity(domainClass, root, mappings, m, sessionFactoryBeanName);

    if (m == null) {
        bindVersion(domainClass.getVersion(), root, mappings, sessionFactoryBeanName);
    } else {
        if (m.getVersioned()) {
            bindVersion(domainClass.getVersion(), root, mappings, sessionFactoryBeanName);
        }
    }

    root.createPrimaryKey();

    createClassProperties(domainClass, root, mappings, sessionFactoryBeanName);
}

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

License:Apache License

protected void bindCompositeId(GrailsDomainClass domainClass, RootClass root,
        CompositeIdentity compositeIdentity, Mappings mappings, String sessionFactoryBeanName) {
    Component id = new Component(mappings, root);
    id.setNullValue("undefined");
    root.setIdentifier(id);//from  ww w  .  ja  v a2s . c o  m
    root.setEmbeddedIdentifier(true);
    id.setComponentClassName(domainClass.getFullName());
    id.setKey(true);
    id.setEmbedded(true);

    String path = qualify(root.getEntityName(), "id");

    id.setRoleName(path);

    String[] props = compositeIdentity.getPropertyNames();
    for (String propName : props) {
        GrailsDomainClassProperty property = domainClass.getPropertyByName(propName);
        if (property == null) {
            throw new MappingException(
                    "Property [" + propName + "] referenced in composite-id mapping of class ["
                            + domainClass.getFullName() + "] is not a valid property!");
        }

        bindComponentProperty(id, null, property, root, "", root.getTable(), mappings, sessionFactoryBeanName);
    }
}

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

License:Apache License

protected void bindVersion(GrailsDomainClassProperty version, RootClass entity, Mappings mappings,
        String sessionFactoryBeanName) {

    SimpleValue val = new SimpleValue(mappings, entity.getTable());

    bindSimpleValue(version, null, val, EMPTY_PATH, mappings, sessionFactoryBeanName);

    if (val.isTypeSpecified()) {
        if (!(val.getType() instanceof IntegerType || val.getType() instanceof LongType
                || val.getType() instanceof TimestampType)) {
            LOG.warn("Invalid version class specified in " + version.getDomainClass().getClazz().getName()
                    + "; must be one of [int, Integer, long, Long, Timestamp, Date]. Not mapping the version.");
            return;
        }/*from   w w w .j ava2  s  . c  om*/
    } else {
        val.setTypeName("version".equals(version.getName()) ? "integer" : "timestamp");
    }
    Property prop = new Property();
    prop.setValue(val);

    bindProperty(version, prop, mappings);
    val.setNullValue("undefined");
    entity.setVersion(prop);
    entity.setOptimisticLockMode(0); // 0 is to use version column
    entity.addProperty(prop);
}

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

License:Apache License

@SuppressWarnings("unchecked")
protected void bindSimpleId(GrailsDomainClassProperty identifier, RootClass entity, Mappings mappings,
        Identity mappedId, String sessionFactoryBeanName) {

    Mapping mapping = getMapping(identifier.getDomainClass());
    boolean useSequence = mapping != null && mapping.isTablePerConcreteClass();

    // create the id value
    SimpleValue id = new SimpleValue(mappings, entity.getTable());
    // set identifier on entity

    Properties params = new Properties();
    entity.setIdentifier(id);/*from w  w  w.  j a va  2s  . c o  m*/

    if (mappedId == null) {
        // configure generator strategy
        id.setIdentifierGeneratorStrategy(useSequence ? "sequence-identity" : "native");
    } else {
        String generator = mappedId.getGenerator();
        if ("native".equals(generator) && useSequence) {
            generator = "sequence-identity";
        }
        id.setIdentifierGeneratorStrategy(generator);
        params.putAll(mappedId.getParams());
        if ("assigned".equals(generator)) {
            id.setNullValue("undefined");
        }
    }

    params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, mappings.getObjectNameNormalizer());

    if (mappings.getSchemaName() != null) {
        params.setProperty(PersistentIdentifierGenerator.SCHEMA, mappings.getSchemaName());
    }
    if (mappings.getCatalogName() != null) {
        params.setProperty(PersistentIdentifierGenerator.CATALOG, mappings.getCatalogName());
    }
    id.setIdentifierGeneratorProperties(params);

    // bind value
    bindSimpleValue(identifier, null, id, EMPTY_PATH, mappings, sessionFactoryBeanName);

    // create property
    Property prop = new Property();
    prop.setValue(id);

    // bind property
    bindProperty(identifier, prop, mappings);
    // set identifier property
    entity.setIdentifierProperty(prop);

    id.getTable().setIdentifierValue(id);
}

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

License:Apache License

/**
 * Binds a root class (one with no super classes) to the runtime meta model
 * based on the supplied Grails domain class
 *
 * @param domainClass The Grails domain class
 * @param mappings    The Hibernate Mappings object
 * @param sessionFactoryBeanName  the session factory bean name
 *//*from   ww  w.  j  av  a 2 s . co m*/
public static void bindRoot(GrailsDomainClass domainClass, Mappings mappings, String sessionFactoryBeanName) {
    if (mappings.getClass(domainClass.getFullName()) != null) {
        LOG.info("[GrailsDomainBinder] Class [" + domainClass.getFullName()
                + "] is already mapped, skipping.. ");
        return;
    }

    RootClass root = new RootClass();
    root.setAbstract(Modifier.isAbstract(domainClass.getClazz().getModifiers()));
    if (!domainClass.hasSubClasses()) {
        root.setPolymorphic(false);
    }
    bindClass(domainClass, root, mappings);

    Mapping m = getMapping(domainClass);

    bindRootPersistentClassCommonValues(domainClass, root, mappings, sessionFactoryBeanName);

    if (!domainClass.getSubClasses().isEmpty()) {
        boolean tablePerSubclass = m != null && !m.getTablePerHierarchy();
        if (!tablePerSubclass) {
            // if the root class has children create a discriminator property
            bindDiscriminatorProperty(root.getTable(), root, mappings);
        }
        // bind the sub classes
        bindSubClasses(domainClass, root, mappings, sessionFactoryBeanName);
    }

    if (root.getEntityPersisterClass() == null) {
        root.setEntityPersisterClass(GroovyAwareSingleTableEntityPersister.class);
    }
    mappings.addClass(root);
}

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

License:Apache License

private static void bindRootPersistentClassCommonValues(GrailsDomainClass domainClass, RootClass root,
        Mappings mappings, String sessionFactoryBeanName) {

    // get the schema and catalog names from the configuration
    Mapping m = getMapping(domainClass.getClazz());
    String schema = mappings.getSchemaName();
    String catalog = mappings.getCatalogName();

    if (m != null) {
        configureDerivedProperties(domainClass, m);
        CacheConfig cc = m.getCache();/*from w  w  w. ja  va  2s  .  co  m*/
        if (cc != null && cc.getEnabled()) {
            root.setCacheConcurrencyStrategy(cc.getUsage());
            if ("read-only".equals(cc.getUsage())) {
                root.setMutable(false);
            }
            root.setLazyPropertiesCacheable(!"non-lazy".equals(cc.getInclude()));
        }

        Integer bs = m.getBatchSize();
        if (bs != null) {
            root.setBatchSize(bs);
        }

        if (m.getDynamicUpdate()) {
            root.setDynamicUpdate(true);
        }
        if (m.getDynamicInsert()) {
            root.setDynamicInsert(true);
        }
    }

    final boolean hasTableDefinition = m != null && m.getTable() != null;
    if (hasTableDefinition && m.getTable().getSchema() != null) {
        schema = m.getTable().getSchema();
    }
    if (hasTableDefinition && m.getTable().getCatalog() != null) {
        catalog = m.getTable().getCatalog();
    }

    // create the table
    Table table = mappings.addTable(schema, catalog, getTableName(domainClass, sessionFactoryBeanName), null,
            false);
    root.setTable(table);

    if (LOG.isDebugEnabled()) {
        LOG.debug("[GrailsDomainBinder] Mapping Grails domain class: " + domainClass.getFullName() + " -> "
                + root.getTable().getName());
    }

    bindIdentity(domainClass, root, mappings, m, sessionFactoryBeanName);

    if (m != null) {
        if (m.getVersioned()) {
            bindVersion(domainClass.getVersion(), root, mappings, sessionFactoryBeanName);
        }
    } else {
        bindVersion(domainClass.getVersion(), root, mappings, sessionFactoryBeanName);
    }

    root.createPrimaryKey();

    createClassProperties(domainClass, root, mappings, sessionFactoryBeanName);
}