Example usage for org.hibernate.mapping Value createForeignKey

List of usage examples for org.hibernate.mapping Value createForeignKey

Introduction

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

Prototype

public void createForeignKey() throws MappingException;

Source Link

Usage

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()/* w ww  . j a v a 2  s  .  c  o 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

/**
 * Binds a unidirectional one-to-many creating a psuedo back reference property in the process.
 *
 * @param property/*from  w w w  .j  a v a 2 s.c o m*/
 * @param mappings
 * @param collection
 */
protected void bindUnidirectionalOneToMany(GrailsDomainClassProperty property, Mappings mappings,
        Collection collection) {
    Value v = collection.getElement();
    v.createForeignKey();
    String entityName;
    if (v instanceof ManyToOne) {
        ManyToOne manyToOne = (ManyToOne) v;

        entityName = manyToOne.getReferencedEntityName();
    } else {
        entityName = ((OneToMany) v).getReferencedEntityName();
    }
    collection.setInverse(false);
    PersistentClass referenced = mappings.getClass(entityName);
    Backref prop = new Backref();
    prop.setEntityName(property.getDomainClass().getFullName());
    prop.setName(UNDERSCORE + addUnderscore(property.getDomainClass().getShortName(), property.getName())
            + "Backref");
    prop.setUpdateable(false);
    prop.setInsertable(true);
    prop.setCollectionRole(collection.getRole());
    prop.setValue(collection.getKey());
    prop.setOptional(true);

    referenced.addProperty(prop);
}

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

License:Apache License

protected Property createProperty(Value value, PersistentClass persistentClass,
        GrailsDomainClassProperty grailsProperty, Mappings mappings) {
    // set type/*from  ww  w. ja  v a2s . c  o  m*/
    value.setTypeUsingReflection(persistentClass.getClassName(), grailsProperty.getName());

    if (value.getTable() != null) {
        value.createForeignKey();
    }

    Property prop = new Property();
    prop.setValue(value);
    bindProperty(grailsProperty, prop, mappings);
    return prop;
}

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);/*from  www. j  av a2  s  .  com*/
    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

/**
 * Binds a unidirectional one-to-many creating a psuedo back reference property in the process.
 *
 * @param property/*from  w ww . j a  va2s.  c  o  m*/
 * @param mappings
 * @param collection
 */
private static void bindUnidirectionalOneToMany(GrailsDomainClassProperty property, Mappings mappings,
        Collection collection) {
    Value v = collection.getElement();
    v.createForeignKey();
    String entityName;
    if (v instanceof ManyToOne) {
        ManyToOne manyToOne = (ManyToOne) v;

        entityName = manyToOne.getReferencedEntityName();
    } else {
        entityName = ((OneToMany) v).getReferencedEntityName();
    }
    collection.setInverse(false);
    PersistentClass referenced = mappings.getClass(entityName);
    Backref prop = new Backref();
    prop.setEntityName(property.getDomainClass().getFullName());
    prop.setName(UNDERSCORE + addUnderscore(property.getDomainClass().getShortName(), property.getName())
            + "Backref");
    prop.setUpdateable(false);
    prop.setInsertable(true);
    prop.setCollectionRole(collection.getRole());
    prop.setValue(collection.getKey());
    prop.setOptional(true);

    referenced.addProperty(prop);
}

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

License:Apache License

private static Property createProperty(Value value, PersistentClass persistentClass,
        GrailsDomainClassProperty grailsProperty, Mappings mappings) {
    // set type// w w w  . ja v a 2 s . com
    value.setTypeUsingReflection(persistentClass.getClassName(), grailsProperty.getName());

    if (value.getTable() != null) {
        value.createForeignKey();
    }

    Property prop = new Property();
    prop.setValue(value);
    bindProperty(grailsProperty, prop, mappings);
    return prop;
}

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.");
    }//  www  .  ja  va  2 s.  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

/**
 * Binds a unidirectional one-to-many creating a psuedo back reference property in the process.
 *
 * @param property/*from ww w  .jav  a 2  s.c  o m*/
 * @param mappings
 * @param collection
 */
protected void bindUnidirectionalOneToMany(org.grails.datastore.mapping.model.types.OneToMany property,
        Mappings mappings, Collection collection) {
    Value v = collection.getElement();
    v.createForeignKey();
    String entityName;
    if (v instanceof ManyToOne) {
        ManyToOne manyToOne = (ManyToOne) v;

        entityName = manyToOne.getReferencedEntityName();
    } else {
        entityName = ((OneToMany) v).getReferencedEntityName();
    }
    collection.setInverse(false);
    PersistentClass referenced = mappings.getClass(entityName);
    Backref prop = new Backref();
    PersistentEntity owner = property.getOwner();
    prop.setEntityName(owner.getName());
    prop.setName(
            UNDERSCORE + addUnderscore(owner.getJavaClass().getSimpleName(), property.getName()) + "Backref");
    prop.setUpdateable(false);
    prop.setInsertable(true);
    prop.setCollectionRole(collection.getRole());
    prop.setValue(collection.getKey());
    prop.setOptional(true);

    referenced.addProperty(prop);
}

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

License:Apache License

protected Property createProperty(Value value, PersistentClass persistentClass,
        PersistentProperty grailsProperty, Mappings mappings) {
    // set type//from   w w  w  .j a  v a2 s .  co  m
    value.setTypeUsingReflection(persistentClass.getClassName(), grailsProperty.getName());

    if (value.getTable() != null) {
        value.createForeignKey();
    }

    Property prop = new Property();
    prop.setValue(value);
    bindProperty(grailsProperty, prop, mappings);
    return prop;
}

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   ww  w.  j a v  a  2  s  . 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.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);
        }
    }
}