Search in sources :

Example 6 with Metadata

use of org.hibernate.boot.Metadata in project hibernate-orm by hibernate.

the class JBossStandaloneJtaExampleTest method buildSessionFactory.

private SessionFactory buildSessionFactory() {
    // Extra options located in src/test/resources/hibernate.properties
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySetting(Environment.DIALECT, "HSQL").applySetting(Environment.HBM2DDL_AUTO, "create-drop").applySetting(Environment.CONNECTION_PROVIDER, JtaAwareConnectionProviderImpl.class.getName()).applySetting(Environment.JNDI_CLASS, "org.jnp.interfaces.NamingContextFactory").applySetting(Environment.TRANSACTION_COORDINATOR_STRATEGY, JtaTransactionCoordinatorBuilderImpl.class.getName()).applySetting(Environment.CURRENT_SESSION_CONTEXT_CLASS, "jta").applySetting(Environment.RELEASE_CONNECTIONS, "auto").applySetting(Environment.USE_SECOND_LEVEL_CACHE, "true").applySetting(Environment.USE_QUERY_CACHE, "true").applySetting(Environment.JTA_PLATFORM, new JBossStandAloneJtaPlatform()).applySetting(Environment.CACHE_REGION_FACTORY, TestInfinispanRegionFactory.class.getName());
    StandardServiceRegistry serviceRegistry = ssrb.build();
    MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    metadataSources.addResource("org/hibernate/test/cache/infinispan/functional/entities/Item.hbm.xml");
    Metadata metadata = metadataSources.buildMetadata();
    for (PersistentClass entityBinding : metadata.getEntityBindings()) {
        if (entityBinding instanceof RootClass) {
            ((RootClass) entityBinding).setCacheConcurrencyStrategy("transactional");
        }
    }
    for (Collection collectionBinding : metadata.getCollectionBindings()) {
        collectionBinding.setCacheConcurrencyStrategy("transactional");
    }
    return metadata.buildSessionFactory();
}
Also used : RootClass(org.hibernate.mapping.RootClass) JtaAwareConnectionProviderImpl(org.hibernate.testing.jta.JtaAwareConnectionProviderImpl) JtaTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) Collection(org.hibernate.mapping.Collection) JBossStandAloneJtaPlatform(org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 7 with Metadata

use of org.hibernate.boot.Metadata 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 8 with Metadata

use of org.hibernate.boot.Metadata 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 9 with Metadata

use of org.hibernate.boot.Metadata in project hibernate-orm by hibernate.

the class SequenceHiLoGeneratorNoIncrementTest method setUp.

@Before
public void setUp() throws Exception {
    serviceRegistry = new StandardServiceRegistryBuilder().enableAutoClose().applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop").build();
    generator = new SequenceStyleGenerator();
    // Build the properties used to configure the id generator
    Properties properties = new Properties();
    properties.setProperty(SequenceStyleGenerator.SEQUENCE_PARAM, TEST_SEQUENCE);
    properties.setProperty(SequenceStyleGenerator.OPT_PARAM, "legacy-hilo");
    // JPA allocationSize of 1
    properties.setProperty(SequenceStyleGenerator.INCREMENT_PARAM, "0");
    properties.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, new ObjectNameNormalizer() {

        @Override
        protected MetadataBuildingContext getBuildingContext() {
            return new MetadataBuildingContextTestingImpl(serviceRegistry);
        }
    });
    generator.configure(StandardBasicTypes.LONG, properties, serviceRegistry);
    final Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
    generator.registerExportables(metadata.getDatabase());
    sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
    sequenceValueExtractor = new SequenceValueExtractor(sessionFactory.getDialect(), TEST_SEQUENCE);
}
Also used : MetadataBuildingContextTestingImpl(org.hibernate.testing.boot.MetadataBuildingContextTestingImpl) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) MetadataBuildingContext(org.hibernate.boot.spi.MetadataBuildingContext) ObjectNameNormalizer(org.hibernate.boot.model.naming.ObjectNameNormalizer) SequenceStyleGenerator(org.hibernate.id.enhanced.SequenceStyleGenerator) Properties(java.util.Properties) Before(org.junit.Before)

Example 10 with Metadata

use of org.hibernate.boot.Metadata in project hibernate-orm by hibernate.

the class SequenceHiLoGeneratorTest method setUp.

@Before
public void setUp() throws Exception {
    serviceRegistry = new StandardServiceRegistryBuilder().enableAutoClose().applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop").build();
    MetadataBuildingContext buildingContext = new MetadataBuildingContextTestingImpl(serviceRegistry);
    Properties properties = new Properties();
    properties.setProperty(SequenceGenerator.SEQUENCE, TEST_SEQUENCE);
    properties.setProperty(SequenceHiLoGenerator.MAX_LO, "3");
    properties.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, buildingContext.getObjectNameNormalizer());
    generator = new SequenceHiLoGenerator();
    generator.configure(StandardBasicTypes.LONG, properties, serviceRegistry);
    Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
    generator.registerExportables(metadata.getDatabase());
    sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
    sequenceValueExtractor = new SequenceValueExtractor(sessionFactory.getDialect(), TEST_SEQUENCE);
}
Also used : MetadataBuildingContextTestingImpl(org.hibernate.testing.boot.MetadataBuildingContextTestingImpl) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) MetadataBuildingContext(org.hibernate.boot.spi.MetadataBuildingContext) Properties(java.util.Properties) Before(org.junit.Before)

Aggregations

Metadata (org.hibernate.boot.Metadata)99 MetadataSources (org.hibernate.boot.MetadataSources)88 Test (org.junit.Test)73 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)46 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)41 PersistentClass (org.hibernate.mapping.PersistentClass)29 TestForIssue (org.hibernate.testing.TestForIssue)24 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)12 Property (org.hibernate.mapping.Property)11 SchemaCreatorImpl (org.hibernate.tool.schema.internal.SchemaCreatorImpl)11 Collection (org.hibernate.mapping.Collection)10 RootClass (org.hibernate.mapping.RootClass)10 Map (java.util.Map)9 HashMap (java.util.HashMap)8 SessionFactory (org.hibernate.SessionFactory)8 Session (org.hibernate.Session)7 Integrator (org.hibernate.integrator.spi.Integrator)7 SessionFactoryServiceRegistry (org.hibernate.service.spi.SessionFactoryServiceRegistry)7 ServiceRegistry (org.hibernate.service.ServiceRegistry)6 Properties (java.util.Properties)5