Example usage for org.hibernate.mapping RootClass setIdentifierProperty

List of usage examples for org.hibernate.mapping RootClass setIdentifierProperty

Introduction

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

Prototype

public void setIdentifierProperty(Property identifierProperty) 

Source Link

Usage

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

License:Open Source License

protected void createPKSingle(Mappings mappings, com.manydesigns.portofino.model.database.Table mdTable,
        String pkName, RootClass clazz, Table tab,
        List<com.manydesigns.portofino.model.database.Column> columnPKList) {
    PrimaryKeyColumn pkcol = mdTable.getPrimaryKey().getPrimaryKeyColumns().get(0);
    com.manydesigns.portofino.model.database.Column column = columnPKList.get(0);
    final PrimaryKey primaryKey = new PrimaryKey();
    primaryKey.setName(pkName);/*  w  w w . ja va2s . c om*/
    primaryKey.setTable(tab);
    tab.setPrimaryKey(primaryKey);

    Column col = createColumn(mappings, tab, column);

    if (col == null) {
        // TODO PAOLO: se la PK non e' buona, tutta la tabella dovrebbe saltare
        logger.error("Skipping primary key");
        return;
    }

    SimpleValue id = (SimpleValue) col.getValue();
    //Make the defaults explicit. See section 5.1.4.5. Assigned identifiers in the Hibernate reference
    //(http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html)
    id.setIdentifierGeneratorStrategy("assigned");
    id.setNullValue("undefined");

    tab.getPrimaryKey().addColumn(col);

    Property prop = createProperty(column, id);
    clazz.addProperty(prop);
    prop.setPropertyAccessorName(mappings.getDefaultAccess());
    //PropertyGeneration generation = PropertyGeneration.parse(null);
    //prop.setGeneration(generation);

    prop.setInsertable(false);
    prop.setUpdateable(false);

    Generator generator = pkcol.getGenerator();

    setPKColumnGenerator(mappings, clazz, tab, column, id, generator);

    tab.setIdentifierValue(id);
    clazz.setIdentifier(id);
    clazz.setIdentifierProperty(prop);
    clazz.setDiscriminatorValue(mdTable.getQualifiedName());

}

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);/*from   www.ja v a  2s.com*/

    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

@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);//from w ww  .  j  a va2s  .  c o 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);
}

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

