Example usage for org.hibernate.mapping DependantValue setNullable

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

Introduction

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

Prototype

public void setNullable(boolean nullable) 

Source Link

Usage

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

License:Open Source License

protected void createO2M(Configuration config, Mappings mappings, ForeignKey relationship) {

    com.manydesigns.portofino.model.database.Table manyMDTable = relationship.getFromTable();
    com.manydesigns.portofino.model.database.Table oneMDTable = relationship.getToTable();

    //Se la classe One non e' dinamica e
    // non ha la proprieta' non inserisco la relazione
    if (oneMDTable.getJavaClass() != null) {
        try {//from  w  w  w  . j  av  a2 s. c o  m
            Class oneClass = oneMDTable.getActualJavaClass();
            JavaClassAccessor accessor = JavaClassAccessor.getClassAccessor(oneClass);
            PropertyAccessor[] propertyAccessors = accessor.getProperties();
            boolean found = false;
            for (PropertyAccessor propertyAccessor : propertyAccessors) {
                if (propertyAccessor.getName().equals(relationship.getActualManyPropertyName())) {
                    found = true;
                }
            }
            if (!found) {
                logger.warn("Property '{}' not found, skipping relationship {}",
                        relationship.getActualManyPropertyName(), relationship.getQualifiedName());
                return;
            }
        } catch (Exception e) {
            //se non c'e' non inserisco la relazione
            logger.warn("Property not found, skipping relationship ", e);
            return;
        }
    }
    //relazione virtuali fra Database differenti
    if (!manyMDTable.getDatabaseName().equalsIgnoreCase(oneMDTable.getDatabaseName())) {
        logger.warn("Relationship crosses databases, skipping: {}", relationship.getQualifiedName());
        return;
    }

    String manyMDQualifiedTableName = manyMDTable.getActualEntityName();
    String oneMDQualifiedTableName = oneMDTable.getActualEntityName();

    PersistentClass clazzOne = config.getClassMapping(oneMDQualifiedTableName);
    if (clazzOne == null) {
        logger.error("Cannot find table '{}' as 'one' side of foreign key '{}'. Skipping relationship.",
                oneMDQualifiedTableName, relationship.getName());
        return;
    }

    PersistentClass clazzMany = config.getClassMapping(manyMDQualifiedTableName);
    if (clazzMany == null) {
        logger.error("Cannot find table '{}' as 'many' side of foreign key '{}'. Skipping relationship.",
                manyMDQualifiedTableName, relationship.getName());
        return;
    }

    //Uso i Bag perche' i set non funzionano con i componenti dinamici
    Bag set = new Bag(mappings, clazzOne);
    // Mettere Lazy in debug a false per ottenere subito eventuali errori
    // nelle relazioni
    set.setLazy(LAZY);

    set.setRole(
            relationship.getToTable().getActualEntityName() + "." + relationship.getActualManyPropertyName());
    //set.setNodeName(relationship.getActualManyPropertyName());
    set.setCollectionTable(clazzMany.getTable());
    OneToMany oneToMany = new OneToMany(mappings, set.getOwner());
    set.setElement(oneToMany);

    oneToMany.setReferencedEntityName(manyMDQualifiedTableName);

    oneToMany.setAssociatedClass(clazzMany);
    oneToMany.setEmbedded(true);

    set.setSorted(false);
    set.setFetchMode(FetchMode.DEFAULT);
    //Riferimenti alle colonne

    DependantValue dv;
    Table tableMany = clazzMany.getTable();
    Table tableOne = clazzOne.getTable();
    List<Column> oneColumns = new ArrayList<Column>();
    List<Column> manyColumns = new ArrayList<Column>();
    //Chiave multipla
    final List<Reference> refs = relationship.getReferences();
    if (refs.size() > 1) {
        dv = createFKComposite(mappings, relationship, manyMDTable, clazzOne, clazzMany, set, tableMany,
                tableOne, oneColumns, manyColumns);
    } else { //chiave straniera singola
        dv = createFKSingle(mappings, clazzOne, clazzMany, tableOne, oneColumns, manyColumns, refs);
    }

    tableMany.createForeignKey(relationship.getName(), manyColumns, oneMDQualifiedTableName, oneColumns);

    dv.setNullable(false);
    set.setKey(dv);
    mappings.addCollection(set);

    Property prop = new Property();
    prop.setName(relationship.getActualManyPropertyName());
    //prop.setNodeName(relationship.getActualManyPropertyName());
    prop.setValue(set);
    if (ForeignKeyConstraintType.importedKeyCascade.name().equalsIgnoreCase(relationship.getOnDelete())) {
        prop.setCascade("delete");
    } else {
        prop.setCascade("none");
    }
    clazzOne.addProperty(prop);

    //if(!StringUtils.)
}

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

