Example usage for org.hibernate.mapping RootClass RootClass

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

Introduction

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

Prototype

public RootClass(MetadataBuildingContext metadataBuildingContext) 

Source Link

Usage

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

License:Apache License

/**
 * Binds a root class (one with no super classes) to the runtime meta model
 * based on the supplied Grails domain class
 *
 * @param entity The Grails domain class
 * @param mappings    The Hibernate Mappings object
 * @param sessionFactoryBeanName  the session factory bean name
 *//*  w  w w .ja va 2 s  .c om*/
public void bindRoot(HibernatePersistentEntity entity, InFlightMetadataCollector mappings,
        String sessionFactoryBeanName) {
    if (mappings.getEntityBinding(entity.getName()) != null) {
        LOG.info("[GrailsDomainBinder] Class [" + entity.getName() + "] is already mapped, skipping.. ");
        return;
    }

    RootClass root = new RootClass(this.metadataBuildingContext);
    root.setAbstract(entity.isAbstract());
    final MappingContext mappingContext = entity.getMappingContext();

    final java.util.Collection<PersistentEntity> children = mappingContext.getDirectChildEntities(entity);
    if (children.isEmpty()) {
        root.setPolymorphic(false);
    }
    bindClass(entity, root, mappings);

    Mapping m = getMapping(entity);

    bindRootPersistentClassCommonValues(entity, root, mappings, sessionFactoryBeanName);

    if (!children.isEmpty()) {
        boolean tablePerSubclass = m != null && !m.getTablePerHierarchy();
        if (!tablePerSubclass) {
            // if the root class has children create a discriminator property
            bindDiscriminatorProperty(root.getTable(), root, mappings);
        }
        // bind the sub classes
        bindSubClasses(entity, root, mappings, sessionFactoryBeanName);
    }

    if (entity.isMultiTenant()) {
        TenantId tenantId = entity.getTenantId();
        if (tenantId != null) {
            String filterCondition = getMultiTenantFilterCondition(sessionFactoryBeanName, entity);
            root.addFilter(GormProperties.TENANT_IDENTITY, filterCondition, true,
                    Collections.<String, String>emptyMap(), Collections.<String, String>emptyMap());
            mappings.addFilterDefinition(new FilterDefinition(GormProperties.TENANT_IDENTITY, filterCondition,
                    Collections.singletonMap(GormProperties.TENANT_IDENTITY,
                            root.getProperty(tenantId.getName()).getType())));
        }

    }
    mappings.addEntityBinding(root);
}

From source file:org.openflexo.jdbc.hbn.MyDBTest.java

License:Open Source License

@Test
@TestOrder(5)//from   w ww. j ava 2s  .  c  o  m
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  w  w  w . j av  a  2  s .  co  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;

}