Example usage for org.hibernate.mapping SimpleValue SimpleValue

List of usage examples for org.hibernate.mapping SimpleValue SimpleValue

Introduction

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

Prototype

public SimpleValue(MetadataBuildingContext buildingContext, Table table) 

Source Link

Usage

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

License:Open Source License

protected Column createColumn(Mappings mappings, Table tab,
        com.manydesigns.portofino.model.database.Column column) {
    Column col = new Column();
    col.setName(escapeName(column.getColumnName()));
    col.setLength(column.getLength());//from  w w  w  . j a  v  a 2 s.  c o m
    col.setPrecision(column.getLength());
    col.setScale(column.getScale());
    col.setNullable(column.isNullable());
    String columnType = column.getColumnType();
    int jdbcType = column.getJdbcType();

    col.setSqlTypeCode(jdbcType);
    col.setSqlType(columnType);

    SimpleValue value = new SimpleValue(mappings, tab);
    if (!setHibernateType(value, column, column.getActualJavaType(), jdbcType)) {
        logger.error("Skipping column {}", column.getQualifiedName());
        return null;
    }

    value.addColumn(col);
    tab.addColumn(col);
    mappings.addColumnBinding(column.getColumnName(), col, tab);

    return col;
}

From source file:com.zutubi.pulse.master.transfer.jdbc.HibernateUniqueKeyTable.java

License:Apache License

public static Table getMapping() {
    Table table = new Table("hibernate_unique_key");
    Column column = new Column("next_hi");
    SimpleValue value = new SimpleValue(null, table);
    value.setTypeName(int.class.getName());
    column.setValue(value);//from w  ww.j  a  v a2  s. c  o m
    column.setSqlTypeCode(Types.INTEGER);
    table.addColumn(column);
    return table;
}

From source file:org.apereo.portal.tools.dbloader.TableXmlHandler.java

License:Apache License

@Override
public void endElement(String uri, String localName, String name) throws SAXException {
    if ("table".equals(name)) {
        for (final Column column : this.currentColumns.values()) {
            this.currentTable.addColumn(column);
        }//  w ww. j av a 2  s .co  m

        if (this.primaryKey != null) {
            this.currentTable.setPrimaryKey(this.primaryKey);
        }

        this.tables.put(this.currentTable.getName(), this.currentTable);
        this.tableColumnTypes.put(this.currentTable.getName(), this.currentColumnTypes);
        this.primaryKey = null;
        this.currentColumns = null;
        this.currentColumnTypes = null;
        this.currentTable = null;
    } else if ("column".equals(name)) {
        this.currentColumns.put(this.currentColumn.getName(), this.currentColumn);
        this.currentColumn = null;
    } else if ("name".equals(name)) {
        final String itemName = this.chars.toString().trim();

        if (this.currentIndex != null) {
            this.currentIndex.setName(itemName);
        } else if (this.currentUnique != null) {
            this.currentUnique.setName(itemName);
        } else if (this.currentTable == null) {
            this.currentTable = new Table(itemName);
        } else if (this.currentColumn == null) {
            this.currentColumn = new Column(itemName);
        }
    } else if ("type".equals(name)) {
        final String sqlTypeName = this.chars.toString().trim();

        final int sqlType = this.getSqlType(sqlTypeName);
        this.currentColumnTypes.put(this.currentColumn.getName(), sqlType);

        final String hibType = this.getHibernateType(sqlType);

        final SimpleValue value = new SimpleValue(this.mappings, this.currentTable);
        value.setTypeName(hibType);

        this.currentColumn.setValue(value);
    } else if ("param".equals(name)) {
        final String param = this.chars.toString().trim();

        final Integer length = Integer.valueOf(param);
        this.currentColumn.setLength(length);
    } else if ("primary-key".equals(name)) {
        final String columnName = this.chars.toString().trim();

        if (this.primaryKey == null) {
            this.primaryKey = new PrimaryKey();
        }

        final Column column = this.currentColumns.get(columnName);
        this.primaryKey.addColumn(column);
    } else if ("not-null".equals(name)) {
        final String columnName = this.chars.toString().trim();
        final Column column = this.currentColumns.get(columnName);
        column.setNullable(false);
    } else if ("column-ref".equals(name)) {
        final String columnName = this.chars.toString().trim();
        final Column column = this.currentColumns.get(columnName);

        if (this.currentIndex != null) {
            this.currentIndex.addColumn(column);
        } else if (this.currentUnique != null) {
            this.currentUnique.addColumn(column);
        }
    } else if ("index".equals(name)) {
        this.currentTable.addIndex(this.currentIndex);
        this.currentIndex = null;
    } else if ("unique".equals(name)) {
        this.currentTable.addUniqueKey(this.currentUnique);
        this.currentUnique = null;
    } else if ("key".equals(name)) {
        this.logger.warn("the 'key' element is ignored, use the table level 'primary-key' element instead");
    }

    this.chars = null;
}

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

