Example usage for org.hibernate.mapping Property Property

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

Introduction

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

Prototype

Property

Source Link

Usage

From source file:com.javaetmoi.core.persistence.hibernate.TestReflectionUtil.java

License:Apache License

@Before
public void setUp() {
    Configuration configuration = new Configuration();
    Mappings mappings = configuration.createMappings();
    Component component = new Component(mappings, new RootClass());
    Property p1 = new Property();
    p1.setName("client");
    SimpleValue p1val = new SimpleValue(mappings);
    p1val.setTypeName("java.lang.Integer");
    p1.setValue(p1val);
    component.addProperty(p1);/*from  w w  w.  j  ava2 s.c om*/
    Property p2 = new Property();
    p2.setName("user");
    SimpleValue p2val = new SimpleValue(mappings);
    p2val.setTypeName("java.lang.String");
    p2.setValue(p2val);
    component.addProperty(p2);
    metadata = new ComponentMetamodel(component);
}

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

License:Open Source License

protected Property createProperty(com.manydesigns.portofino.model.database.Column column, Value value) {
    Property prop = new Property();
    prop.setName(column.getActualPropertyName());
    //prop.setNodeName(column.getActualPropertyName());
    prop.setValue(value);// w  w w  .  j av a 2  s.c om
    return prop;
}

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 ww  . j  av a  2  s . 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:com.manydesigns.portofino.persistence.hibernate.HibernateConfig.java

License:Open Source License

protected void createM2O(Configuration config, Mappings mappings, ForeignKey relationship) {
    com.manydesigns.portofino.model.database.Table manyMDTable = relationship.getFromTable();
    com.manydesigns.portofino.model.database.Table oneMDTable = relationship.getToTable();
    String manyMDQualifiedTableName = manyMDTable.getActualEntityName();
    String oneMDQualifiedTableName = oneMDTable.getActualEntityName();

    RootClass clazz = (RootClass) mappings.getClass(manyMDQualifiedTableName);
    if (clazz == null) {
        logger.error("Cannot find table '{}' as 'many' side of foreign key '{}'. Skipping relationship.",
                manyMDQualifiedTableName, relationship.getName());
        return;/*from   ww w  . j a  va 2  s.c  o m*/
    }

    Table tab = clazz.getTable();
    List<String> columnNames = new ArrayList<String>();

    for (Reference ref : relationship.getReferences()) {
        if (ref.getActualFromColumn() == null) {
            logger.error("Missing from column {}, skipping relationship", ref.getFromColumn());
            return;
        }
        columnNames.add(ref.getFromColumn());
    }

    ManyToOne m2o = new ManyToOne(mappings, tab);
    m2o.setLazy(LAZY);
    final HashMap<String, PersistentClass> persistentClasses = new HashMap<String, PersistentClass>();
    persistentClasses.put(oneMDQualifiedTableName, config.getClassMapping(oneMDQualifiedTableName));
    m2o.setReferencedEntityName(oneMDQualifiedTableName);
    m2o.createPropertyRefConstraints(persistentClasses);

    PersistentClass manyClass = config.getClassMapping(manyMDQualifiedTableName);
    for (String columnName : columnNames) {
        Column col = new Column();
        col.setName(escapeName(columnName));
        //Recupero la colonna precedentemente associata alla tabella:
        //essa ha uno uniqueIdentifier generato al momento dell'associazione alla tabella;
        //questo viene utilizzato per disambiguare l'alias della colonna nelle query
        //SQL generate da Hibernate.
        col = manyClass.getTable().getColumn(col);
        if (col == null) {
            logger.error("Column not found in 'many' entity {}: {}, " + "skipping relationship",
                    manyClass.getEntityName(), columnName);
            return;
        }
        m2o.addColumn(col);
    }

    Property prop = new Property();
    prop.setName(relationship.getActualOnePropertyName());
    //prop.setNodeName(relationship.getActualOnePropertyName());
    prop.setValue(m2o);
    prop.setCascade("none"); //TODO era "all", capire
    prop.setInsertable(false);
    prop.setUpdateable(false);
    clazz.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  w w w.  j a v  a  2 s .  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.AbstractGrailsDomainBinder.java

License:Apache License

protected void bindVersion(GrailsDomainClassProperty version, RootClass entity, Mappings mappings,
        String sessionFactoryBeanName) {

    SimpleValue val = new SimpleValue(mappings, entity.getTable());

    bindSimpleValue(version, null, val, EMPTY_PATH, mappings, sessionFactoryBeanName);

    if (val.isTypeSpecified()) {
        if (!(val.getType() instanceof IntegerType || val.getType() instanceof LongType
                || val.getType() instanceof TimestampType)) {
            LOG.warn("Invalid version class specified in " + version.getDomainClass().getClazz().getName()
                    + "; must be one of [int, Integer, long, Long, Timestamp, Date]. Not mapping the version.");
            return;
        }/*from  w  w w  .  j  a  v a2 s  .co m*/
    } else {
        val.setTypeName("version".equals(version.getName()) ? "integer" : "timestamp");
    }
    Property prop = new Property();
    prop.setValue(val);

    bindProperty(version, prop, mappings);
    val.setNullValue("undefined");
    entity.setVersion(prop);
    entity.setOptimisticLockMode(0); // 0 is to use version column
    entity.addProperty(prop);
}

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

License:Apache License

@SuppressWarnings("unchecked")
protected void bindSimpleId(GrailsDomainClassProperty identifier, RootClass entity, Mappings mappings,
        Identity mappedId, String sessionFactoryBeanName) {

    Mapping mapping = getMapping(identifier.getDomainClass());
    boolean useSequence = mapping != null && mapping.isTablePerConcreteClass();

    // create the id value
    SimpleValue id = new SimpleValue(mappings, entity.getTable());
    // set identifier on entity

    Properties params = new Properties();
    entity.setIdentifier(id);/*  w ww  .j  ava  2s.  c  om*/

    if (mappedId == null) {
        // configure generator strategy
        id.setIdentifierGeneratorStrategy(useSequence ? "sequence-identity" : "native");
    } else {
        String generator = mappedId.getGenerator();
        if ("native".equals(generator) && useSequence) {
            generator = "sequence-identity";
        }
        id.setIdentifierGeneratorStrategy(generator);
        params.putAll(mappedId.getParams());
        if ("assigned".equals(generator)) {
            id.setNullValue("undefined");
        }
    }

    params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, mappings.getObjectNameNormalizer());

    if (mappings.getSchemaName() != null) {
        params.setProperty(PersistentIdentifierGenerator.SCHEMA, mappings.getSchemaName());
    }
    if (mappings.getCatalogName() != null) {
        params.setProperty(PersistentIdentifierGenerator.CATALOG, mappings.getCatalogName());
    }
    id.setIdentifierGeneratorProperties(params);

    // bind value
    bindSimpleValue(identifier, null, id, EMPTY_PATH, mappings, sessionFactoryBeanName);

    // create property
    Property prop = new Property();
    prop.setValue(id);

    // bind property
    bindProperty(identifier, prop, mappings);
    // set identifier property
    entity.setIdentifierProperty(prop);

    id.getTable().setIdentifierValue(id);
}

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//from  w ww.  jav  a2s .c  om
    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 bindVersion(GrailsDomainClassProperty version, RootClass entity, Mappings mappings,
        String sessionFactoryBeanName) {

    SimpleValue val = new SimpleValue(mappings, entity.getTable());

    bindSimpleValue(version, null, val, EMPTY_PATH, mappings, sessionFactoryBeanName);

    if (val.isTypeSpecified()) {
        if (!(val.getType() instanceof IntegerType || val.getType() instanceof LongType
                || val.getType() instanceof TimestampType)) {
            LOG.warn("Invalid version class specified in " + version.getDomainClass().getClazz().getName()
                    + "; must be one of [int, Integer, long, Long, Timestamp, Date]. Not mapping the version.");
            return;
        }/*  w  ww .  ja v  a2s .co  m*/
    } else {
        val.setTypeName("version".equals(version.getName()) ? "integer" : "timestamp");
    }
    Property prop = new Property();
    prop.setValue(val);

    bindProperty(version, prop, mappings);
    val.setNullValue("undefined");
    entity.setVersion(prop);
    entity.addProperty(prop);
}

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

