Example usage for org.hibernate.mapping SimpleValue setTypeName

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

Introduction

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

Prototype

public void setTypeName(String typeName) 

Source Link

Usage

From source file:com.javaetmoi.core.persistence.hibernate.TestReflectionUtil.java

License:Apache License

@Before
public void setUp() {
    Configuration configuration = new Configuration();
    Mappings mappings = configuration.createMappings();
    Component component = new Component(mappings, new RootClass());
    Property p1 = new Property();
    p1.setName("client");
    SimpleValue p1val = new SimpleValue(mappings);
    p1val.setTypeName("java.lang.Integer");
    p1.setValue(p1val);
    component.addProperty(p1);//from w w w  . j  a v  a 2s.  c  o  m
    Property p2 = new Property();
    p2.setName("user");
    SimpleValue p2val = new SimpleValue(mappings);
    p2val.setTypeName("java.lang.String");
    p2.setValue(p2val);
    component.addProperty(p2);
    metadata = new ComponentMetamodel(component);
}

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

License:Open Source License

public boolean setHibernateType(@Nullable SimpleValue value,
        com.manydesigns.portofino.model.database.Column column, Class javaType, final int jdbcType) {
    String typeName;//from w  w w . ja v a 2s.  co  m
    Properties typeParams = null;
    if (javaType == null) {
        return false;
    }
    if (javaType == Long.class) {
        typeName = LongType.INSTANCE.getName();
    } else if (javaType == Short.class) {
        typeName = ShortType.INSTANCE.getName();
    } else if (javaType == Integer.class) {
        typeName = IntegerType.INSTANCE.getName();
    } else if (javaType == Byte.class) {
        typeName = ByteType.INSTANCE.getName();
    } else if (javaType == Float.class) {
        typeName = FloatType.INSTANCE.getName();
    } else if (javaType == Double.class) {
        typeName = DoubleType.INSTANCE.getName();
    } else if (javaType == Character.class) {
        typeName = CharacterType.INSTANCE.getName();
    } else if (javaType == String.class) {
        typeName = StringType.INSTANCE.getName();
    } else if (java.util.Date.class.isAssignableFrom(javaType)) {
        switch (jdbcType) {
        case Types.DATE:
            typeName = DateType.INSTANCE.getName();
            break;
        case Types.TIME:
            typeName = TimeType.INSTANCE.getName();
            break;
        case Types.TIMESTAMP:
            typeName = TimestampType.INSTANCE.getName();
            break;
        default:
            typeName = null;
        }
    } else if (javaType == Boolean.class) {
        if (jdbcType == Types.BIT || jdbcType == Types.BOOLEAN) {
            typeName = BooleanType.INSTANCE.getName();
        } else if (jdbcType == Types.NUMERIC || jdbcType == Types.DECIMAL || jdbcType == Types.INTEGER
                || jdbcType == Types.SMALLINT || jdbcType == Types.TINYINT || jdbcType == Types.BIGINT) {
            typeName = NumericBooleanType.INSTANCE.getName();
        } else if (jdbcType == Types.CHAR || jdbcType == Types.VARCHAR) {
            typeName = StringBooleanType.class.getName();
            typeParams = new Properties();
            typeParams.setProperty("true", trueString != null ? trueString : StringBooleanType.NULL);
            typeParams.setProperty("false", falseString != null ? falseString : StringBooleanType.NULL);
            typeParams.setProperty("sqlType", String.valueOf(jdbcType));
        } else {
            typeName = null;
        }
    } else if (javaType == BigDecimal.class) {
        typeName = BigDecimalType.INSTANCE.getName();
    } else if (javaType == BigInteger.class) {
        typeName = BigIntegerType.INSTANCE.getName();
    } else if (javaType == byte[].class) {
        typeName = BlobType.INSTANCE.getName();
    } else {
        typeName = null;
    }

    if (typeName == null) {
        logger.error("Unsupported type (java type: {}, jdbc type: {}) " + "for column '{}'.",
                new Object[] { javaType, jdbcType, column.getColumnName() });
        return false;
    }

    if (value != null) {
        value.setTypeName(typeName);
        if (typeParams != null) {
            value.setTypeParameters(typeParams);
        }
    }
    return true;
}

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);/*  w  ww.  j  ava  2 s.  com*/
    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);
        }//from   w w  w .j a v  a  2  s. c  om

        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()));
    }//from w  w  w. j  a v  a 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  a2s .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

/**
 * 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
 *//* w w  w  . j a va  2  s.c o  m*/
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

protected void bindEnumType(GrailsDomainClassProperty property, Class<?> propertyType, SimpleValue simpleValue,
        String columnName) {//from   w  w w  .j av a2 s  .  c o  m

    PropertyConfig pc = getPropertyConfig(property);
    String typeName = getTypeName(property, getPropertyConfig(property), getMapping(property.getDomainClass()));
    if (typeName == null) {
        Properties enumProperties = new Properties();
        enumProperties.put(ENUM_CLASS_PROP, propertyType.getName());

        String enumType = pc == null ? DEFAULT_ENUM_TYPE : pc.getEnumType();
        if (enumType.equals(DEFAULT_ENUM_TYPE) && identityEnumTypeSupports(propertyType)) {
            simpleValue.setTypeName("org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType");
        } else {
            simpleValue.setTypeName(ENUM_TYPE_CLASS);
            if (enumType.equals(DEFAULT_ENUM_TYPE) || "string".equalsIgnoreCase(enumType)) {
                enumProperties.put(ENUM_TYPE_PROP, String.valueOf(Types.VARCHAR));
            } else if (!"ordinal".equalsIgnoreCase(enumType)) {
                LOG.warn("Invalid enumType specified when mapping property [" + property.getName()
                        + "] of class [" + property.getDomainClass().getClazz().getName()
                        + "]. Using defaults instead.");
            }
        }
        simpleValue.setTypeParameters(enumProperties);
    } else {
        simpleValue.setTypeName(typeName);
    }

    Table t = simpleValue.getTable();
    Column column = new Column();

    if (property.getDomainClass().isRoot()) {
        column.setNullable(property.isOptional());
    } else {
        Mapping mapping = getMapping(property.getDomainClass());
        if (mapping == null || mapping.getTablePerHierarchy()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("[GrailsDomainBinder] Sub class property [" + property.getName()
                        + "] for column name [" + column.getName() + "] set to nullable");
            }
            column.setNullable(true);
        } else {
            column.setNullable(property.isOptional());
        }
    }
    column.setValue(simpleValue);
    column.setName(columnName);
    if (t != null)
        t.addColumn(column);

    simpleValue.addColumn(column);

    PropertyConfig propertyConfig = getPropertyConfig(property);
    if (propertyConfig != null && !propertyConfig.getColumns().isEmpty()) {
        bindIndex(columnName, column, propertyConfig.getColumns().get(0), t);
        bindColumnConfigToColumn(column, propertyConfig.getColumns().get(0));
    }
}

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;
        }/* www .  java  2 s  .c o  m*/
    } 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

protected void setTypeForPropertyConfig(GrailsDomainClassProperty grailsProp, SimpleValue simpleValue,
        PropertyConfig config) {/* w  ww  .ja v a2  s .  c o  m*/
    final String typeName = getTypeName(grailsProp, getPropertyConfig(grailsProp),
            getMapping(grailsProp.getDomainClass()));
    if (typeName == null) {
        simpleValue.setTypeName(grailsProp.getType().getName());
    } else {
        simpleValue.setTypeName(typeName);
        if (config != null) {
            simpleValue.setTypeParameters(config.getTypeParams());
        }
    }
}