Example usage for org.hibernate.mapping Bag Bag

List of usage examples for org.hibernate.mapping Bag Bag

Introduction

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

Prototype

public Bag(MetadataBuildingContext buildingContext, PersistentClass owner) 

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  .  ja v  a 2s. com*/
            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 createInstances() {

    if (initialized) {
        return;//www  . ja  va 2  s. co  m
    }

    initialized = true;

    SET = new CollectionType(Set.class, binder) {
        @Override
        public Collection create(GrailsDomainClassProperty property, PersistentClass owner, String path,
                Mappings mappings, String sessionFactoryBeanName) throws MappingException {
            org.hibernate.mapping.Set coll = new org.hibernate.mapping.Set(mappings, owner);
            coll.setCollectionTable(owner.getTable());
            binder.bindCollection(property, coll, owner, mappings, path, sessionFactoryBeanName);
            return coll;
        }
    };
    INSTANCES.put(Set.class, SET);
    INSTANCES.put(SortedSet.class, SET);

    LIST = new CollectionType(List.class, binder) {
        @Override
        public Collection create(GrailsDomainClassProperty property, PersistentClass owner, String path,
                Mappings mappings, String sessionFactoryBeanName) throws MappingException {
            org.hibernate.mapping.List coll = new org.hibernate.mapping.List(mappings, owner);
            coll.setCollectionTable(owner.getTable());
            binder.bindCollection(property, coll, owner, mappings, path, sessionFactoryBeanName);
            return coll;
        }
    };
    INSTANCES.put(List.class, LIST);

    BAG = new CollectionType(java.util.Collection.class, binder) {
        @Override
        public Collection create(GrailsDomainClassProperty property, PersistentClass owner, String path,
                Mappings mappings, String sessionFactoryBeanName) throws MappingException {
            Bag coll = new Bag(mappings, owner);
            coll.setCollectionTable(owner.getTable());
            binder.bindCollection(property, coll, owner, mappings, path, sessionFactoryBeanName);
            return coll;
        }
    };
    INSTANCES.put(java.util.Collection.class, BAG);

    MAP = new CollectionType(Map.class, binder) {
        @Override
        public Collection create(GrailsDomainClassProperty property, PersistentClass owner, String path,
                Mappings mappings, String sessionFactoryBeanName) throws MappingException {
            org.hibernate.mapping.Map map = new org.hibernate.mapping.Map(mappings, owner);
            binder.bindCollection(property, map, owner, mappings, path, sessionFactoryBeanName);
            return map;
        }
    };
    INSTANCES.put(Map.class, MAP);
}