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[]
}
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();
}
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());
}
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();
}
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();
}
}
}
Aggregations