Example usage for org.hibernate SessionFactory getCurrentSession

List of usage examples for org.hibernate SessionFactory getCurrentSession

Introduction

In this page you can find the example usage for org.hibernate SessionFactory getCurrentSession.

Prototype

Session getCurrentSession() throws HibernateException;

Source Link

Document

Obtains the current session.

Usage

From source file:edu.rmit.eres.seaports.database.DemographicsDataSourceLoader.java

License:Open Source License

/**
 * Main method used to load  the Demographics Data Source only.
 * On an existing database, this may duplicate data.
 * @param args: no parameters//w  ww  .  j av  a  2 s .  c om
 */
public static void main(String[] args) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    config.configure("hibernate.cfg.xml");
    new SchemaExport(config).create(true, true);

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    LoadDemographicsDataSource(session);

    session.getTransaction().commit();
}

From source file:edu.rmit.eres.seaports.database.DisplayTypesLoader.java

License:Open Source License

/**
 * Main method used to load the display types existing in the database.
 * On an existing database, this may duplicate data.
 * @param args: no parameters//  w w  w  .  ja  v a2s .  co  m
 */
public static void main(String[] args) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    config.configure("hibernate.cfg.xml");
    new SchemaExport(config).create(true, true);

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    LoadDisplayTypes(session);

    session.getTransaction().commit();
}

From source file:edu.rmit.eres.seaports.database.FutureClimateRiskDataSourceLoader.java

License:Open Source License

/**
 * Main method used to load  the CSIRO Data Source only.
 * On an existing database, this may duplicate data.
 * @param args: no parameters/*  ww  w . j av  a2  s . co m*/
 */
public static void main(String[] args) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    config.configure("hibernate.cfg.xml");
    new SchemaExport(config).create(true, true);

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    LoadFutureClimateRiskDataSource(session);

    session.getTransaction().commit();
}

From source file:edu.rmit.eres.seaports.database.ObservedExtremeDataSourceLoader.java

License:Open Source License

/**
 * Main method used to load  the CSIRO Data Source only.
 * On an existing database, this may duplicate data.
 * @param args: no parameters/*from   ww w  .j  a v  a  2  s .c  o m*/
 */
public static void main(String[] args) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    config.configure("hibernate.cfg.xml");
    new SchemaExport(config).create(true, true);

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    LoadObservedExtremeDataSource(session);

    session.getTransaction().commit();
}

From source file:edu.rmit.eres.seaports.database.ObservedTrendDataSourceLoader.java

License:Open Source License

/**
 * Main method used to load  the CSIRO Data Source only.
 * On an existing database, this may duplicate data.
 * @param args: no parameters/*from   w  w w. j  a  v  a  2s .  co  m*/
 */
public static void main(String[] args) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    config.configure("hibernate.cfg.xml");
    new SchemaExport(config).create(true, true);

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    LoadObservedTrendDataSource(session);

    session.getTransaction().commit();
}

From source file:edu.rmit.eres.seaports.database.PastClimateImpactDescriptionDataSourceLoader.java

License:Open Source License

/**
 * Main method used to load  the CSIRO Data Source only.
 * On an existing database, this may duplicate data.
 * @param args: no parameters// w  w  w  .j av a  2s .  c  om
 */
public static void main(String[] args) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    config.configure("hibernate.cfg.xml");
    new SchemaExport(config).create(true, true);

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    LoadPastClimateImpactDescriptionDataSource(session);

    session.getTransaction().commit();
}

From source file:edu.rmit.eres.seaports.database.ProjectedClimateChangeDataSourceLoader.java

License:Open Source License

/**
 * Main method used to load  the Projected Climate Change Data Source only.
 * On an existing database, this may duplicate data.
 * @param args: no parameters//  w  w w.j  a  va  2  s  .c o  m
 */
public static void main(String[] args) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    config.configure("hibernate.cfg.xml");
    new SchemaExport(config).create(true, true);

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    LoadProjectedClimateChangeDataSource(session);

    session.getTransaction().commit();
}

From source file:edu.rmit.eres.seaports.database.ProjectedClimateExtremeDataSourceLoader.java

License:Open Source License

/**
 * Main method used to load the Projected Climate Extreme Data Source only.
 * On an existing database, this may duplicate data.
 * @param args: no parameters//from ww  w  .ja  v a 2 s.c  o m
 */
public static void main(String[] args) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    config.configure("hibernate.cfg.xml");
    new SchemaExport(config).create(true, true);

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    LoadProjectedClimateExtremeDataSource(session);

    session.getTransaction().commit();
}

