use of org.hibernate.SQLQuery in project hibernate-orm by hibernate.
the class NativeSQLQueriesTest method testEscapeColonInSQL.
@Test
@RequiresDialect(MySQL5Dialect.class)
public void testEscapeColonInSQL() throws QueryException {
Session s = openSession();
Transaction t = s.beginTransaction();
SQLQuery query = s.createSQLQuery("SELECT @row \\:= 1");
List list = query.list();
assertTrue(list.get(0).toString().equals("1"));
t.commit();
s.close();
}
use of org.hibernate.SQLQuery in project hibernate-orm by hibernate.
the class NativeSQLQueriesTest method testMappedAliasStrategy.
@Test
@SuppressWarnings({ "deprecation", "UnusedDeclaration" })
public void testMappedAliasStrategy() {
Session s = openSession();
Transaction t = s.beginTransaction();
Organization ifa = new Organization("IFA");
Organization jboss = new Organization("JBoss");
Person gavin = new Person("Gavin");
Employment emp = new Employment(gavin, jboss, "AU");
Serializable orgId = s.save(jboss);
Serializable orgId2 = s.save(ifa);
s.save(gavin);
s.save(emp);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Query namedQuery = s.getNamedQuery("AllEmploymentAsMapped");
List list = namedQuery.list();
assertEquals(1, list.size());
Employment emp2 = (Employment) list.get(0);
assertEquals(emp2.getEmploymentId(), emp.getEmploymentId());
assertEquals(emp2.getStartDate().getDate(), emp.getStartDate().getDate());
assertEquals(emp2.getEndDate(), emp.getEndDate());
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Query sqlQuery = s.getNamedQuery("EmploymentAndPerson");
sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
list = sqlQuery.list();
assertEquals(1, list.size());
Object res = list.get(0);
assertClassAssignability(Map.class, res.getClass());
Map m = (Map) res;
assertEquals(2, m.size());
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
sqlQuery = s.getNamedQuery("organizationreturnproperty");
sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
list = sqlQuery.list();
assertEquals(2, list.size());
m = (Map) list.get(0);
assertEquals(2, m.size());
assertTrue(m.containsKey("org"));
assertTrue(m.containsKey("emp"));
assertClassAssignability(m.get("org").getClass(), Organization.class);
if (jboss.getId() == ((Organization) m.get("org")).getId()) {
assertClassAssignability(m.get("emp").getClass(), Employment.class);
}
Map m2 = (Map) list.get(1);
assertEquals(2, m.size());
assertTrue(m2.containsKey("org"));
assertTrue(m2.containsKey("emp"));
assertClassAssignability(m2.get("org").getClass(), Organization.class);
if (jboss.getId() == ((Organization) m2.get("org")).getId()) {
assertClassAssignability(m2.get("emp").getClass(), Employment.class);
}
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
namedQuery = s.getNamedQuery("EmploymentAndPerson");
list = namedQuery.list();
assertEquals(1, list.size());
Object[] objs = (Object[]) list.get(0);
assertEquals(2, objs.length);
emp2 = (Employment) objs[0];
gavin = (Person) objs[1];
s.delete(emp2);
s.delete(jboss);
s.delete(gavin);
s.delete(ifa);
t.commit();
s.close();
}
use of org.hibernate.SQLQuery in project hibernate-orm by hibernate.
the class NativeSqlAndQuotedIdentifiersTest method testExpandedEntityMapping.
@Test
public void testExpandedEntityMapping() {
Session session = openSession();
session.beginTransaction();
SQLQuery query = (SQLQuery) session.getNamedQuery("query-person");
query.setResultSetMapping("person-entity-expanded");
query.list();
session.getTransaction().commit();
session.close();
}
use of org.hibernate.SQLQuery in project hibernate-orm by hibernate.
the class NativeSqlAndQuotedIdentifiersTest method testBasicEntityMapping.
@Test
public void testBasicEntityMapping() {
Session session = openSession();
session.beginTransaction();
SQLQuery query = (SQLQuery) session.getNamedQuery("query-person");
query.setResultSetMapping("person-entity-basic");
query.list();
session.getTransaction().commit();
session.close();
}
use of org.hibernate.SQLQuery in project midpoint by Evolveum.
the class ObjectUpdater method nonOverwriteAddObjectAttempt.
private <T extends ObjectType> String nonOverwriteAddObjectAttempt(PrismObject<T> object, RObject rObject, String originalOid, Session session, OrgClosureManager.Context closureContext) throws ObjectAlreadyExistsException, SchemaException, DtoTranslationException {
// check name uniqueness (by type)
if (StringUtils.isNotEmpty(originalOid)) {
LOGGER.trace("Checking oid uniqueness.");
//todo improve this table name bullshit
Class hqlType = ClassMapper.getHQLTypeClass(object.getCompileTimeClass());
SQLQuery query = session.createSQLQuery("select count(*) from " + RUtil.getTableName(hqlType) + " where oid=:oid");
query.setString("oid", object.getOid());
Number count = (Number) query.uniqueResult();
if (count != null && count.longValue() > 0) {
throw new ObjectAlreadyExistsException("Object '" + object.getCompileTimeClass().getSimpleName() + "' with oid '" + object.getOid() + "' already exists.");
}
}
updateFullObject(rObject, object);
LOGGER.trace("Saving object (non overwrite).");
String oid = (String) session.save(rObject);
lookupTableHelper.addLookupTableRows(session, rObject, false);
caseHelper.addCertificationCampaignCases(session, rObject, false);
if (closureManager.isEnabled()) {
Collection<ReferenceDelta> modifications = createAddParentRefDelta(object);
closureManager.updateOrgClosure(null, modifications, session, oid, object.getCompileTimeClass(), OrgClosureManager.Operation.ADD, closureContext);
}
return oid;
}
Aggregations