Example usage for org.hibernate.boot.registry StandardServiceRegistryBuilder StandardServiceRegistryBuilder

List of usage examples for org.hibernate.boot.registry StandardServiceRegistryBuilder StandardServiceRegistryBuilder

Introduction

In this page you can find the example usage for org.hibernate.boot.registry StandardServiceRegistryBuilder StandardServiceRegistryBuilder.

Prototype

public StandardServiceRegistryBuilder() 

Source Link

Document

Create a default builder.

Usage

From source file:com.yahoo.elide.datastores.hibernate5.HibernateEntityManagerDataStoreSupplier.java

License:Apache License

@Override
public DataStore get() {
    // Add additional checks to our static check mappings map.
    // NOTE: This is a bit hacky. We need to do a major overhaul on our test architecture
    TestCheckMappings.MAPPINGS.put("filterCheck", Filtered.FilterCheck.class);
    TestCheckMappings.MAPPINGS.put("filterCheck3", Filtered.FilterCheck3.class);

    Map<String, Object> options = new HashMap<>();
    ArrayList<Class> bindClasses = new ArrayList<>();

    try {//from   w w  w .  ja v  a2 s .c  o m
        bindClasses.addAll(ClassScanner.getAnnotatedClasses(Parent.class.getPackage(), Entity.class));
    } catch (MappingException e) {
        throw new IllegalStateException(e);
    }

    options.put("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver");
    options.put("javax.persistence.jdbc.url",
            JDBC_PREFIX + System.getProperty(MYSQL_PORT_PROPERTY, MYSQL_PORT) + JDBC_SUFFIX);
    options.put("javax.persistence.jdbc.user", ROOT);
    options.put("javax.persistence.jdbc.password", ROOT);
    options.put(AvailableSettings.LOADED_CLASSES, bindClasses);

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("elide-tests", options);
    HibernateEntityManager em = (HibernateEntityManager) emf.createEntityManager();

    // method to force class initialization
    MetadataSources metadataSources = new MetadataSources(new StandardServiceRegistryBuilder()
            .configure("hibernate.cfg.xml").applySetting(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread")
            .applySetting(Environment.URL,
                    JDBC_PREFIX + System.getProperty(MYSQL_PORT_PROPERTY, MYSQL_PORT) + JDBC_SUFFIX)
            .applySetting(Environment.USER, ROOT).applySetting(Environment.PASS, ROOT).build());

    try {
        ClassScanner.getAnnotatedClasses(Parent.class.getPackage(), Entity.class)
                .forEach(metadataSources::addAnnotatedClass);
    } catch (MappingException e) {
        throw new IllegalStateException(e);
    }

    MetadataImplementor metadataImplementor = (MetadataImplementor) metadataSources.buildMetadata();

    EnumSet<TargetType> type = EnumSet.of(TargetType.DATABASE);
    // create example tables from beans
    SchemaExport schemaExport = new SchemaExport();
    schemaExport.drop(type, metadataImplementor);
    schemaExport.execute(type, SchemaExport.Action.CREATE, metadataImplementor);

    if (!schemaExport.getExceptions().isEmpty()) {
        throw new IllegalStateException(schemaExport.getExceptions().toString());
    }

    return new AbstractHibernateStore.Builder(em).withScrollEnabled(true)
            .withScrollMode(ScrollMode.FORWARD_ONLY).build();
}

From source file:com.yahoo.elide.datastores.multiplex.bridgeable.BridgeableStoreSupplier.java

License:Apache License

@Override
public DataStore get() {
    // method to force class initialization
    MetadataSources metadataSources = new MetadataSources(new StandardServiceRegistryBuilder()
            .configure("hibernate.cfg.xml").applySetting(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread")
            .applySetting(Environment.URL,
                    "jdbc:mysql://localhost:" + System.getProperty("mysql.port", "3306")
                            + "/root?serverTimezone=UTC")
            .applySetting(Environment.USER, "root").applySetting(Environment.PASS, "root").build());

    metadataSources.addAnnotatedClass(HibernateUser.class);

    MetadataImplementor metadataImplementor = (MetadataImplementor) metadataSources.buildMetadata();

    // create example tables from beans
    SchemaExport schemaExport = new SchemaExport(metadataImplementor); //.setHaltOnError(true);
    schemaExport.drop(false, true);/*  w w w  . j  ava  2s.co  m*/
    schemaExport.execute(false, true, false, true);

    if (!schemaExport.getExceptions().isEmpty()) {
        throw new RuntimeException(schemaExport.getExceptions().toString());
    }

    LATEST_HIBERNATE_STORE = new HibernateStore.Builder(metadataImplementor.buildSessionFactory())
            .withScrollEnabled(true).withScrollMode(ScrollMode.FORWARD_ONLY).build();

    BridgeableRedisStore hbaseStore = new BridgeableRedisStore();

    return new MultiplexManager(LATEST_HIBERNATE_STORE, hbaseStore);
}

From source file:com.yahoo.elide.standalone.Util.java

License:Apache License

/**
 * Retrieve a hibernate session factory.
 *
 * @param hibernate5ConfigPath File path to hibernate config (i.e. hibernate-cfg.xml)
 * @param modelPackageName Name of package containing all models to be loaded by hibernate
 * @return Hibernate session factory.// w ww .  ja  v a 2s  .  c  om
 */
public static SessionFactory getSessionFactory(String hibernate5ConfigPath, String modelPackageName) {
    StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
            .configure(new File(hibernate5ConfigPath)).build();
    MetadataSources sources = new MetadataSources(standardRegistry);

    getAllEntities(modelPackageName).forEach(sources::addAnnotatedClass);

    Metadata metaData = sources.getMetadataBuilder().build();
    return metaData.getSessionFactoryBuilder().build();
}

From source file:com.zanvork.guildhub.model.dao.HibernateMySQLDAO.java

public static SessionFactory getSessionFactory(String dbconnect) {

    if (sessionFactory != null && sessionFactory.containsKey(dbconnect)) {
        return sessionFactory.get(dbconnect);
    }// w w  w. j  a  va  2 s . c  o m

    try {
        synchronized (sessionFactory) {
            Configuration configuration = new Configuration().configure();

            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();
            SessionFactory sf = configuration.buildSessionFactory(serviceRegistry);
            sessionFactory.put(dbconnect, sf);
        }
    } catch (HibernateException ex) {
        throw new ExceptionInInitializerError(ex);
    }
    return sessionFactory.get(dbconnect);
}

From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java

License:Apache License

protected Object executeWithConnection(Callback c) throws SQLException {
    StandardServiceRegistryImpl serviceRegistry = null;
    Connection connection = null;
    try {/*  www.  j a va2s.  c  o m*/
        serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder()
                .applySettings(connectionProperties).build();
        connection = serviceRegistry.getService(ConnectionProvider.class).getConnection();

        conditionalBuildMappings();

        return c.execute(connection);
    } finally {
        if (serviceRegistry != null) {
            serviceRegistry.getService(ConnectionProvider.class).closeConnection(connection);
            serviceRegistry.destroy();
        }
    }
}

From source file:compositekey.savedata.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    /* TODO output your page here. You may use following sample code. */

    Configuration con = new Configuration();
    con.configure("hibernate.cfg.xml");
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(con.getProperties()).build();
    SessionFactory sf = con.buildSessionFactory(serviceRegistry);
    Session s = sf.openSession();/*  w w w .j  a v a2 s  .  com*/

    Transaction tr = s.beginTransaction();

    Compositetesting com = new Compositetesting();

    com.getId().setFname("ishwar");
    com.getId().setName("Rohit");
    com.setId_1(10);
    com.setSalary(100);
    s.save(com);
    tr.commit();

}

From source file:Conexion.NewHibernateUtil.java

public static SessionFactory getSessionFactory() {

    if (sessionFactory == null) {
        // loads configuration and mappings
        Configuration configuration = new Configuration().configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();

        // builds a session factory from the service registry
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    }//from  ww w. jav  a  2  s  . c o  m

    return sessionFactory;
}

From source file:Connect.DeleteDao.java

public static int delete(GS_Movie u) {
    int i = 0;/*from w ww . j av a 2 s  . c  o  m*/
    Configuration configuration = new Configuration();
    configuration.configure();
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    Session session = sessionFactory.openSession();
    Transaction t = null;
    try {
        t = session.beginTransaction();
        // t.begin();
        session.delete(u);
        t.commit();
    } catch (Exception ex) {
        if (t != null) {
            t.rollback();
        }
        ex.printStackTrace();
    } finally {
        session.close();
    }
    return i;
}

From source file:Connect.HistoryDao.java

public static int add(GS_History u) {
    int i = 0;//www .  j ava 2  s  .  c  o  m
    Configuration configuration = new Configuration();
    configuration.configure();
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    Session session = sessionFactory.openSession();
    Transaction t = null;
    try {
        t = session.beginTransaction();
        // t.begin();
        i = (Integer) session.save(u);
        t.commit();
    } catch (Exception ex) {
        if (t != null) {
            t.rollback();
        }
        ex.printStackTrace();
    } finally {
        session.close();
    }
    return i;
}

From source file:Connect.HistoryResDao.java

public static int add(GS_HistoryRes u) {
    int i = 0;/*  w  w  w . j a v  a  2  s. c om*/
    Configuration configuration = new Configuration();
    configuration.configure();
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    Session session = sessionFactory.openSession();
    Transaction t = null;
    try {
        t = session.beginTransaction();
        // t.begin();
        i = (Integer) session.save(u);
        t.commit();
    } catch (Exception ex) {
        if (t != null) {
            t.rollback();
        }
        ex.printStackTrace();
    } finally {
        session.close();
    }
    return i;
}