Example usage for org.hibernate.mapping Column setNullable

List of usage examples for org.hibernate.mapping Column setNullable

Introduction

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

Prototype

public void setNullable(boolean nullable) 

Source Link

Usage

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

License:Apache License

protected void bindEnumType(PersistentProperty property, Class<?> propertyType, SimpleValue simpleValue,
        String columnName) {/*www  .  ja v a2 s.co m*/

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

        String enumType = pc == null ? DEFAULT_ENUM_TYPE : pc.getEnumType();
        boolean isDefaultEnumType = enumType.equals(DEFAULT_ENUM_TYPE);
        simpleValue.setTypeName(ENUM_TYPE_CLASS);
        if (isDefaultEnumType || "string".equalsIgnoreCase(enumType)) {
            enumProperties.put(EnumType.TYPE, String.valueOf(Types.VARCHAR));
            enumProperties.put(EnumType.NAMED, Boolean.TRUE.toString());
        } else if ("identity".equals(enumType)) {
            simpleValue.setTypeName(HibernateUtils.buildIdentityEnumTypeFactory().getName());
        } else if (!"ordinal".equalsIgnoreCase(enumType)) {
            simpleValue.setTypeName(enumType);
        }
        simpleValue.setTypeParameters(enumProperties);
    } else {
        simpleValue.setTypeName(typeName);
    }

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

    if (owner.isRoot()) {
        column.setNullable(property.isNullable());
    } else {
        Mapping mapping = getMapping(owner);
        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.isNullable());
        }
    }
    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(property, column, propertyConfig.getColumns().get(0));
    }
}

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;/*from  w w w.j  a v a  2s  .c o  m*/
    // 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;
    }
    // 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

/**
 * Binds a value for the specified parameters to the meta model.
 *
 * @param type        The type of the property
 * @param simpleValue The simple value instance
 * @param nullable    Whether it is nullable
 * @param columnName  The property name//from   w ww.j a  v  a2s.c o  m
 * @param mappings    The mappings
 */
protected void bindSimpleValue(String type, SimpleValue simpleValue, boolean nullable, String columnName,
        InFlightMetadataCollector mappings) {

    simpleValue.setTypeName(type);
    Table t = simpleValue.getTable();
    Column column = new Column();
    column.setNullable(nullable);
    column.setValue(simpleValue);
    column.setName(columnName);
    if (t != null)
        t.addColumn(column);

    simpleValue.addColumn(column);
}

From source file:org.openflexo.technologyadapter.jdbc.model.DynamicModelBuilder.java

License:Open Source License

public Metadata buildDynamicModel() {
    Metadata metadata = metadataCollector.buildMetadataInstance(metadataBuildingContext);

    Database database = metadata.getDatabase();

    // **********
    // Creation / Dfinition de la table T_Dynamic_Table

    Table table = metadataCollector.addTable("", "", "T_Dynamic_Table", null, false);
    table.setName("T_Dynamic_Table");
    Column col = new Column();
    col.setName("pouet");
    col.setLength(256);//from   w  w w  .j a va 2  s.  c  o  m
    col.setSqlType("CHAR(256)");
    col.setNullable(false);
    table.addColumn(col);

    PrimaryKey pk = new PrimaryKey(table);
    pk.addColumn(col);

    UniqueKey uk1 = new UniqueKey();
    uk1.setName("Nom_Unique");
    uk1.setTable(table);
    uk1.addColumn(col);
    table.addUniqueKey(uk1);

    Column col2 = new Column();
    col2.setName("padam");
    col2.setLength(256);
    col2.setSqlType("CHAR(256)");
    col2.setNullable(true);
    table.addColumn(col2);
    // pour rire les couples "Nom + Prenom" doivent tre uniques
    UniqueKey uk = new UniqueKey();
    uk.setName("Couple_Nom_Prenom_Unique");
    uk.setTable(table);
    uk.addColumn(col);
    uk.addColumn(col2);
    table.addUniqueKey(uk);

    // une colonne de clef etrangre vers T_Adresse
    Column col3 = new Column();
    col3.setName("id_addr");
    col3.setLength(16);
    col3.setSqlType("INTEGER");
    col3.setNullable(true);
    table.addColumn(col3);

    // **********
    // Creation / Dfinition de la table T_Adresse
    Table table2 = metadataCollector.addTable("", "", "T_Adresse", null, false);
    table2.setName("T_Adresse");
    Column col4 = new Column();
    col4.setName("Id");
    col4.setLength(16);
    col4.setSqlType("INTEGER");
    col4.setNullable(false);
    table2.addColumn(col4);

    pk = new PrimaryKey(table2);
    pk.addColumn(col);

    uk1 = new UniqueKey();
    uk1.setName("Id_Unique");
    uk1.setTable(table2);
    uk1.addColumn(col4);
    table.addUniqueKey(uk1);

    Column col5 = new Column();
    col5.setName("Adresse");
    col5.setLength(512);
    col5.setSqlType("CHAR(512)");
    col5.setNullable(true);
    table2.addColumn(col5);

    // ************************
    // Creation de l'entit persiste "Dynamic_Class"

    RootClass pClass = new RootClass(metadataBuildingContext);
    pClass.setEntityName("Dynamic_Class");
    pClass.setJpaEntityName("Dynamic_Class");
    pClass.setTable(table);
    metadataCollector.addEntityBinding(pClass);

    // Creation d'une proprit (clef) et son mapping

    Property prop = new Property();
    prop.setName("Nom");
    SimpleValue value = new SimpleValue((MetadataImplementor) metadata, table);
    value.setTypeName("java.lang.String");
    value.setIdentifierGeneratorStrategy("assigned");
    value.addColumn(col);
    value.setTable(table);
    prop.setValue(value);
    pClass.setDeclaredIdentifierProperty(prop);
    pClass.setIdentifierProperty(prop);
    pClass.setIdentifier(value);

    // Creation d'une proprit et son mapping

    prop = new Property();
    prop.setName("Prenom");
    value = new SimpleValue((MetadataImplementor) metadata, table);
    value.setTypeName(String.class.getCanonicalName());
    value.addColumn(col2);
    value.setTable(table);
    prop.setValue(value);
    pClass.addProperty(prop);

    // ************************
    // Creation de l'entit persiste "Adresse"

    RootClass pClass2 = new RootClass(metadataBuildingContext);
    pClass2.setEntityName("Adresse");
    pClass2.setJpaEntityName("Adresse");
    pClass2.setTable(table2);
    metadataCollector.addEntityBinding(pClass2);

    // Creation d'une proprit (clef) et son mapping

    prop = new Property();
    prop.setName("Identifiant");
    value = new SimpleValue((MetadataImplementor) metadata, table2);
    value.setTypeName("java.lang.Integer");
    value.setIdentifierGeneratorStrategy("native");
    value.addColumn(col4);
    value.setTable(table2);
    prop.setValue(value);
    pClass2.setDeclaredIdentifierProperty(prop);
    pClass2.setIdentifierProperty(prop);
    pClass2.setIdentifier(value);

    // Creation d'une proprit et son mapping

    prop = new Property();
    prop.setName("Prenom");
    value = new SimpleValue((MetadataImplementor) metadata, table2);
    value.setTypeName(String.class.getCanonicalName());
    value.addColumn(col5);
    value.setTable(table2);
    prop.setValue(value);
    pClass2.addProperty(prop);

    try {
        ((MetadataImplementor) metadata).validate();
    } catch (MappingException e) {
        System.out.println("Validation Error: " + e.getMessage());
    }

    return metadata;

}