License:Apache License

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

    Mapping mapping = getMapping(identifier.getOwner());
    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 w  w.  j a  v  a  2 s . 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.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

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

    Mapping mapping = getMapping(identifier.getOwner());
    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);/*from  w  w w  .  j ava 2  s .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 (params.containsKey(SEQUENCE_KEY)) {
            params.put(SequenceStyleGenerator.SEQUENCE_PARAM, params.getProperty(SEQUENCE_KEY));
        }
        if ("assigned".equals(generator)) {
            id.setNullValue("undefined");
        }
    }

    String schemaName = getSchemaName(mappings);
    String catalogName = getCatalogName(mappings);

    params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER,
            this.metadataBuildingContext.getObjectNameNormalizer());

    if (schemaName != null) {
        params.setProperty(PersistentIdentifierGenerator.SCHEMA, schemaName);
    }
    if (catalogName != null) {
        params.setProperty(PersistentIdentifierGenerator.CATALOG, catalogName);
    }
    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.openflexo.jdbc.hbn.MyDBTest.java

License:Open Source License

@Test
@TestOrder(5)//from  w w  w  . jav a  2s .c om
public void testDeclareJDBCMapping() {

    MetadataBuildingContextRootImpl metadataBuildingContext = new MetadataBuildingContextRootImpl(
            buildingOptions, new ClassLoaderAccessImpl(null, config.getServiceRegistry()), metadataCollector);
    metadata = metadataCollector.buildMetadataInstance(metadataBuildingContext);

    // ************************
    // Creation de l'entit persiste "Dynamic_Class"

    RootClass pClass = new RootClass(metadataBuildingContext);
    pClass.setEntityName("Dynamic_Class");
    pClass.setJpaEntityName("Dynamic_Class");
    pClass.setTable(table);
    metadataCollector.addEntityBinding(pClass);

    // Creation d'une proprit (clef) et son mapping

    Property prop = new Property();
    prop.setName("Nom");
    SimpleValue value = new SimpleValue((MetadataImplementor) metadata, table);
    value.setTypeName("java.lang.String");
    value.setIdentifierGeneratorStrategy("assigned");
    value.addColumn(col);
    value.setTable(table);
    prop.setValue(value);
    pClass.setDeclaredIdentifierProperty(prop);
    pClass.setIdentifierProperty(prop);
    pClass.setIdentifier(value);

    // Creation d'une proprit et son mapping

    prop = new Property();
    prop.setName("Prenom");
    value = new SimpleValue((MetadataImplementor) metadata, table);
    value.setTypeName(String.class.getCanonicalName());
    value.addColumn(col2);
    value.setTable(table);
    prop.setValue(value);
    pClass.addProperty(prop);

    // ************************
    // Creation de l'entit persiste "Adresse"

    RootClass pClass2 = new RootClass(metadataBuildingContext);
    pClass2.setEntityName("Adresse");
    pClass2.setJpaEntityName("Adresse");
    pClass2.setTable(table2);
    metadataCollector.addEntityBinding(pClass2);

    // Creation d'une proprit (clef) et son mapping

    prop = new Property();
    prop.setName("Identifiant");
    value = new SimpleValue((MetadataImplementor) metadata, table2);
    value.setTypeName("java.lang.Integer");
    value.setIdentifierGeneratorStrategy("native");
    value.addColumn(col4);
    value.setTable(table2);
    prop.setValue(value);
    pClass2.setDeclaredIdentifierProperty(prop);
    pClass2.setIdentifierProperty(prop);
    pClass2.setIdentifier(value);

    // Creation d'une proprit et son mapping

    prop = new Property();
    prop.setName("Prenom");
    value = new SimpleValue((MetadataImplementor) metadata, table2);
    value.setTypeName(String.class.getCanonicalName());
    value.addColumn(col5);
    value.setTable(table2);
    prop.setValue(value);
    pClass2.addProperty(prop);

    try {
        ((MetadataImplementor) metadata).validate();
    } catch (MappingException e) {
        System.out.println("Validation Error: " + e.getMessage());
    }

    Namespace namespace = metadataCollector.getDatabase().getDefaultNamespace();

    for (JDBCTable aTable : connection.getSchema().getTables()) {
        System.out.println("Found table:  " + aTable + " hop: " + aTable.getName());
        Identifier logicalName = metadataCollector.getDatabase().toIdentifier(aTable.getName());
        System.out.println("logicalName=" + logicalName);
        Table laTable = namespace.locateTable(logicalName);
        System.out.println("latable=" + laTable);
    }

}

From source file:org.openflexo.technologyadapter.jdbc.model.DynamicModelBuilder.java

License:Open Source License

public Metadata buildDynamicModel() {
    Metadata metadata = metadataCollector.buildMetadataInstance(metadataBuildingContext);

    Database database = metadata.getDatabase();

    // **********
    // Creation / Dfinition de la table T_Dynamic_Table

    Table table = metadataCollector.addTable("", "", "T_Dynamic_Table", null, false);
    table.setName("T_Dynamic_Table");
    Column col = new Column();
    col.setName("pouet");
    col.setLength(256);//from www  .j  av  a2 s  . c o m
    col.setSqlType("CHAR(256)");
    col.setNullable(false);
    table.addColumn(col);

    PrimaryKey pk = new PrimaryKey(table);
    pk.addColumn(col);

    UniqueKey uk1 = new UniqueKey();
    uk1.setName("Nom_Unique");
    uk1.setTable(table);
    uk1.addColumn(col);
    table.addUniqueKey(uk1);

    Column col2 = new Column();
    col2.setName("padam");
    col2.setLength(256);
    col2.setSqlType("CHAR(256)");
    col2.setNullable(true);
    table.addColumn(col2);
    // pour rire les couples "Nom + Prenom" doivent tre uniques
    UniqueKey uk = new UniqueKey();
    uk.setName("Couple_Nom_Prenom_Unique");
    uk.setTable(table);
    uk.addColumn(col);
    uk.addColumn(col2);
    table.addUniqueKey(uk);

    // une colonne de clef etrangre vers T_Adresse
    Column col3 = new Column();
    col3.setName("id_addr");
    col3.setLength(16);
    col3.setSqlType("INTEGER");
    col3.setNullable(true);
    table.addColumn(col3);

    // **********
    // Creation / Dfinition de la table T_Adresse
    Table table2 = metadataCollector.addTable("", "", "T_Adresse", null, false);
    table2.setName("T_Adresse");
    Column col4 = new Column();
    col4.setName("Id");
    col4.setLength(16);
    col4.setSqlType("INTEGER");
    col4.setNullable(false);
    table2.addColumn(col4);

    pk = new PrimaryKey(table2);
    pk.addColumn(col);

    uk1 = new UniqueKey();
    uk1.setName("Id_Unique");
    uk1.setTable(table2);
    uk1.addColumn(col4);
    table.addUniqueKey(uk1);

    Column col5 = new Column();
    col5.setName("Adresse");
    col5.setLength(512);
    col5.setSqlType("CHAR(512)");
    col5.setNullable(true);
    table2.addColumn(col5);

    // ************************
    // Creation de l'entit persiste "Dynamic_Class"

    RootClass pClass = new RootClass(metadataBuildingContext);
    pClass.setEntityName("Dynamic_Class");
    pClass.setJpaEntityName("Dynamic_Class");
    pClass.setTable(table);
    metadataCollector.addEntityBinding(pClass);

    // Creation d'une proprit (clef) et son mapping

    Property prop = new Property();
    prop.setName("Nom");
    SimpleValue value = new SimpleValue((MetadataImplementor) metadata, table);
    value.setTypeName("java.lang.String");
    value.setIdentifierGeneratorStrategy("assigned");
    value.addColumn(col);
    value.setTable(table);
    prop.setValue(value);
    pClass.setDeclaredIdentifierProperty(prop);
    pClass.setIdentifierProperty(prop);
    pClass.setIdentifier(value);

    // Creation d'une proprit et son mapping

    prop = new Property();
    prop.setName("Prenom");
    value = new SimpleValue((MetadataImplementor) metadata, table);
    value.setTypeName(String.class.getCanonicalName());
    value.addColumn(col2);
    value.setTable(table);
    prop.setValue(value);
    pClass.addProperty(prop);

    // ************************
    // Creation de l'entit persiste "Adresse"

    RootClass pClass2 = new RootClass(metadataBuildingContext);
    pClass2.setEntityName("Adresse");
    pClass2.setJpaEntityName("Adresse");
    pClass2.setTable(table2);
    metadataCollector.addEntityBinding(pClass2);

    // Creation d'une proprit (clef) et son mapping

    prop = new Property();
    prop.setName("Identifiant");
    value = new SimpleValue((MetadataImplementor) metadata, table2);
    value.setTypeName("java.lang.Integer");
    value.setIdentifierGeneratorStrategy("native");
    value.addColumn(col4);
    value.setTable(table2);
    prop.setValue(value);
    pClass2.setDeclaredIdentifierProperty(prop);
    pClass2.setIdentifierProperty(prop);
    pClass2.setIdentifier(value);

    // Creation d'une proprit et son mapping

    prop = new Property();
    prop.setName("Prenom");
    value = new SimpleValue((MetadataImplementor) metadata, table2);
    value.setTypeName(String.class.getCanonicalName());
    value.addColumn(col5);
    value.setTable(table2);
    prop.setValue(value);
    pClass2.addProperty(prop);

    try {
        ((MetadataImplementor) metadata).validate();
    } catch (MappingException e) {
        System.out.println("Validation Error: " + e.getMessage());
    }

    return metadata;

}