License:Apache License

protected void bindMapSecondPass(GrailsDomainClassProperty property, Mappings mappings,
        Map<?, ?> persistentClasses, org.hibernate.mapping.Map map, String sessionFactoryBeanName) {
    bindCollectionSecondPass(property, mappings, persistentClasses, map, sessionFactoryBeanName);

    SimpleValue value = new SimpleValue(mappings, map.getCollectionTable());

    bindSimpleValue(getIndexColumnType(property, STRING_TYPE), value, true,
            getIndexColumnName(property, sessionFactoryBeanName), mappings);
    PropertyConfig pc = getPropertyConfig(property);
    if (pc != null && pc.getIndexColumn() != null) {
        bindColumnConfigToColumn(getColumnForSimpleValue(value), getSingleColumnConfig(pc.getIndexColumn()));
    }//  w  w w .j a va 2 s. c  om

    if (!value.isTypeSpecified()) {
        throw new MappingException("map index element must specify a type: " + map.getRole());
    }
    map.setIndex(value);

    if (!property.isOneToMany() && !property.isManyToMany()) {
        SimpleValue elt = new SimpleValue(mappings, map.getCollectionTable());
        map.setElement(elt);

        String typeName = getTypeName(property, getPropertyConfig(property),
                getMapping(property.getDomainClass()));
        if (typeName == null) {
            if (property.isBasicCollectionType()) {
                typeName = property.getReferencedPropertyType().getName();
            } else {
                typeName = StandardBasicTypes.STRING.getName();
            }
        }
        bindSimpleValue(typeName, elt, false, getMapElementName(property, sessionFactoryBeanName), mappings);

        elt.setTypeName(typeName);

        map.setInverse(false);
    } else {
        map.setInverse(false);
    }
}

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

License:Apache License