License:Apache License

@SuppressWarnings("unchecked")
private static void bindSimpleId(GrailsDomainClassProperty identifier, RootClass entity, Mappings mappings,
        Identity mappedId, String sessionFactoryBeanName) {

    // create the id value
    SimpleValue id = new SimpleValue(mappings, entity.getTable());
    // set identifier on entity

    Properties params = new Properties();
    entity.setIdentifier(id);/*ww w.  j a  va2  s .  co m*/

    if (mappedId != null) {
        id.setIdentifierGeneratorStrategy(mappedId.getGenerator());
        params.putAll(mappedId.getParams());
        if ("assigned".equals(mappedId.getGenerator())) {
            id.setNullValue("undefined");
        }
    } else {
        // configure generator strategy
        id.setIdentifierGeneratorStrategy("native");
    }

    params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, mappings.getObjectNameNormalizer());

    if (mappings.getSchemaName() != null) {
        params.setProperty(PersistentIdentifierGenerator.SCHEMA, mappings.getSchemaName());
    }
    if (mappings.getCatalogName() != null) {
        params.setProperty(PersistentIdentifierGenerator.CATALOG, mappings.getCatalogName());
    }
    id.setIdentifierGeneratorProperties(params);

    // bind value
    bindSimpleValue(identifier, null, id, EMPTY_PATH, mappings, sessionFactoryBeanName);

    // create property
    Property prop = new Property();
    prop.setValue(id);

    // bind property
    bindProperty(identifier, prop, mappings);
    // set identifier property
    entity.setIdentifierProperty(prop);

    id.getTable().setIdentifierValue(id);
}