From source file:edu.rmit.eres.seaports.database.RegionsLoader.java

License:Open Source License

/**
 * Main method used to load Regions and Seaports in the database.
 * On an existing database, this may duplicate data.
 * @param args: no parameters//from  w w w .j av a 2s .  c om
 */
public static void main(String[] args) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    config.configure("hibernate.cfg.xml");
    new SchemaExport(config).create(true, true);

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    LoadRegions(session);

    session.getTransaction().commit();
}

From source file:edu.rmit.eres.seaports.database.TestDatabaseLoader.java

License:Open Source License

/**
 * Main method loading all the test data in the test database using Hibernate.
 * Running this is equivalent to importing the 'seaports_test_dump.sql' SQL 
 * script in an empty 'seaports' database, minus the engineering model examples.
 * @param args: no parameters//from  www  .j av  a  2s .c o  m
 */
public static void main(String[] args) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    config.configure("hibernate-test.cfg.xml");
    new SchemaExport(config).create(true, true);

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    // Regions
    Region r1 = new Region("Region 1",
            "176.284180,-15.891800 -178.046875,-15.891800 -178.046875,-20.070571 176.284180,-20.070571");
    Region r2 = new Region("Region 2", "");
    session.save(r1);
    session.save(r2);

    // Seaports
    Seaport port1 = new Seaport("CODE1", "Port 1", 0.0, 0.0, r1, "City 1");
    Seaport port2 = new Seaport("CODE2", "Port 2", 0.0, 0.0, r1, "City 2");
    Seaport port3 = new Seaport("CODE3", "Port 3", 0.0, 0.0, r1, "City 3");
    session.save(port1);
    session.save(port2);
    session.save(port3);

    // Loads the element categories
    CategoriesLoader.LoadCategories(session);

    // Loads the display types available
    DisplayTypesLoader.LoadDisplayTypes(session);

    ObservedTrendDataSourceLoader.LoadObservedTrendDataSource(session);
    ObservedExtremeDataSourceLoader.LoadObservedExtremeDataSource(session);
    ProjectedClimateChangeDataSourceLoader.LoadProjectedClimateChangeDataSource(session);
    ProjectedClimateExtremeDataSourceLoader.LoadProjectedClimateExtremeDataSource(session);
    PastClimateImpactDescriptionDataSourceLoader.LoadPastClimateImpactDescriptionDataSource(session);
    CurrentClimateVulnerabilityDataSourceLoader.LoadCurrentClimateVulnerabilityDataSource(session);
    FutureClimateRiskDataSourceLoader.LoadFutureClimateRiskDataSource(session);
    TradeDataSourceLoader.LoadTradeDataSource(session);
    DemographicsDataSourceLoader.LoadDemographicsDataSource(session);

    // Retrieves the created element categories
    ElementCategory observedClimateCategory = (ElementCategory) (session.get(ElementCategory.class, 1));
    ElementCategory futureClimateCategory = (ElementCategory) (session.get(ElementCategory.class, 2));
    ElementCategory nonClimateCategory = (ElementCategory) (session.get(ElementCategory.class, 3));
    ElementCategory applicationsCategory = (ElementCategory) (session.get(ElementCategory.class, 4));
    // Retrieves the created  data sources
    DataSource dsCsiro = (DataSource) (session.get(DataSource.class, 1)); // ID 1 = CSIRO
    // Retrieves the created display types
    DisplayType tableDisplayType = (DisplayType) (session.get(DisplayType.class, 3)); // ID 3 = Table
    // Retrieves the created parameter options
    DataSourceParameterOption csiroVariableTemperature = (DataSourceParameterOption) (session
            .get(DataSourceParameterOption.class, 1)); // ID 3 = Temperature
    DataSourceParameterOption mediumEmScn = (DataSourceParameterOption) (session
            .get(DataSourceParameterOption.class, 5)); // ID 5 = Medium Emission Scenario
    DataSourceParameterOption year2055 = (DataSourceParameterOption) (session
            .get(DataSourceParameterOption.class, 8)); // ID 8 = Year 2055

    // Add Users
    User u1 = new User("testuser1", DEFAULT_PASSWORD, true, true, UserLoginService.ROLE_USER,
            "email@company.com", "testuser1", "testuser1");
    User u2 = new User("testuser2", DEFAULT_PASSWORD, true, true, UserLoginService.ROLE_USER,
            "email@company.com", "testuser2", "testuser2");
    User u3 = new User("testuser3", DEFAULT_PASSWORD, true, true, UserLoginService.ROLE_USER,
            "email@company.com", "testuser3", "testuser3");
    User u4 = new User("testuser4", DEFAULT_PASSWORD, true, true, UserLoginService.ROLE_USER,
            "email@company.com", "testuser4", "testuser4");
    User u5 = new User("testadmin1", DEFAULT_PASSWORD, true, true, UserLoginService.ROLE_ADMINISTRATOR,
            "email@company.com", "testadmin1", "testadmin1");
    User u6 = new User("testadmin2", DEFAULT_PASSWORD, true, true, UserLoginService.ROLE_ADMINISTRATOR,
            "email@company.com", "testadmin2", "testadmin2");
    session.save(u1);
    session.save(u2);
    session.save(u3);
    session.save(u4);
    session.save(u5);
    session.save(u6);

    DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
        date = dateFormatter.parse("2013-04-10");
    } catch (ParseException e) {
    }

    // Add Reports
    Report user1wb = new Report("User 1 Workboard", "Purpose", "private", u1, port1, null);
    user1wb.setCreationDate(date);

    Report user1us1 = new Report("User 1 Story 1", "Purpose", "private", u1, port2, null);
    user1us1.setCreationDate(date);

    Report user1us2 = new Report("User 1 Story 2 (Public)", "Purpose", "public", u1, port1, null);
    user1us2.setCreationDate(date);

    Report user1us3 = new Report("User 1 Story 3 (Published)", "Purpose", "public", u1, port1, null);
    user1us3.setCreationDate(date);
    user1us3.setElements(new ArrayList<Element>());

    Report user2wb = new Report("User 2 Workboard (Empty)", "Purpose", "private", u2, port2, null);
    user2wb.setCreationDate(date);

    Report user2us = new Report("User 2 Story (Empty)", "Purpose", "private", u2, port1, null);
    user2us.setCreationDate(date);

    session.save(user1wb);
    session.save(user1us1);
    session.save(user1us2);
    session.save(user1us3);
    session.save(user2wb);
    session.save(user2us);

    // Add Elements in the report
    date = new Date();
    String content = "This, is, a, test, for, CSV, Element";
    InputElement ie1 = new InputElement(date, "Test 1", nonClimateCategory, user1wb, true, 1, "text/csv",
            content.getBytes(), true, false);
    session.save(ie1);

    content = "This is the second test";
    InputElement ie2 = new InputElement(date, "Test 2", observedClimateCategory, user1wb, true, 1, "text/plain",
            content.getBytes(), true, false);
    session.save(ie2);

    content = "This is the third test";
    InputElement ie3 = new InputElement(date, "Test 3", futureClimateCategory, user1wb, true, 1, "text/plain",
            content.getBytes(), true, false);
    session.save(ie3);

    content = "Report's Data Element content test";
    InputElement ie4 = new InputElement(date, "Test 4", nonClimateCategory, user1us1, true, 1, "text/plain",
            content.getBytes(), true, false);
    session.save(ie4);

    InputElement text1 = new InputElement(date, "Comment text 1", nonClimateCategory, user1us1, true, 2,
            "text/plain", content.getBytes(), true, false);
    session.save(text1);

    InputElement ie5 = new InputElement(date, "Test 5", applicationsCategory, user1us1, true, 3, "text/plain",
            content.getBytes(), true, false);
    session.save(ie5);

    content = "This is a second text comment";
    InputElement text2 = new InputElement(date, "Comment text 2", futureClimateCategory, user1us1, true, 4,
            "text/plain", content.getBytes(), true, false);
    session.save(text2);

    InputElement ie6 = new InputElement(date, "Test 6", applicationsCategory, user1us1, true, 5, "text/plain",
            content.getBytes(), true, false);
    session.save(ie6);

    // CSIRO Data Element
    List<DataSourceParameterOption> selectedOptions = new ArrayList<DataSourceParameterOption>();
    selectedOptions.add(csiroVariableTemperature);
    selectedOptions.add(mediumEmScn);
    selectedOptions.add(year2055);
    DataElement deCsiro = new DataElement(date, "CSIRO Data Element Test", futureClimateCategory, user1wb, true,
            1, dsCsiro, selectedOptions, tableDisplayType, true, false);
    session.save(deCsiro);

    try {
        ReportPublication pub = new ReportPublication(user1us3);
        session.save(pub);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Commit all the transaction
    session.getTransaction().commit();

    System.out.println("Created TEST database schema with test data");
    System.out.println("DONE !");
}