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());
}
});
}
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"));
}
}
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) {
}
}
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) {
}
}
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);
}
Aggregations