use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class TransactionsTest method bmt.
@Test
public void bmt() {
//tag::transactions-api-bmt-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 {
// Assuming a JTA transaction is not already active,
// this call the TM/UT begin method. If a JTA
// transaction is already active, we remember that
// the Transaction associated with the Session did
// not "initiate" the JTA transaction and will later
// nop-op the commit and rollback calls...
session.getTransaction().begin();
session.persist(new Customer());
Customer customer = (Customer) session.createQuery("select c from Customer c").uniqueResult();
// calls TM/UT commit method, assuming we are initiator.
session.getTransaction().commit();
} catch (Exception e) {
// where the exception happened
if (session.getTransaction().getStatus() == TransactionStatus.ACTIVE || session.getTransaction().getStatus() == TransactionStatus.MARKED_ROLLBACK) {
// calls TM/UT commit method, assuming we are initiator;
// otherwise marks the JTA transaction for rollback only
session.getTransaction().rollback();
}
// handle the underlying error
} finally {
session.close();
sessionFactory.close();
}
//end::transactions-api-bmt-example[]
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class ThreadLocalSessionContext method bind.
/**
* Associates the given session with the current thread of execution.
*
* @param session The session to bind.
*/
public static void bind(org.hibernate.Session session) {
final SessionFactory factory = session.getSessionFactory();
cleanupAnyOrphanedSession(factory);
doBind(session, factory);
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class HibernateAnnotationMappingTest method testUniqueConstraintAnnotationOnNaturalIds.
@Test
@TestForIssue(jiraKey = "HHH-7446")
public void testUniqueConstraintAnnotationOnNaturalIds() throws Exception {
Configuration configuration = new Configuration();
configuration.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
configuration.addAnnotatedClass(Month.class);
SessionFactory sf = null;
try {
sf = configuration.buildSessionFactory();
sf.close();
} catch (ConcurrentModificationException e) {
fail(e.toString());
}
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class CriterionTest method testIlikeRendering.
@Test
public void testIlikeRendering() {
SessionFactory sf = new Configuration().addAnnotatedClass(IrrelevantEntity.class).setProperty(AvailableSettings.DIALECT, IlikeSupportingDialect.class.getName()).setProperty(Environment.HBM2DDL_AUTO, "create-drop").buildSessionFactory();
try {
final Criteria criteria = sf.openSession().createCriteria(IrrelevantEntity.class);
final CriteriaQueryTranslator translator = new CriteriaQueryTranslator((SessionFactoryImplementor) sf, (CriteriaImpl) criteria, IrrelevantEntity.class.getName(), "a");
final Criterion ilikeExpression = Restrictions.ilike("name", "abc");
final String ilikeExpressionSqlFragment = ilikeExpression.toSqlString(criteria, translator);
assertEquals("a.name insensitiveLike ?", ilikeExpressionSqlFragment);
} finally {
sf.close();
}
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class CriterionTest method testIlikeMimicing.
@Test
public void testIlikeMimicing() {
SessionFactory sf = new Configuration().addAnnotatedClass(IrrelevantEntity.class).setProperty(AvailableSettings.DIALECT, NonIlikeSupportingDialect.class.getName()).setProperty(Environment.HBM2DDL_AUTO, "create-drop").buildSessionFactory();
try {
final Criteria criteria = sf.openSession().createCriteria(IrrelevantEntity.class);
final CriteriaQueryTranslator translator = new CriteriaQueryTranslator((SessionFactoryImplementor) sf, (CriteriaImpl) criteria, IrrelevantEntity.class.getName(), "a");
final Criterion ilikeExpression = Restrictions.ilike("name", "abc");
final String ilikeExpressionSqlFragment = ilikeExpression.toSqlString(criteria, translator);
assertEquals("lowLowLow(a.name) like ?", ilikeExpressionSqlFragment);
} finally {
sf.close();
}
}
Aggregations