protected void bindListSecondPass(GrailsDomainClassProperty property, Mappings mappings,
        Map<?, ?> persistentClasses, org.hibernate.mapping.List list, String sessionFactoryBeanName) {

    bindCollectionSecondPass(property, mappings, persistentClasses, list, sessionFactoryBeanName);

    String columnName = getIndexColumnName(property, sessionFactoryBeanName);
    final boolean isManyToMany = property.isManyToMany();

    if (isManyToMany && !property.isOwningSide()) {
        throw new MappingException("Invalid association [" + property.getDomainClass().getName() + "->"
                + property.getName()//from   w  w w.  j  a v  a2  s .  co  m
                + "]. List collection types only supported on the owning side of a many-to-many relationship.");
    }

    Table collectionTable = list.getCollectionTable();
    SimpleValue iv = new SimpleValue(mappings, collectionTable);
    bindSimpleValue("integer", iv, true, columnName, mappings);
    iv.setTypeName("integer");
    list.setIndex(iv);
    list.setBaseIndex(0);
    list.setInverse(false);

    Value v = list.getElement();
    v.createForeignKey();

    if (property.isBidirectional()) {

        String entityName;
        Value element = list.getElement();
        if (element instanceof ManyToOne) {
            ManyToOne manyToOne = (ManyToOne) element;
            entityName = manyToOne.getReferencedEntityName();
        } else {
            entityName = ((OneToMany) element).getReferencedEntityName();
        }

        PersistentClass referenced = mappings.getClass(entityName);

        Class<?> mappedClass = referenced.getMappedClass();
        Mapping m = getMapping(mappedClass);

        boolean compositeIdProperty = isCompositeIdProperty(m, property.getOtherSide());
        if (!compositeIdProperty) {
            Backref prop = new Backref();
            prop.setEntityName(property.getDomainClass().getFullName());
            prop.setName(UNDERSCORE
                    + addUnderscore(property.getDomainClass().getShortName(), property.getName()) + "Backref");
            prop.setSelectable(false);
            prop.setUpdateable(false);
            if (isManyToMany) {
                prop.setInsertable(false);
            }
            prop.setCollectionRole(list.getRole());
            prop.setValue(list.getKey());

            DependantValue value = (DependantValue) prop.getValue();
            if (!property.isCircular()) {
                value.setNullable(false);
            }
            value.setUpdateable(true);
            prop.setOptional(false);

            referenced.addProperty(prop);
        }

        if ((!list.getKey().isNullable() && !list.isInverse()) || compositeIdProperty) {
            IndexBackref ib = new IndexBackref();
            ib.setName(UNDERSCORE + property.getName() + "IndexBackref");
            ib.setUpdateable(false);
            ib.setSelectable(false);
            if (isManyToMany) {
                ib.setInsertable(false);
            }
            ib.setCollectionRole(list.getRole());
            ib.setEntityName(list.getOwner().getEntityName());
            ib.setValue(list.getIndex());
            referenced.addProperty(ib);
        }
    }
}

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

License:Apache License

protected void bindCollectionWithJoinTable(GrailsDomainClassProperty property, Mappings mappings,
        Collection collection, PropertyConfig config, String sessionFactoryBeanName) {

    NamingStrategy namingStrategy = getNamingStrategy(sessionFactoryBeanName);

    SimpleValue element;/*from  w  w  w. j  av  a 2  s  . co  m*/
    if (property.isBasicCollectionType()) {
        element = new SimpleValue(mappings, collection.getCollectionTable());
    } else {
        // for a normal unidirectional one-to-many we use a join column
        element = new ManyToOne(mappings, collection.getCollectionTable());
        bindUnidirectionalOneToManyInverseValues(property, (ManyToOne) element);
    }
    collection.setInverse(false);

    String columnName;

    final boolean hasJoinColumnMapping = hasJoinColumnMapping(config);
    if (property.isBasicCollectionType()) {
        final Class<?> referencedType = property.getReferencedPropertyType();
        String className = referencedType.getName();
        final boolean isEnum = referencedType.isEnum();
        if (hasJoinColumnMapping) {
            columnName = config.getJoinTable().getColumn().getName();
        } else {
            columnName = isEnum ? namingStrategy.propertyToColumnName(className)
                    : addUnderscore(namingStrategy.propertyToColumnName(property.getName()),
                            namingStrategy.propertyToColumnName(className));
        }

        if (isEnum) {
            bindEnumType(property, referencedType, element, columnName);
        } else {

            String typeName = getTypeName(property, config, getMapping(property.getDomainClass()));
            if (typeName == null) {
                Type type = mappings.getTypeResolver().basic(className);
                if (type != null) {
                    typeName = type.getName();
                }
            }
            if (typeName == null) {
                String domainName = property.getDomainClass().getName();
                throw new MappingException("Missing type or column for column[" + columnName + "] on domain["
                        + domainName + "] referencing[" + className + "]");
            }

            bindSimpleValue(typeName, element, true, columnName, mappings);
            if (hasJoinColumnMapping) {
                bindColumnConfigToColumn(getColumnForSimpleValue(element), config.getJoinTable().getColumn());
            }
        }
    } else {
        final GrailsDomainClass domainClass = property.getReferencedDomainClass();

        Mapping m = getMapping(domainClass.getClazz());
        if (hasCompositeIdentifier(m)) {
            CompositeIdentity ci = (CompositeIdentity) m.getIdentity();
            bindCompositeIdentifierToManyToOne(property, element, ci, domainClass, EMPTY_PATH,
                    sessionFactoryBeanName);
        } else {
            if (hasJoinColumnMapping) {
                columnName = config.getJoinTable().getColumn().getName();
            } else {
                columnName = namingStrategy.propertyToColumnName(domainClass.getPropertyName())
                        + FOREIGN_KEY_SUFFIX;
            }

            bindSimpleValue("long", element, true, columnName, mappings);
        }
    }

    collection.setElement(element);

    bindCollectionForPropertyConfig(collection, config);
}

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

