Search in sources :

Example 66 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 67 with Metadata

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

the class MetadataTest method testBuildingMetamodelWithParameterizedCollection.

@Test
@SuppressWarnings({ "unchecked" })
public void testBuildingMetamodelWithParameterizedCollection() {
    Metadata metadata = new MetadataSources().addAnnotatedClass(WithGenericCollection.class).buildMetadata();
    SessionFactoryImplementor sfi = (SessionFactoryImplementor) metadata.buildSessionFactory();
    MetamodelImpl metamodel = new MetamodelImpl(sfi, ((MetadataImplementor) metadata).getTypeConfiguration());
    metamodel.initialize((MetadataImplementor) metadata, JpaMetaModelPopulationSetting.IGNORE_UNSUPPORTED);
    sfi.close();
}
Also used : MetamodelImpl(org.hibernate.metamodel.internal.MetamodelImpl) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) Test(org.junit.Test)

Example 68 with Metadata

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

the class AbstractCharsetNamingStrategyTest method testWithCustomNamingStrategy.

@Test
public void testWithCustomNamingStrategy() throws Exception {
    Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(Address.class).addAnnotatedClass(Person.class).getMetadataBuilder().applyImplicitNamingStrategy(new LongIdentifierNamingStrategy()).build();
    UniqueKey uniqueKey = metadata.getEntityBinding(Address.class.getName()).getTable().getUniqueKeyIterator().next();
    assertEquals(expectedUniqueKeyName(), uniqueKey.getName());
    org.hibernate.mapping.ForeignKey foreignKey = (org.hibernate.mapping.ForeignKey) metadata.getEntityBinding(Address.class.getName()).getTable().getForeignKeyIterator().next();
    assertEquals(expectedForeignKeyName(), foreignKey.getName());
    org.hibernate.mapping.Index index = metadata.getEntityBinding(Address.class.getName()).getTable().getIndexIterator().next();
    assertEquals(expectedIndexName(), index.getName());
}
Also used : LongIdentifierNamingStrategy(org.hibernate.test.annotations.namingstrategy.LongIdentifierNamingStrategy) UniqueKey(org.hibernate.mapping.UniqueKey) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) Test(org.junit.Test)

Example 69 with Metadata

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

the class RefreshUpdatedDataTest method acquireResources.

@Before
@SuppressWarnings("unused")
public void acquireResources() {
    final StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().configure("hibernate-config/hibernate.cfg.xml");
    if (H2Dialect.class.equals(Dialect.getDialect().getClass())) {
        ssrb.applySetting(AvailableSettings.URL, "jdbc:h2:mem:db-mvcc;MVCC=true");
    }
    ssrb.applySetting(AvailableSettings.GENERATE_STATISTICS, "true");
    serviceRegistry = ssrb.configure("hibernate-config/hibernate.cfg.xml").applySetting(AvailableSettings.HBM2DDL_DATABASE_ACTION, Action.CREATE).build();
    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    metadataSources.addAnnotatedClass(ReadWriteCacheableItem.class);
    metadataSources.addAnnotatedClass(ReadWriteVersionedCacheableItem.class);
    metadataSources.addAnnotatedClass(NonStrictReadWriteCacheableItem.class);
    metadataSources.addAnnotatedClass(NonStrictReadWriteVersionedCacheableItem.class);
    final Metadata metadata = metadataSources.buildMetadata();
    TestHelper.createRegions(metadata, true);
    sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) Before(org.junit.Before)

Example 70 with Metadata

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

the class NegativeValueSequenceTest method testNegativeTwoAllocationSizeNoopOptimizer.

@Test
@TestForIssue(jiraKey = "HHH-5933")
public void testNegativeTwoAllocationSizeNoopOptimizer() {
    ServiceRegistryImplementor serviceRegistry = null;
    SessionFactoryImplementor sessionFactory = null;
    Session session = null;
    try {
        serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop").build();
        Triggerable triggerable = logInspection.watchForLogMessages("HHH000116");
        Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(NegativeTwoIncrementSize.class).buildMetadata();
        // NegativeTwoIncrementSize ID has allocationSize == -2, so warning should be triggered.
        assertEquals(true, triggerable.wasTriggered());
        sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
        assertOptimizer(sessionFactory, NegativeTwoIncrementSize.class, NoopOptimizer.class);
        session = sessionFactory.openSession();
        session.getTransaction().begin();
        // (since negative NoopOptimizer negative default is -1)
        for (Integer i = -10; i >= -15; i--) {
            NegativeTwoIncrementSize theEntity = new NegativeTwoIncrementSize();
            session.persist(theEntity);
            assertEquals(i, theEntity.id);
        }
    } finally {
        if (session != null) {
            session.getTransaction().rollback();
            session.close();
        }
        if (sessionFactory != null) {
            sessionFactory.close();
        }
        if (serviceRegistry != null) {
            serviceRegistry.destroy();
        }
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) ServiceRegistryImplementor(org.hibernate.service.spi.ServiceRegistryImplementor) Triggerable(org.hibernate.testing.logger.Triggerable) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

Metadata (org.hibernate.boot.Metadata)148 MetadataSources (org.hibernate.boot.MetadataSources)124 Test (org.junit.Test)103 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)73 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)56 PersistentClass (org.hibernate.mapping.PersistentClass)46 TestForIssue (org.hibernate.testing.TestForIssue)31 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)18 Session (org.hibernate.Session)14 HashMap (java.util.HashMap)13 SchemaCreatorImpl (org.hibernate.tool.schema.internal.SchemaCreatorImpl)13 Property (org.hibernate.mapping.Property)12 RootClass (org.hibernate.mapping.RootClass)12 Map (java.util.Map)11 IdentifierGenerator (org.hibernate.id.IdentifierGenerator)11 SessionFactory (org.hibernate.SessionFactory)10 Collection (org.hibernate.mapping.Collection)10 ServiceRegistryImplementor (org.hibernate.service.spi.ServiceRegistryImplementor)10 ServiceRegistry (org.hibernate.service.ServiceRegistry)8 Before (org.junit.Before)8