Search in sources :

Example 26 with Query

use of org.hibernate.query.Query in project hibernate-orm by hibernate.

the class PostgreSqlLobStringTest method testBadClobDataSavedAsStringworksAfterUpdate.

@Test
public void testBadClobDataSavedAsStringworksAfterUpdate() {
    doInHibernate(this::sessionFactory, session -> {
        session.doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("update test_entity\n" + "set \n" + "    clobfield = lo_from_bytea(0, cast(clobfield as bytea)),\n" + "    firstlobfield = lo_from_bytea(0, cast(firstlobfield as bytea)),\n" + "    secondlobfield = lo_from_bytea(0, cast(secondlobfield as bytea))");
            }
        });
    });
    doInHibernate(this::sessionFactory, session -> {
        final Query query = session.createQuery("from TestEntity");
        final List<TestEntity> results = query.list();
        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(value3));
        } catch (SQLException e) {
            fail(e.getMessage());
        }
    });
}
Also used : Query(org.hibernate.query.Query) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Clob(java.sql.Clob) Test(org.junit.Test)

Example 27 with Query

use of org.hibernate.query.Query in project hibernate-orm by hibernate.

the class PostgreSqlLobStringTest method testBadClobDataSavedAsStringFails.

@Test
public void testBadClobDataSavedAsStringFails() {
    try {
        doInHibernate(this::sessionFactory, session -> {
            final Query query = session.createQuery("from TestEntity");
            final List<TestEntity> results = query.list();
            fail("Exception thrown expected");
        });
    } catch (Exception e) {
        Exception rootException = (Exception) ExceptionUtil.rootCause(e);
        assertTrue(rootException.getMessage().startsWith("Bad value for type long"));
    }
}
Also used : Query(org.hibernate.query.Query) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 28 with Query

use of org.hibernate.query.Query in project hibernate-orm by hibernate.

the class ClosedManagerTests method testQuerySetNamedParameter.

@Test
public void testQuerySetNamedParameter() {
    final Session session = sessionFactory().openSession();
    final Query qry = session.createQuery("select i from Item i where i.id = :id");
    session.close();
    assertThat(session.isOpen(), CoreMatchers.is(false));
    try {
        qry.setParameter("id", 1);
        fail("Expecting call to fail");
    } catch (IllegalStateException expected) {
    }
}
Also used : Query(org.hibernate.query.Query) Session(org.hibernate.Session) AbstractJPATest(org.hibernate.test.jpa.AbstractJPATest) Test(org.junit.Test)

Example 29 with Query

use of org.hibernate.query.Query in project hibernate-orm by hibernate.

the class ClosedManagerTests method testQuerySetMaxResults.

@Test
public void testQuerySetMaxResults() {
    final Session session = sessionFactory().openSession();
    final Query qry = session.createQuery("select i from Item i");
    session.close();
    assertThat(session.isOpen(), CoreMatchers.is(false));
    try {
        qry.setMaxResults(1);
        fail("Expecting call to fail");
    } catch (IllegalStateException expected) {
    }
}
Also used : Query(org.hibernate.query.Query) Session(org.hibernate.Session) AbstractJPATest(org.hibernate.test.jpa.AbstractJPATest) Test(org.junit.Test)

Example 30 with Query

use of org.hibernate.query.Query in project hibernate-orm by hibernate.

the class EntitiesModifiedAtRevisionQuery method list.

@Override
public List list() {
    /*
         * The query that we need to create:
         *   SELECT new list(e) FROM versionsReferencedEntity e
         *   WHERE
         * (all specified conditions, transformed, on the "e" entity) AND
         * e.revision = :revision
         */
    AuditEntitiesConfiguration verEntCfg = enversService.getAuditEntitiesConfiguration();
    String revisionPropertyPath = verEntCfg.getRevisionNumberPath();
    qb.getRootParameters().addWhereWithParam(revisionPropertyPath, "=", revision);
    // all specified conditions
    for (AuditCriterion criterion : criterions) {
        criterion.addToQuery(enversService, versionsReader, aliasToEntityNameMap, QueryConstants.REFERENCED_ENTITY_ALIAS, qb, qb.getRootParameters());
    }
    for (final AuditAssociationQueryImpl<?> associationQuery : associationQueries) {
        associationQuery.addCriterionsToQuery(versionsReader);
    }
    Query query = buildQuery();
    // add named parameter (used for ValidityAuditStrategy and association queries)
    Collection<String> params = query.getParameterMetadata().getNamedParameterNames();
    if (params.contains(REVISION_PARAMETER)) {
        query.setParameter(REVISION_PARAMETER, revision);
    }
    List queryResult = query.list();
    return applyProjections(queryResult, revision);
}
Also used : Query(org.hibernate.query.Query) AuditEntitiesConfiguration(org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration) AuditCriterion(org.hibernate.envers.query.criteria.AuditCriterion) List(java.util.List)

Aggregations

Query (org.hibernate.query.Query)157 Test (org.junit.Test)44 Session (org.hibernate.Session)39 List (java.util.List)24 ArrayList (java.util.ArrayList)19 TestForIssue (org.hibernate.testing.TestForIssue)19 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)17 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)17 HashMap (java.util.HashMap)15 Map (java.util.Map)14 NativeQuery (org.hibernate.query.NativeQuery)14 AbstractJPATest (org.hibernate.test.jpa.AbstractJPATest)11 SessionFactory (org.hibernate.SessionFactory)9 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)7 InvocationHandler (java.lang.reflect.InvocationHandler)7 Method (java.lang.reflect.Method)7 SQLException (java.sql.SQLException)7 Collectors (java.util.stream.Collectors)7 AbstractQueryFacade (org.jboss.tools.hibernate.runtime.common.AbstractQueryFacade)7 RQuery (com.evolveum.midpoint.repo.sql.query.RQuery)6