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