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

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

Introduction

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

Prototype

public Collection<Table> getTables() 

Source Link

Usage

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

License:Open Source License

@Test
@TestOrder(3)/*from  www.  jav  a 2  s .  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(8)//  w w w .  ja v a2  s .co  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"));*/
    }
}