Example usage for org.hibernate.mapping OneToMany setReferencedEntityName

List of usage examples for org.hibernate.mapping OneToMany setReferencedEntityName

Introduction

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

Prototype

public void setReferencedEntityName(String referencedEntityName) 

Source Link

Document

Associated entity on the "many" side

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 {/* w  ww.ja v  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:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

protected void bindOneToMany(GrailsDomainClassProperty currentGrailsProp, OneToMany one, Mappings mappings) {
    one.setReferencedEntityName(currentGrailsProp.getReferencedPropertyType().getName());
    one.setIgnoreNotFound(true);/*from   www .j av  a 2 s.  c  om*/
}

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

License:Apache License

private static void bindOneToMany(GrailsDomainClassProperty currentGrailsProp, OneToMany one,
        @SuppressWarnings("unused") Mappings mappings) {
    one.setReferencedEntityName(currentGrailsProp.getReferencedPropertyType().getName());
    one.setIgnoreNotFound(true);/*from   ww  w.j a v  a  2  s.c  o m*/
}

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

License:Apache License

protected void bindOneToMany(org.grails.datastore.mapping.model.types.OneToMany currentGrailsProp,
        OneToMany one, Mappings mappings) {
    one.setReferencedEntityName(currentGrailsProp.getAssociatedEntity().getName());
    one.setIgnoreNotFound(true);//from  w w  w  .  j  a va 2s .  c  o m
}

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

License:Apache License

protected void bindOneToMany(org.grails.datastore.mapping.model.types.OneToMany currentGrailsProp,
        OneToMany one, InFlightMetadataCollector mappings) {
    one.setReferencedEntityName(currentGrailsProp.getAssociatedEntity().getName());
    one.setIgnoreNotFound(true);/*w ww  .j  ava 2s  . co m*/
}