Search in sources :

Example 1 with Employment

use of org.hibernate.test.sql.hand.Employment 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();
}
Also used : Serializable(java.io.Serializable) Organization(org.hibernate.test.sql.hand.Organization) Transaction(org.hibernate.Transaction) SQLQuery(org.hibernate.SQLQuery) Query(org.hibernate.Query) Employment(org.hibernate.test.sql.hand.Employment) List(java.util.List) Person(org.hibernate.test.sql.hand.Person) Map(java.util.Map) HashMap(java.util.HashMap) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with Employment

use of org.hibernate.test.sql.hand.Employment 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();
}
Also used : Organization(org.hibernate.test.sql.hand.Organization) Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) Employment(org.hibernate.test.sql.hand.Employment) List(java.util.List) Person(org.hibernate.test.sql.hand.Person) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with Employment

use of org.hibernate.test.sql.hand.Employment 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();
}
Also used : Serializable(java.io.Serializable) Organization(org.hibernate.test.sql.hand.Organization) Employment(org.hibernate.test.sql.hand.Employment) SQLQuery(org.hibernate.SQLQuery) Person(org.hibernate.test.sql.hand.Person) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with Employment

use of org.hibernate.test.sql.hand.Employment in project hibernate-orm by hibernate.

the class NativeSQLQueriesTest method testSQLQueryInterface.

@Test
public void testSQLQueryInterface() {
    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);
    List l = s.createSQLQuery(getOrgEmpRegionSQL()).addEntity("org", Organization.class).addJoin("emp", "org.employments").addScalar("regionCode", StringType.INSTANCE).list();
    assertEquals(2, l.size());
    l = s.createSQLQuery(getOrgEmpPersonSQL()).addEntity("org", Organization.class).addJoin("emp", "org.employments").addJoin("pers", "emp.employee").list();
    assertEquals(l.size(), 1);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    l = s.createSQLQuery("select {org.*}, {emp.*} " + "from ORGANIZATION org " + "     left outer join EMPLOYMENT emp on org.ORGID = emp.EMPLOYER, ORGANIZATION org2").addEntity("org", Organization.class).addJoin("emp", "org.employments").setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE).list();
    assertEquals(l.size(), 2);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    s.delete(emp);
    s.delete(gavin);
    s.delete(ifa);
    s.delete(jboss);
    t.commit();
    s.close();
}
Also used : Organization(org.hibernate.test.sql.hand.Organization) Transaction(org.hibernate.Transaction) Employment(org.hibernate.test.sql.hand.Employment) List(java.util.List) Person(org.hibernate.test.sql.hand.Person) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with Employment

use of org.hibernate.test.sql.hand.Employment in project hibernate-orm by hibernate.

the class NativeSQLQueriesTest method testResultSetMappingDefinition.

@Test
public void testResultSetMappingDefinition() {
    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);
    List l = s.createSQLQuery(getOrgEmpRegionSQL()).setResultSetMapping("org-emp-regionCode").list();
    assertEquals(l.size(), 2);
    l = s.createSQLQuery(getOrgEmpPersonSQL()).setResultSetMapping("org-emp-person").list();
    assertEquals(l.size(), 1);
    s.delete(emp);
    s.delete(gavin);
    s.delete(ifa);
    s.delete(jboss);
    t.commit();
    s.close();
}
Also used : Organization(org.hibernate.test.sql.hand.Organization) Transaction(org.hibernate.Transaction) Employment(org.hibernate.test.sql.hand.Employment) List(java.util.List) Person(org.hibernate.test.sql.hand.Person) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Session (org.hibernate.Session)7 Employment (org.hibernate.test.sql.hand.Employment)7 Organization (org.hibernate.test.sql.hand.Organization)7 Person (org.hibernate.test.sql.hand.Person)7 Test (org.junit.Test)7 Transaction (org.hibernate.Transaction)6 List (java.util.List)5 Serializable (java.io.Serializable)4 Query (org.hibernate.Query)3 SQLQuery (org.hibernate.SQLQuery)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Date (java.util.Date)1 Iterator (java.util.Iterator)1 Dimension (org.hibernate.test.sql.hand.Dimension)1 SpaceShip (org.hibernate.test.sql.hand.SpaceShip)1