License:Apache License

/**
 * Creates and binds the discriminator property used in table-per-hierarchy inheritance to
 * discriminate between sub class instances
 *
 * @param table    The table to bind onto
 * @param entity   The root class entity
 * @param mappings The mappings instance
 *//*from  w ww.  j  a v  a2  s .  c om*/
protected void bindDiscriminatorProperty(Table table, RootClass entity, Mappings mappings) {
    Mapping m = getMapping(entity.getMappedClass());
    SimpleValue d = new SimpleValue(mappings, table);
    entity.setDiscriminator(d);
    entity.setDiscriminatorValue(
            m != null && m.getDiscriminator() != null ? m.getDiscriminator() : entity.getClassName());

    if (m != null && m.getDiscriminatorMap().get("insert") != null) {
        entity.setDiscriminatorInsertable((Boolean) m.getDiscriminatorMap().get("insert"));
    }
    if (m != null && m.getDiscriminatorMap().get("type") != null) {
        d.setTypeName((String) m.getDiscriminatorMap().get("type"));
    }

    if (m != null && m.getDiscriminatorMap().get("formula") != null) {
        Formula formula = new Formula();
        formula.setFormula((String) m.getDiscriminatorMap().get("formula"));
        d.addFormula(formula);
    } else {
        bindSimpleValue(STRING_TYPE, d, false, RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME, mappings);

        ColumnConfig cc = m == null ? null : m.getDiscriminatorColumn();
        if (cc != null) {
            Column c = (Column) d.getColumnIterator().next();
            if (cc.getName() != null) {
                c.setName(cc.getName());
            }
            bindColumnConfigToColumn(c, cc);
        }
    }

    entity.setPolymorphic(true);
}

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

License:Apache License

/**
 * Creates and binds the properties for the specified Grails domain class and PersistentClass
 * and binds them to the Hibernate runtime meta model
 *
 * @param domainClass     The Grails domain class
 * @param persistentClass The Hibernate PersistentClass instance
 * @param mappings        The Hibernate Mappings instance
 * @param sessionFactoryBeanName  the session factory bean name
 *///from  ww w . j  ava 2s .c  om
