Search in sources :

Example 26 with RequiresDialect

use of org.hibernate.testing.RequiresDialect in project hibernate-orm by hibernate.

the class PessimisticWriteLockTimeoutTest method testSkipLocked.

@Test
@RequiresDialect({ Oracle8iDialect.class, PostgreSQL95Dialect.class })
public void testSkipLocked() throws NoSuchFieldException, IllegalAccessException {
    Session session = sessionFactory().openSession();
    session.beginTransaction();
    try {
        session.createQuery("select a from A a", A.class).unwrap(org.hibernate.query.Query.class).setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE).setTimeOut(LockOptions.SKIP_LOCKED)).list();
        String lockingQuery = sqlStatementInterceptor.getSqlQueries().getLast();
        assertTrue(lockingQuery.toLowerCase().contains("skip locked"));
    } finally {
        session.getTransaction().commit();
        session.close();
    }
}
Also used : LockOptions(org.hibernate.LockOptions) Session(org.hibernate.Session) Test(org.junit.Test) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 27 with RequiresDialect

use of org.hibernate.testing.RequiresDialect in project hibernate-orm by hibernate.

the class OrderByTest method testCriteriaNullsFirstLast.

@Test
@TestForIssue(jiraKey = "HHH-465")
@RequiresDialect(value = { H2Dialect.class, MySQLDialect.class, SQLServer2008Dialect.class }, comment = "By default H2 places NULL values first, so testing 'NULLS LAST' expression. " + "For MySQL and SQL Server 2008 testing overridden Dialect#renderOrderByElement(String, String, String, NullPrecedence) method. " + "MySQL and SQL Server 2008 does not support NULLS FIRST / LAST syntax at the moment, so transforming the expression to 'CASE WHEN ...'.")
public void testCriteriaNullsFirstLast() {
    Session session = openSession();
    // Populating database with test data.
    session.getTransaction().begin();
    Zoo zoo1 = new Zoo(null);
    Zoo zoo2 = new Zoo("Warsaw ZOO");
    session.persist(zoo1);
    session.persist(zoo2);
    session.getTransaction().commit();
    session.clear();
    session.getTransaction().begin();
    Criteria criteria = session.createCriteria(Zoo.class);
    criteria.addOrder(org.hibernate.criterion.Order.asc("name").nulls(NullPrecedence.LAST));
    Iterator<Zoo> iterator = (Iterator<Zoo>) criteria.list().iterator();
    Assert.assertEquals(zoo2.getName(), iterator.next().getName());
    Assert.assertNull(iterator.next().getName());
    session.getTransaction().commit();
    session.clear();
    // Cleanup data.
    session.getTransaction().begin();
    session.delete(zoo1);
    session.delete(zoo2);
    session.getTransaction().commit();
    session.close();
}
Also used : Iterator(java.util.Iterator) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 28 with RequiresDialect

use of org.hibernate.testing.RequiresDialect in project hibernate-orm by hibernate.

the class OrderByTest method testNullsFirstLastSpawnMultipleColumns.