License:Open Source License

private DependantValue createFKComposite(Mappings mappings,
        com.manydesigns.portofino.model.database.ForeignKey relationship,
        com.manydesigns.portofino.model.database.Table manyMDTable, PersistentClass clazzOne,
        PersistentClass clazzMany, Bag set, Table tableMany, Table tableOne, List<Column> oneColumns,
        List<Column> manyColumns) {
    DependantValue dv;
    Component component = new Component(mappings, set);
    component.setDynamic(manyMDTable.getActualJavaClass() == null);
    component.setEmbedded(true);/*w w w .j av a  2s . c  o  m*/
    dv = new DependantValue(mappings, clazzMany.getTable(), component);
    dv.setNullable(true);
    dv.setUpdateable(true);

    for (Reference ref : relationship.getReferences()) {
        String colToName = ref.getToColumn();
        String colToPropertyName = ref.getActualToColumn().getActualPropertyName();
        String colFromName = ref.getFromColumn();
        Iterator it = tableMany.getColumnIterator();
        while (it.hasNext()) {
            Column col = (Column) it.next();
            if (col.getName().equals(colFromName)) {
                dv.addColumn(col);
                manyColumns.add(col);
                break;
            }
        }

        Iterator it2 = tableOne.getColumnIterator();
        while (it2.hasNext()) {
            Column col = (Column) it2.next();
            if (col.getName().equals(colToName)) {
                oneColumns.add(col);
                break;
            }
        }
        Property refProp;
        refProp = getRefProperty(clazzOne, colToPropertyName);
        component.addProperty(refProp);
    }
    return dv;
}

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

License:Open Source License

private DependantValue createFKSingle(Mappings mappings, PersistentClass clazzOne, PersistentClass clazzMany,
        Table tableOne, List<Column> oneColumns, List<Column> manyColumns, List<Reference> refs) {
    DependantValue dv;
    Property refProp;/*from w  w  w.j  a  v a  2 s . co  m*/

    Reference reference = refs.get(0);
    String colFromName = reference.getFromColumn();
    String colToName = reference.getToColumn();
    String colToPropertyName = reference.getActualToColumn().getActualPropertyName();
    refProp = getRefProperty(clazzOne, colToPropertyName);
    dv = new DependantValue(mappings, clazzMany.getTable(), refProp.getPersistentClass().getKey());
    dv.setNullable(true);
    dv.setUpdateable(true);

    Iterator it = clazzMany.getTable().getColumnIterator();
    while (it.hasNext()) {
        Column col = (Column) it.next();
        if (col.getName().equals(colFromName)) {
            dv.addColumn(col);
            manyColumns.add(col);
            break;
        }
    }

    Iterator it2 = tableOne.getColumnIterator();
    while (it2.hasNext()) {
        Column col = (Column) it2.next();
        if (col.getName().equals(colToName)) {
            oneColumns.add(col);
            break;
        }
    }
    return dv;
}

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 ww  w . ja  v  a 2s  .  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 the DependentValue object that forms a primary key reference for the collection.
 *
 * @param mappings//  w w w  .ja  v  a 2s.c o  m
 * @param property          The grails property
 * @param collection        The collection object
 * @param persistentClasses
 * @return The DependantValue (key)
 */
protected DependantValue createPrimaryKeyValue(Mappings mappings, GrailsDomainClassProperty property,
        Collection collection, Map<?, ?> persistentClasses) {
    KeyValue keyValue;
    DependantValue key;
    String propertyRef = collection.getReferencedPropertyName();
    // this is to support mapping by a property
    if (propertyRef == null) {
        keyValue = collection.getOwner().getIdentifier();
    } else {
        keyValue = (KeyValue) collection.getOwner().getProperty(propertyRef).getValue();
    }

    if (LOG.isDebugEnabled())
        LOG.debug("[GrailsDomainBinder] creating dependant key value  to table ["
                + keyValue.getTable().getName() + "]");

    key = new DependantValue(mappings, collection.getCollectionTable(), keyValue);

    key.setTypeName(null);
    // make nullable and non-updateable
    key.setNullable(true);
    key.setUpdateable(false);
    return key;
}

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

