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 hibernate-orm by hibernate.
the class SQLLoaderTest method testCompositeIdId.
@Test
@TestForIssue(jiraKey = "HHH-21")
// because the XML mapping defines the loader for CompositeIdId using a column name that needs to be quoted
@RequiresDialectFeature(DoubleQuoteDialect.class)
public void testCompositeIdId() throws HibernateException, SQLException {
Session s = openSession();
s.beginTransaction();
CompositeIdId id = new CompositeIdId();
id.setName("Max");
id.setUser("c64");
id.setId("games");
s.save(id);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
// having a composite id with one property named id works since the map used by sqlloader to map names to properties handles it.
// NOTE : SYSTEM is an ANSI SQL defined keyword, so it gets quoted; so it needs to get quoted here too
String sql = String.format("select %1$s as {c.user}, " + " id as {c.id}, name as {c.name}, " + " foo as {c.composite.foo}, " + " bar as {c.composite.bar} " + "from CompositeIdId " + "where %1$s=? and id=?", getDialect().openQuote() + "user" + getDialect().closeQuote());
SQLQuery query = s.createSQLQuery(sql).addEntity("c", CompositeIdId.class);
query.setString(0, "c64");
query.setString(1, "games");
CompositeIdId id2 = (CompositeIdId) query.uniqueResult();
check(id, id2);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
CompositeIdId useForGet = new CompositeIdId();
useForGet.setUser("c64");
useForGet.setId("games");
// this doesn't work since the verification does not take column span into respect!
CompositeIdId getted = (CompositeIdId) s.get(CompositeIdId.class, useForGet);
check(id, getted);
s.getTransaction().commit();
s.close();
}
Aggregations