Example usage for org.hibernate.boot.model.relational Namespace locateTable

List of usage examples for org.hibernate.boot.model.relational Namespace locateTable

Introduction

In this page you can find the example usage for org.hibernate.boot.model.relational Namespace locateTable.

Prototype

public Table locateTable(Identifier logicalTableName) 

Source Link

Document

Returns the table with the specified logical table name.

Usage

From source file:net.e6tech.elements.persist.hibernate.ModifiedTableGenerator.java

License:Apache License

@Override
public void registerExportables(Database database) {
    final Dialect dialect = database.getJdbcEnvironment().getDialect();

    final Namespace namespace = database.locateNamespace(qualifiedTableName.getCatalogName(),
            qualifiedTableName.getSchemaName());

    Table table = namespace.locateTable(qualifiedTableName.getObjectName());
    if (table == null) {
        table = namespace.createTable(qualifiedTableName.getObjectName(), false);

        // todo : note sure the best solution here.  do we add the columns if missing?  other?
        final Column segmentColumn = new ExportableColumn(database, table, segmentColumnName,
                StringType.INSTANCE, dialect.getTypeName(Types.VARCHAR, segmentValueLength, 0, 0));
        segmentColumn.setNullable(false);
        table.addColumn(segmentColumn);//from w ww  .jav a  2 s.c o m

        // lol
        table.setPrimaryKey(new PrimaryKey(table));
        table.getPrimaryKey().addColumn(segmentColumn);

        final Column valueColumn = new ExportableColumn(database, table, valueColumnName, LongType.INSTANCE);
        table.addColumn(valueColumn);
    }

    // allow physical naming strategies a chance to kick in
    this.renderedTableName = database.getJdbcEnvironment().getQualifiedObjectNameFormatter()
            .format(table.getQualifiedTableName(), dialect);

    this.selectQuery = buildSelectQuery(dialect);
    this.updateQuery = buildUpdateQuery();
    this.insertQuery = buildInsertQuery();

}

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

License:Open Source License

@Test
@TestOrder(3)//w  w w .j  a v  a 2s  . co  m
public void createHbnConfig() {

    config = new HbnConfig(new BootstrapServiceRegistryBuilder().build());

    config.setProperty("hibernate.connection.driver_class", jdbcDriverClassname);
    config.setProperty("hibernate.connection.url", jdbcURL);
    config.setProperty("hibernate.connection.username", jdbcUser);
    config.setProperty("hibernate.connection.password", jdbcPwd);
    config.setProperty("hibernate.connection.pool_size", "1");
    config.setProperty("hibernate.dialect", hbnDialect);
    config.setProperty("hibernate.show_sql", "true");
    // creates object, wipe out if already exists
    // config.setProperty("hibernate.hbm2ddl.auto", "create-drop");

    buildingOptions = new MetadataBuilderImpl.MetadataBuildingOptionsImpl(config.getServiceRegistry());
    metadataCollector = new InFlightMetadataCollectorImpl(buildingOptions, new TypeResolver());

    /*final Namespace namespace = metadataCollector.getDatabase().locateNamespace(
    getDatabase().toIdentifier( catalogName ),
    getDatabase().toIdentifier( schemaName )
    );*/

    Iterable<Namespace> namespaces = metadataCollector.getDatabase().getNamespaces();

    System.out.println("Prout debut");
    for (Namespace ns : namespaces) {
        System.out.println("> hop: " + ns);
    }
    System.out.println("Prout fin");

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

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

    System.out.println(namespace.getTables());

}

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

License:Open Source License

@Test
@TestOrder(5)/*  ww  w .j  av  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.jdbc.hbn.MyDBTest.java

License:Open Source License

@Test
@TestOrder(8)//from  w ww  .  j  a v a 2 s  .  c o  m
public void testConnectToDB() {

    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>  On essaie de faire un truc");

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

    try (SessionFactory hbnSessionFactory = metadata.buildSessionFactory();
            Session hbnSession = hbnSessionFactory.withOptions().openSession()) {

        Iterable<Namespace> namespaces = metadataCollector.getDatabase().getNamespaces();

        System.out.println("Prout debut");
        for (Namespace ns : namespaces) {
            System.out.println("> hop: " + ns);
        }
        System.out.println("Prout fin");

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

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

        System.out.println(namespace.getTables());

        /*Map<String, String> syl = new HashMap<>();
        syl.put("Nom", "Sylvain2");
        Map<String, String> chris = new HashMap<>();
        chris.put("Nom", "Guychard2");
        chris.put("Prenom", "Christophe2");
                
        // Srialisation de l'instance
        // Hibernate native
        Transaction trans = hbnSession.beginTransaction();
                
        hbnSession.save("Dynamic_Class", syl);
        hbnSession.save("Dynamic_Class", chris);
                
        trans.commit();*/

        // NativeQuery<?> sqlQ = hbnSession.createNativeQuery("select * from T_Dynamic_Table_2;");

        Query<?> sqlQ = hbnSession.createQuery("select o from Dynamic_Class o");

        List<?> result = sqlQ.getResultList();
        assertEquals(5, result.size());

        for (Object o : result) {
            System.out.println(" > " + o + " of " + o.getClass());
            /*if (o.getClass().isArray()) {
               Object[] array = (Object[]) o;
               for (Object o2 : array) {
                  System.out.println("  >> " + o2);
               }
            }*/
        }

        Transaction t = hbnSession.beginTransaction();

        Map<String, Object> o1 = (Map<String, Object>) result.get(0);
        // Unused Map<String, Object> o2 = (Map<String, Object>)
        result.get(1);
        // Unused Map<String, Object> o3 = (Map<String, Object>)
        result.get(2);
        // Unused Map<String, Object> o4 = (Map<String, Object>)
        result.get(3);
        // Unused Map<String, Object> o5 = (Map<String, Object>)
        result.get(4);

        o1.put("Prenom", "toto");

        t.commit();

        /*System.out.println("DEBUT");
        Set<EntityType<?>> entities = hbnSession.getMetamodel().getEntities();
        for (EntityType<?> ent : entities) {
           System.out.println("Entit dynamique: " + ent.getName());
        }
        System.out.println("FIN");
                
        System.err.println(hbnSession.load("Dynamic_Class", "Sylvain"));*/
    }
}