License:Apache License

private static 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);

    SimpleValue iv = new SimpleValue(mappings, list.getCollectionTable());
    bindSimpleValue("integer", iv, true, columnName, mappings);
    iv.setTypeName("integer");
    list.setIndex(iv);/*  ww w .ja va2s  . c  o  m*/
    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);

        final boolean isManyToMany = property.isManyToMany();
        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.GrailsDomainBinder.java

License:Apache License

/**
 * Creates the DependentValue object that forms a primary key reference for the collection.
 *
 * @param mappings//  w w  w  .  j a va 2  s . c o m
 * @param property          The grails property
 * @param collection        The collection object
 * @param persistentClasses
 * @return The DependantValue (key)
 */
private static DependantValue createPrimaryKeyValue(Mappings mappings,
        @SuppressWarnings("unused") GrailsDomainClassProperty property, Collection collection,
        @SuppressWarnings("unused") Map<?, ?> persistentClasses) {
    KeyValue keyValue;
    DependantValue key;
    String propertyRef = collection.getReferencedPropertyName();
    // this is to support mapping by a property
    if (propertyRef == null) {
        keyValue = collection.getOwner().getIdentifier();
    } else {
        keyValue = (KeyValue) collection.getOwner().getProperty(propertyRef).getValue();
    }

    if (LOG.isDebugEnabled())
        LOG.debug("[GrailsDomainBinder] creating dependant key value  to table ["
                + keyValue.getTable().getName() + "]");

    key = new DependantValue(mappings, collection.getCollectionTable(), keyValue);

    key.setTypeName(null);
    // make nullable and non-updateable
    key.setNullable(true);
    key.setUpdateable(false);
    return key;
}

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

License:Apache License

protected void bindListSecondPass(ToMany 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 instanceof ManyToMany;

    if (isManyToMany && !property.isOwningSide()) {
        throw new MappingException("Invalid association [" + property
                + "]. List collection types only supported on the owning side of a many-to-many relationship.");
    }//from  ww w.  j  ava 2s  .c om

    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.getInverseSide());
        if (!compositeIdProperty) {
            Backref prop = new Backref();
            final PersistentEntity owner = property.getOwner();
            prop.setEntityName(owner.getName());
            prop.setName(UNDERSCORE + addUnderscore(owner.getJavaClass().getSimpleName(), 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.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

/**
 * Creates the DependentValue object that forms a primary key reference for the collection.
 *
 * @param mappings//from w w  w .  j  a  v a 2 s .com
 * @param property          The grails property
 * @param collection        The collection object
 * @param persistentClasses
 * @return The DependantValue (key)
 */
protected DependantValue createPrimaryKeyValue(Mappings mappings, PersistentProperty property,
        Collection collection, Map<?, ?> persistentClasses) {
    KeyValue keyValue;
    DependantValue key;
    String propertyRef = collection.getReferencedPropertyName();
    // this is to support mapping by a property
    if (propertyRef == null) {
        keyValue = collection.getOwner().getIdentifier();
    } else {
        keyValue = (KeyValue) collection.getOwner().getProperty(propertyRef).getValue();
    }

    if (LOG.isDebugEnabled())
        LOG.debug("[GrailsDomainBinder] creating dependant key value  to table ["
                + keyValue.getTable().getName() + "]");

    key = new DependantValue(mappings, collection.getCollectionTable(), keyValue);

    key.setTypeName(null);
    // make nullable and non-updateable
    key.setNullable(true);
    key.setUpdateable(false);
    return key;
}

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

License:Apache License

protected void bindListSecondPass(ToMany property, InFlightMetadataCollector 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 instanceof ManyToMany;

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

    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.getEntityBinding(entityName);

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

        boolean compositeIdProperty = isCompositeIdProperty(m, property.getInverseSide());
        if (!compositeIdProperty) {
            Backref prop = new Backref();
            final PersistentEntity owner = property.getOwner();
            prop.setEntityName(owner.getName());
            prop.setName(UNDERSCORE + addUnderscore(owner.getJavaClass().getSimpleName(), 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);
        }
    }
}