Search in sources :

Example 1 with RequiresDialect

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

the class ProcedureCallImplTest method testNoNullPointerExceptionThrown.

@Test
@TestForIssue(jiraKey = "HHH-13644")
@RequiresDialect(H2Dialect.class)
public void testNoNullPointerExceptionThrown(EntityManagerFactoryScope scope) {
    scope.inTransaction(em -> {
        em.createNativeQuery("CREATE ALIAS GET_RANDOM_VALUE FOR \"java.lang.Math.random\";").executeUpdate();
        Query query = em.createStoredProcedureQuery("GET_RANDOM_VALUE");
        Stream stream = query.getResultStream();
        Assert.assertEquals(1, stream.count());
    });
}
Also used : Query(jakarta.persistence.Query) Stream(java.util.stream.Stream) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue) RequiresDialect(org.hibernate.testing.orm.junit.RequiresDialect)

Example 2 with RequiresDialect

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

the class LobStringTest method testUsingStringLobAnnotatedPropertyInNativeQuery.

@Test
@TestForIssue(jiraKey = "HHH-11477")
@RequiresDialect(PostgreSQLDialect.class)
public void testUsingStringLobAnnotatedPropertyInNativeQuery(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        final List<TestEntity> results = session.createNativeQuery("select te.* " + "from test_entity te " + "where lower(convert_from(lo_get(cast(te.firstLobField as oid)), 'UTF8')) LIKE :value", TestEntity.class).setParameter("value", value1).getResultList();
        assertThat(results.size(), is(1));
        final TestEntity testEntity = results.get(0);
        assertThat(testEntity.getFirstLobField(), is(value1));
        assertThat(testEntity.getSecondLobField(), is(value2));
        final Clob clobField = testEntity.getClobField();
        try {
            assertThat(clobField.getSubString(1, (int) clobField.length()), is(value2));
        } catch (SQLException e) {
            fail(e.getMessage());
        }
    });
}
Also used : SQLException(java.sql.SQLException) Clob(java.sql.Clob) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue) RequiresDialect(org.hibernate.testing.orm.junit.RequiresDialect)

Example 3 with RequiresDialect

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

the class NativeSQLQueriesTest method testEscapeColonInSQL.

@Test
@RequiresDialect(value = MySQLDialect.class, majorVersion = 5)
public void testEscapeColonInSQL(SessionFactoryScope scope) throws QueryException {
    scope.inTransaction(session -> {
        NativeQuery query = session.createNativeQuery("SELECT @row \\:= 1");
        List list = query.list();
        assertTrue(list.get(0).toString().equals("1"));
    });
}
Also used : NativeQuery(org.hibernate.query.NativeQuery) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) RequiresDialect(org.hibernate.testing.orm.junit.RequiresDialect)

Example 4 with RequiresDialect

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

the class NamedQueryCommentTest method testUpdateNamedNativeQueryWithQueryHintUsingIndex.

@Test
@RequiresDialect(H2Dialect.class)
public void testUpdateNamedNativeQueryWithQueryHintUsingIndex(EntityManagerFactoryScope scope) {
    scope.inTransaction(entityManager -> {
        statementInspector.clear();
        Query query = entityManager.createNamedQuery("UpdateNamedNativeQuery");
        query.setParameter("title", GAME_TITLES[0]);
        query.setParameter("id", 1L);
        query.unwrap(org.hibernate.query.Query.class).addQueryHint("INDEX (game idx_game_id)");
        int updateCount = query.executeUpdate();
        assertEquals(1, updateCount);
        statementInspector.assertExecutedCount(1);
        statementInspector.assertExecuted("/* COMMENT_INDEX_game_title */ update game set title = ? where id = ?");
    });
}
Also used : TypedQuery(jakarta.persistence.TypedQuery) Query(jakarta.persistence.Query) NamedNativeQuery(org.hibernate.annotations.NamedNativeQuery) NamedQuery(org.hibernate.annotations.NamedQuery) Test(org.junit.jupiter.api.Test) RequiresDialect(org.hibernate.testing.orm.junit.RequiresDialect)

Example 5 with RequiresDialect

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

the class NativeQueryOrdinalParametersTest method testCteNativeQueryOrdinalParameter.

@Test
@TestForIssue(jiraKey = "HHH-12532")
// Add RequiresDialect be Cockroach version 201
@RequiresDialect(value = PostgreSQLDialect.class, majorVersion = 8, minorVersion = 2)
@RequiresDialect(value = CockroachDialect.class, majorVersion = 20, minorVersion = 1)
public void testCteNativeQueryOrdinalParameter(EntityManagerFactoryScope scope) {
    Node root1 = new Node();
    root1.setCode("ABC");
    Node root2 = new Node();
    root2.setCode("DEF");
    Node node11 = new Node();
    node11.setParent(root1);
    Node node21 = new Node();
    node21.setParent(root2);
    Node node211 = new Node();
    node211.setParent(node21);
    scope.inTransaction(entityManager -> {
        entityManager.persist(root1);
        entityManager.persist(root2);
        entityManager.persist(node11);
        entityManager.persist(node21);
        entityManager.persist(node211);
    });
    scope.inTransaction(entityManager -> {
        Query cte = entityManager.createNativeQuery("WITH RECURSIVE CTE(id, parent_id) AS ( " + "  SELECT id, parent_id " + "  FROM Node " + "  WHERE code like ?1 and parent_id is null" + "  UNION ALL " + "  SELECT child.id, child.parent_id " + "  FROM Node child  " + "  JOIN CTE cte " + "  ON cte.id = child.parent_id " + ") SELECT DISTINCT id as integer " + "  FROM CTE cte");
        List<Long> root1Ids = cte.setParameter(1, "AB%").getResultList();
        assertEquals(2, root1Ids.size());
        assertTrue(root1Ids.contains(root1.getId()));
        assertTrue(root1Ids.contains(node11.getId()));
        List<Long> root2Ids = cte.setParameter(1, "DE%").getResultList();
        assertEquals(3, root2Ids.size());
        assertTrue(root2Ids.contains(root2.getId()));
        assertTrue(root2Ids.contains(node21.getId()));
        assertTrue(root2Ids.contains(node211.getId()));
    });
}
Also used : Query(jakarta.persistence.Query) NativeQuery(org.hibernate.query.NativeQuery) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue) RequiresDialect(org.hibernate.testing.orm.junit.RequiresDialect)

Aggregations

RequiresDialect (org.hibernate.testing.orm.junit.RequiresDialect)12 Test (org.junit.jupiter.api.Test)12 Query (jakarta.persistence.Query)5 TestForIssue (org.hibernate.testing.TestForIssue)4 TypedQuery (jakarta.persistence.TypedQuery)3 NamedNativeQuery (org.hibernate.annotations.NamedNativeQuery)3 NamedQuery (org.hibernate.annotations.NamedQuery)3 NativeQuery (org.hibernate.query.NativeQuery)2 HibernateCriteriaBuilder (org.hibernate.query.criteria.HibernateCriteriaBuilder)2 SkipForDialect (org.hibernate.testing.SkipForDialect)2 BasicEntity (org.hibernate.testing.orm.domain.gambit.BasicEntity)2 Clob (java.sql.Clob)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Stream (java.util.stream.Stream)1 Transaction (org.hibernate.Transaction)1 BaseSessionFactoryFunctionalTest (org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest)1