Example usage for org.hibernate.boot Metadata getDatabase

List of usage examples for org.hibernate.boot Metadata getDatabase

Introduction

In this page you can find the example usage for org.hibernate.boot Metadata getDatabase.

Prototype

Database getDatabase();

Source Link

Document

Retrieve the database model.

Usage

From source file:com.blazebit.persistence.integration.hibernate.Hibernate52Integrator.java

License:Apache License

@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory,
        SessionFactoryServiceRegistry serviceRegistry) {
    // TODO: remember metadata for exact column types
    for (PersistentClass clazz : metadata.getEntityBindings()) {
        Class<?> entityClass = clazz.getMappedClass();

        if (entityClass != null && entityClass.isAnnotationPresent(CTE.class)) {
            clazz.getTable().setSubselect("select * from " + clazz.getJpaEntityName());
        }//from w  ww  .j av  a2  s.c o  m
    }

    serviceRegistry.locateServiceBinding(PersisterClassResolver.class)
            .setService(new CustomPersisterClassResolver());
    TableNameFormatter formatter = new NativeTableNameFormatter(
            sessionFactory.getJdbcServices().getJdbcEnvironment().getQualifiedObjectNameFormatter());
    serviceRegistry.locateServiceBinding(Database.class)
            .setService(new SimpleDatabase(getTableIterator(metadata.getDatabase().getNamespaces()),
                    sessionFactory.getDialect(), formatter, metadata));
}

From source file:com.blazebit.persistence.integration.hibernate.Hibernate5Integrator.java

License:Apache License

@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory,
        SessionFactoryServiceRegistry serviceRegistry) {
    for (PersistentClass clazz : metadata.getEntityBindings()) {
        Class<?> entityClass = clazz.getMappedClass();

        if (entityClass != null && entityClass.isAnnotationPresent(CTE.class)) {
            clazz.getTable().setSubselect("select * from " + clazz.getJpaEntityName());
        }//  w  ww  . ja  v  a2  s  . co  m
    }

    serviceRegistry.locateServiceBinding(PersisterClassResolver.class)
            .setService(new CustomPersisterClassResolver());
    TableNameFormatter formatter = new NativeTableNameFormatter(
            sessionFactory.getJdbcServices().getJdbcEnvironment().getQualifiedObjectNameFormatter());
    serviceRegistry.locateServiceBinding(Database.class)
            .setService(new SimpleDatabase(getTableIterator(metadata.getDatabase().getNamespaces()),
                    sessionFactory.getDialect(), formatter, metadata));
}

From source file:com.blazebit.persistence.integration.hibernate.Hibernate60Integrator.java

License:Apache License

@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory,
        SessionFactoryServiceRegistry serviceRegistry) {
    for (PersistentClass clazz : metadata.getEntityBindings()) {
        Class<?> entityClass = clazz.getMappedClass();

        if (entityClass != null && entityClass.isAnnotationPresent(CTE.class)) {
            clazz.getTable().setSubselect("select * from " + clazz.getJpaEntityName());
        }/*from  w ww.  ja  v a2s  .  co m*/
    }

    serviceRegistry.locateServiceBinding(PersisterClassResolver.class)
            .setService(new CustomPersisterClassResolver());
    TableNameFormatter formatter = new NativeTableNameFormatter(
            sessionFactory.getJdbcServices().getJdbcEnvironment().getQualifiedObjectNameFormatter());
    serviceRegistry.locateServiceBinding(Database.class)
            .setService(new SimpleDatabase(getTableIterator(metadata.getDatabase().getNamespaces()),
                    sessionFactory.getJdbcServices().getDialect(), formatter, metadata));
}

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 ww  w.  ja  va2  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;

}