Search in sources :

Example 26 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder in project hibernate-orm by hibernate.

the class BootstrapTest method test_basic_custom_type_register_UserType_example.

@Test
public void test_basic_custom_type_register_UserType_example() {
    try {
        //tag::basic-custom-type-register-UserType-example[]
        ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().build();
        MetadataSources sources = new MetadataSources(standardRegistry);
        MetadataBuilder metadataBuilder = sources.getMetadataBuilder();
        metadataBuilder.applyBasicType(BitSetUserType.INSTANCE, "bitset");
    //end::basic-custom-type-register-UserType-example[]
    } catch (Exception ignore) {
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataBuilder(org.hibernate.boot.MetadataBuilder) MetadataSources(org.hibernate.boot.MetadataSources) SessionFactoryServiceRegistry(org.hibernate.service.spi.SessionFactoryServiceRegistry) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) ServiceRegistry(org.hibernate.service.ServiceRegistry) Test(org.junit.Test)

Example 27 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder in project hibernate-orm by hibernate.

the class TransactionsTest method cmt.

@Test
public void cmt() {
    //tag::transactions-api-cmt-example[]
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta").build();
    Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(Customer.class).getMetadataBuilder().build();
    SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
    // Note: depending on the JtaPlatform used and some optional settings,
    // the underlying transactions here will be controlled through either
    // the JTA TransactionManager or UserTransaction
    Session session = sessionFactory.openSession();
    try {
        // Since we are in CMT, a JTA transaction would
        // already have been started.  This call essentially
        // no-ops
        session.getTransaction().begin();
        Number customerCount = (Number) session.createQuery("select count(c) from Customer c").uniqueResult();
        // Since we did not start the transaction ( CMT ),
        // we also will not end it.  This call essentially
        // no-ops in terms of transaction handling.
        session.getTransaction().commit();
    } catch (Exception e) {
        // marking the underlying CMT transaction for rollback only).
        if (session.getTransaction().getStatus() == TransactionStatus.ACTIVE || session.getTransaction().getStatus() == TransactionStatus.MARKED_ROLLBACK) {
            session.getTransaction().rollback();
        }
    // handle the underlying error
    } finally {
        session.close();
        sessionFactory.close();
    }
//end::transactions-api-cmt-example[]
}
Also used : SessionFactory(org.hibernate.SessionFactory) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Session(org.hibernate.Session) Test(org.junit.Test)

Example 28 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder in project hibernate-orm by hibernate.

the class TransactionsTest method jdbc.

@Test
public void jdbc() {
    //tag::transactions-api-jdbc-example[]
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jdbc").build();
    Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(Customer.class).getMetadataBuilder().build();
    SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
    Session session = sessionFactory.openSession();
    try {
        // calls Connection#setAutoCommit( false ) to
        // signal start of transaction
        session.getTransaction().begin();
        session.createQuery("UPDATE customer set NAME = 'Sir. '||NAME").executeUpdate();
        // calls Connection#commit(), if an error
        // happens we attempt a rollback
        session.getTransaction().commit();
    } catch (Exception e) {
        // where the exception happened
        if (session.getTransaction().getStatus() == TransactionStatus.ACTIVE || session.getTransaction().getStatus() == TransactionStatus.MARKED_ROLLBACK) {
            session.getTransaction().rollback();
        }
    // handle the underlying error
    } finally {
        session.close();
        sessionFactory.close();
    }
//end::transactions-api-jdbc-example[]
}
Also used : SessionFactory(org.hibernate.SessionFactory) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Session(org.hibernate.Session) Test(org.junit.Test)

Example 29 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder in project hibernate-orm by hibernate.

the class Configuration method reset.

protected void reset() {
    implicitNamingStrategy = ImplicitNamingStrategyJpaCompliantImpl.INSTANCE;
    physicalNamingStrategy = PhysicalNamingStrategyStandardImpl.INSTANCE;
    namedQueries = new HashMap<String, NamedQueryDefinition>();
    namedSqlQueries = new HashMap<String, NamedSQLQueryDefinition>();
    sqlResultSetMappings = new HashMap<String, ResultSetMappingDefinition>();
    namedEntityGraphMap = new HashMap<String, NamedEntityGraphDefinition>();
    namedProcedureCallMap = new HashMap<String, NamedProcedureCallDefinition>();
    standardServiceRegistryBuilder = new StandardServiceRegistryBuilder(bootstrapServiceRegistry);
    entityTuplizerFactory = new EntityTuplizerFactory();
    interceptor = EmptyInterceptor.INSTANCE;
    properties = new Properties();
    properties.putAll(standardServiceRegistryBuilder.getSettings());
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) Properties(java.util.Properties) NamedProcedureCallDefinition(org.hibernate.cfg.annotations.NamedProcedureCallDefinition) NamedEntityGraphDefinition(org.hibernate.cfg.annotations.NamedEntityGraphDefinition) NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) EntityTuplizerFactory(org.hibernate.tuple.entity.EntityTuplizerFactory) ResultSetMappingDefinition(org.hibernate.engine.ResultSetMappingDefinition)

Example 30 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder in project hibernate-orm by hibernate.

the class ManagedProviderConnectionHelper method createServiceRegistry.

private static StandardServiceRegistryImpl createServiceRegistry(Properties properties) {
    Environment.verifyProperties(properties);
    ConfigurationHelper.resolvePlaceHolders(properties);
    return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings(properties).build();
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl)

Aggregations

StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)165 MetadataSources (org.hibernate.boot.MetadataSources)105 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)94 Test (org.junit.Test)92 Metadata (org.hibernate.boot.Metadata)46 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)33 PersistentClass (org.hibernate.mapping.PersistentClass)27 Before (org.junit.Before)26 Properties (java.util.Properties)24 TestForIssue (org.hibernate.testing.TestForIssue)23 SessionFactory (org.hibernate.SessionFactory)15 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)15 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)14 Property (org.hibernate.mapping.Property)14 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)14 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)13 File (java.io.File)12 Map (java.util.Map)12 HashMap (java.util.HashMap)11 Session (org.hibernate.Session)10