use of org.hibernate.test.sql.hand.Organization in project hibernate-orm by hibernate.
the class CustomStoredProcTestSupport method testEntityStoredProcedure.
@Test
public void testEntityStoredProcedure() throws HibernateException, SQLException {
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");
s.persist(ifa);
s.persist(jboss);
s.persist(gavin);
s.persist(emp);
Query namedQuery = s.getNamedQuery("selectAllEmployments");
List list = namedQuery.list();
assertTrue(list.get(0) instanceof Employment);
s.delete(emp);
s.delete(ifa);
s.delete(jboss);
s.delete(gavin);
t.commit();
s.close();
}
use of org.hibernate.test.sql.hand.Organization 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.test.sql.hand.Organization in project hibernate-orm by hibernate.
the class NativeSQLQueriesTest method testScalarValues.
@Test
public void testScalarValues() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
Organization ifa = new Organization("IFA");
Organization jboss = new Organization("JBoss");
Serializable idIfa = s.save(ifa);
Serializable idJBoss = s.save(jboss);
s.flush();
List result = s.getNamedQuery("orgNamesOnly").list();
assertTrue(result.contains("IFA"));
assertTrue(result.contains("JBoss"));
result = s.getNamedQuery("orgNamesOnly").setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
Map m = (Map) result.get(0);
assertEquals(2, result.size());
assertEquals(1, m.size());
assertTrue(m.containsKey("NAME"));
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Iterator iter = s.getNamedQuery("orgNamesAndOrgs").list().iterator();
Object[] o = (Object[]) iter.next();
assertEquals("expecting 2 values", 2, o.length);
assertEquals(o[0], "IFA");
assertEquals(((Organization) o[1]).getName(), "IFA");
o = (Object[]) iter.next();
assertEquals(o[0], "JBoss");
assertEquals(((Organization) o[1]).getName(), "JBoss");
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
// test that the ordering of the results is truly based on the order in which they were defined
iter = s.getNamedQuery("orgsAndOrgNames").list().iterator();
Object[] row = (Object[]) iter.next();
assertEquals("expecting 2 values", 2, row.length);
assertEquals("expecting non-scalar result first", Organization.class, row[0].getClass());
assertEquals("expecting scalar result second", String.class, row[1].getClass());
assertEquals(((Organization) row[0]).getName(), "IFA");
assertEquals(row[1], "IFA");
row = (Object[]) iter.next();
assertEquals("expecting non-scalar result first", Organization.class, row[0].getClass());
assertEquals("expecting scalar result second", String.class, row[1].getClass());
assertEquals(((Organization) row[0]).getName(), "JBoss");
assertEquals(row[1], "JBoss");
assertFalse(iter.hasNext());
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
iter = s.getNamedQuery("orgIdsAndOrgNames").list().iterator();
o = (Object[]) iter.next();
assertEquals(o[1], "IFA");
assertEquals(o[0], idIfa);
o = (Object[]) iter.next();
assertEquals(o[1], "JBoss");
assertEquals(o[0], idJBoss);
s.delete(ifa);
s.delete(jboss);
t.commit();
s.close();
}
use of org.hibernate.test.sql.hand.Organization in project hibernate-orm by hibernate.
the class CustomInsertSQLWithIdentityColumnTest method testBadInsertionFails.
@Test
public void testBadInsertionFails() {
Session session = openSession();
session.beginTransaction();
Organization org = new Organization("hola!");
try {
session.save(org);
session.delete(org);
fail("expecting bad custom insert statement to fail");
} catch (JDBCException e) {
// expected failure
}
session.getTransaction().rollback();
session.close();
}
use of org.hibernate.test.sql.hand.Organization in project hibernate-orm by hibernate.
the class NativeSQLQueriesTest method testExplicitReturnAPI.
@Test
@SuppressWarnings({ "UnusedDeclaration" })
public void testExplicitReturnAPI() {
Session s = openSession();
s.beginTransaction();
Organization jboss = new Organization("JBoss");
Person me = new Person("Steve");
Employment emp = new Employment(me, jboss, "US");
Serializable jbossId = s.save(jboss);
s.save(me);
s.save(emp);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
String sql = "SELECT org.ORGID as orgid," + " org.NAME as name," + " emp.EMPLOYER as employer," + " emp.EMPID as empid," + " emp.EMPLOYEE as employee," + " emp.EMPLOYER as employer," + " emp.STARTDATE as startDate," + " emp.ENDDATE as endDate," + " emp.REGIONCODE as regionCode," + " emp.AMOUNT as AMOUNT," + " emp.CURRENCY as CURRENCY" + " FROM ORGANIZATION org" + " LEFT OUTER JOIN EMPLOYMENT emp ON org.ORGID = emp.EMPLOYER";
// as a control, lets apply an existing rs mapping
SQLQuery sqlQuery = s.createSQLQuery(sql);
sqlQuery.setResultSetMapping("org-description");
sqlQuery.list();
// next try a partial mapping def
sqlQuery.addRoot("org", Organization.class);
sqlQuery.addFetch("emp", "org", "employments");
sqlQuery.list();
// now try full explicit mappings
sqlQuery.addRoot("org", Organization.class).addProperty("id", "orgid").addProperty("name").addColumnAlias("name");
sqlQuery.addFetch("emp", "org", "employments").addProperty("key", "employer").addProperty("element", "empid").addProperty("element.employee", "employee").addProperty("element.employer", "employer").addProperty("element.startDate", "startDate").addProperty("element.endDate", "endDate").addProperty("element.regionCode", "regionCode").addProperty("element.employmentId", "empId").addProperty("element.salary").addColumnAlias("AMOUNT").addColumnAlias("CURRENCY");
sqlQuery.list();
// lets try a totally different approach now and pull back scalars, first with explicit types
sqlQuery.addScalar("orgid", LongType.INSTANCE).addScalar("name", StringType.INSTANCE).addScalar("empid", LongType.INSTANCE).addScalar("employee", LongType.INSTANCE).addScalar("startDate", TimestampType.INSTANCE).addScalar("endDate", TimestampType.INSTANCE).addScalar("regionCode", StringType.INSTANCE).addScalar("empId", LongType.INSTANCE).addScalar("AMOUNT", FloatType.INSTANCE).addScalar("CURRENCY", StringType.INSTANCE);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.delete(emp);
s.delete(jboss);
s.delete(me);
s.getTransaction().commit();
s.close();
}
Aggregations