@Test
@TestForIssue(jiraKey = "HHH-465")
@RequiresDialect(value = { H2Dialect.class, MySQLDialect.class, SQLServer2008Dialect.class }, comment = "By default H2 places NULL values first, so testing 'NULLS LAST' expression. " + "For MySQL and SQL Server 2008 testing overridden Dialect#renderOrderByElement(String, String, String, NullPrecedence) method. " + "MySQL and SQL Server 2008 does not support NULLS FIRST / LAST syntax at the moment, so transforming the expression to 'CASE WHEN ...'.")
public void testNullsFirstLastSpawnMultipleColumns() {
    Session session = openSession();
    // Populating database with test data.
    session.getTransaction().begin();
    Zoo zoo = new Zoo();
    zoo.setName("Berlin ZOO");
    Visitor visitor1 = new Visitor(null, null);
    Visitor visitor2 = new Visitor(null, "Antoniak");
    Visitor visitor3 = new Visitor("Lukasz", "Antoniak");
    zoo.getVisitors().add(visitor1);
    zoo.getVisitors().add(visitor2);
    zoo.getVisitors().add(visitor3);
    session.save(zoo);
    session.save(visitor1);
    session.save(visitor2);
    session.save(visitor3);
    session.getTransaction().commit();
    session.clear();
    session.getTransaction().begin();
    zoo = (Zoo) session.get(Zoo.class, zoo.getId());
    Iterator<Visitor> iterator = zoo.getVisitors().iterator();
    Assert.assertEquals(3, zoo.getVisitors().size());
    Assert.assertEquals(visitor3, iterator.next());
    Assert.assertEquals(visitor2, iterator.next());
    Assert.assertEquals(visitor1, iterator.next());
    session.getTransaction().commit();
    session.clear();
    // Cleanup data.
    session.getTransaction().begin();
    session.delete(visitor1);
    session.delete(visitor2);
    session.delete(visitor3);
    session.delete(zoo);
    session.getTransaction().commit();
    session.close();
}
Also used : Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 29 with RequiresDialect

use of org.hibernate.testing.RequiresDialect in project hibernate-orm by hibernate.

the class ComponentTest method testComponentQueryMethodNoParensFailureExpected.

@Test
@RequiresDialect(value = SybaseASE15Dialect.class)
@FailureExpected(jiraKey = "HHH-3150")
public void testComponentQueryMethodNoParensFailureExpected() {
    // Sybase should translate "current_timestamp" in HQL to "getdate()";
    // This fails currently due to HHH-3510. The following test should be
    // deleted and testComponentQueries() should be updated (as noted
    // in that test case) when HHH-3510 is fixed.
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Employee emp = new Employee();
    emp.setHireDate(new Date());
    emp.setPerson(new Person());
    emp.getPerson().setName("steve");
    emp.getPerson().setDob(new Date());
    s.save(emp);
    s.createQuery("from Employee e where e.person = ('steve', current_timestamp)").list();
    s.delete(emp);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 30 with RequiresDialect

use of org.hibernate.testing.RequiresDialect in project hibernate-orm by hibernate.

the class SQLTest method test_sql_jpa_entity_associations_query_one_to_many_join_example.

@Test
@RequiresDialect(H2Dialect.class)
@RequiresDialect(Oracle8iDialect.class)
@RequiresDialect(PostgreSQL82Dialect.class)
public void test_sql_jpa_entity_associations_query_one_to_many_join_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        List<Phone> phones = entityManager.createNativeQuery("SELECT * " + "FROM Phone ph " + "JOIN phone_call c ON c.phone_id = ph.id", Phone.class).getResultList();
        for (Phone phone : phones) {
            List<Call> calls = phone.getCalls();
        }
        assertEquals(2, phones.size());
    });
}
Also used : Call(org.hibernate.userguide.model.Call) Phone(org.hibernate.userguide.model.Phone) Test(org.junit.Test) RequiresDialect(org.hibernate.testing.RequiresDialect)

Aggregations

RequiresDialect (org.hibernate.testing.RequiresDialect)36 Test (org.junit.Test)34 Session (org.hibernate.Session)18 TestForIssue (org.hibernate.testing.TestForIssue)13 EntityManager (javax.persistence.EntityManager)9 RequiresDialectFeature (org.hibernate.testing.RequiresDialectFeature)7 Callable (java.util.concurrent.Callable)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Transaction (org.hibernate.Transaction)6 HashMap (java.util.HashMap)5 Phone (org.hibernate.userguide.model.Phone)5 LockTimeoutException (javax.persistence.LockTimeoutException)4 List (java.util.List)3 LockOptions (org.hibernate.LockOptions)3 FailureExpected (org.hibernate.testing.FailureExpected)3 Call (org.hibernate.userguide.model.Call)3 Query (javax.persistence.Query)2 QueryTimeoutException (javax.persistence.QueryTimeoutException)2 MetadataSources (org.hibernate.boot.MetadataSources)2 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)2