protected void createClassProperties(GrailsDomainClass domainClass, PersistentClass persistentClass,
        Mappings mappings, String sessionFactoryBeanName) {

    GrailsDomainClassProperty[] persistentProperties = domainClass.getPersistentProperties();
    Table table = persistentClass.getTable();

    Mapping gormMapping = getMapping(domainClass.getClazz());

    if (gormMapping != null) {
        table.setComment(gormMapping.getComment());
    }

    for (GrailsDomainClassProperty currentGrailsProp : persistentProperties) {

        // if its inherited skip
        boolean isBidirectionalManyToOne = isBidirectionalManyToOne(currentGrailsProp);
        if (currentGrailsProp.isInherited()) {
            continue;
        }
        if (currentGrailsProp.isInherited() && isBidirectionalManyToOne && currentGrailsProp.isCircular()) {
            continue;
        }
        if (isCompositeIdProperty(gormMapping, currentGrailsProp))
            continue;
        if (isIdentityProperty(gormMapping, currentGrailsProp))
            continue;

        if (LOG.isDebugEnabled()) {
            LOG.debug("[GrailsDomainBinder] Binding persistent property [" + currentGrailsProp.getName() + "]");
        }

        Value value = null;

        // see if it's a collection type
        CollectionType collectionType = CT.collectionTypeForClass(currentGrailsProp.getType());

        Class<?> userType = getUserType(currentGrailsProp);

        if (userType != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                        + "] as SimpleValue");
            }
            value = new SimpleValue(mappings, table);
            bindSimpleValue(currentGrailsProp, null, (SimpleValue) value, EMPTY_PATH, mappings,
                    sessionFactoryBeanName);
        } else if (collectionType != null) {
            String typeName = getTypeName(currentGrailsProp, getPropertyConfig(currentGrailsProp), gormMapping);
            if ("serializable".equals(typeName)) {
                value = new SimpleValue(mappings, table);
                bindSimpleValue(typeName, (SimpleValue) value, currentGrailsProp.isOptional(),
                        getColumnNameForPropertyAndPath(currentGrailsProp, EMPTY_PATH, null,
                                sessionFactoryBeanName),
                        mappings);
            } else {
                // create collection
                Collection collection = collectionType.create(currentGrailsProp, persistentClass, EMPTY_PATH,
                        mappings, sessionFactoryBeanName);
                mappings.addCollection(collection);
                value = collection;
            }
        } else if (currentGrailsProp.isEnum()) {
            value = new SimpleValue(mappings, table);
            bindEnumType(currentGrailsProp, (SimpleValue) value, EMPTY_PATH, sessionFactoryBeanName);
        }
        // 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, EMPTY_PATH, mappings, sessionFactoryBeanName);
        } else if (currentGrailsProp.isOneToOne() && userType == null) {
            if (LOG.isDebugEnabled())
                LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                        + "] as OneToOne");

            if (currentGrailsProp.isHasOne() && !currentGrailsProp.isBidirectional()) {
                throw new MappingException("hasOne property [" + currentGrailsProp.getDomainClass().getName()
                        + "." + currentGrailsProp.getName()
                        + "] is not bidirectional. Specify the other side of the relationship!");
            } else if (canBindOneToOneWithSingleColumnAndForeignKey(currentGrailsProp)) {
                value = new OneToOne(mappings, table, persistentClass);
                bindOneToOne(currentGrailsProp, (OneToOne) value, EMPTY_PATH, sessionFactoryBeanName);
            } else {
                if (currentGrailsProp.isHasOne() && currentGrailsProp.isBidirectional()) {
                    value = new OneToOne(mappings, table, persistentClass);
                    bindOneToOne(currentGrailsProp, (OneToOne) value, EMPTY_PATH, sessionFactoryBeanName);
                } else {
                    value = new ManyToOne(mappings, table);
                    bindManyToOne(currentGrailsProp, (ManyToOne) value, EMPTY_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);
            bindSimpleValue(currentGrailsProp, null, (SimpleValue) value, EMPTY_PATH, mappings,
                    sessionFactoryBeanName);
        }

        if (value != null) {
            Property property = createProperty(value, persistentClass, currentGrailsProp, mappings);
            persistentClass.addProperty(property);
        }
    }

    bindNaturalIdentifier(table, gormMapping, persistentClass);
}

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;//from  w  ww  .  j  av  a  2  s  .co  m
    // 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;
    }
    // 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.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;
        }// w w w .j  av a  2 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);
}