Search in sources :

Example 6 with Organization

use of org.hibernate.test.sql.hand.Organization 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 7 with Organization

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

the class NativeSQLQueriesTest method testManualSynchronization.

@Test
public void testManualSynchronization() {
    Session s = openSession();
    s.beginTransaction();
    sessionFactory().getStatistics().clear();
    // create an Organization...
    Organization jboss = new Organization("JBoss");
    s.persist(jboss);
    // now query on Employment, this should not cause an auto-flush
    s.createSQLQuery(getEmploymentSQL()).addSynchronizedQuerySpace("ABC").list();
    assertEquals(0, sessionFactory().getStatistics().getEntityInsertCount());
    // now try to query on Employment but this time add Organization as a synchronized query space...
    s.createSQLQuery(getEmploymentSQL()).addSynchronizedEntityClass(Organization.class).list();
    assertEquals(1, sessionFactory().getStatistics().getEntityInsertCount());
    // clean up
    s.delete(jboss);
    s.getTransaction().commit();
    s.close();
}
Also used : Organization(org.hibernate.test.sql.hand.Organization) Session(org.hibernate.Session) Test(org.junit.Test)

Example 8 with Organization

use of org.hibernate.test.sql.hand.Organization 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)

Example 9 with Organization

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

the class NativeSQLQueriesTest method testAutoDetectAliasing.

@Test
@SuppressWarnings({ "UnusedDeclaration", "deprecation", "UnusedAssignment" })
public void testAutoDetectAliasing() {
    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();
    List list = s.createSQLQuery(getEmploymentSQL()).addEntity(Employment.class.getName()).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());
    s.clear();
    list = s.createSQLQuery(getEmploymentSQL()).addEntity(Employment.class.getName()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
    assertEquals(1, list.size());
    Map m = (Map) list.get(0);
    assertTrue(m.containsKey("Employment"));
    assertEquals(1, m.size());
    list = s.createSQLQuery(getEmploymentSQL()).list();
    assertEquals(1, list.size());
    Object[] o = (Object[]) list.get(0);
    assertEquals(8, o.length);
    list = s.createSQLQuery(getEmploymentSQL()).setResultTransformer(new UpperCasedAliasToEntityMapResultTransformer()).list();
    assertEquals(1, list.size());
    m = (Map) list.get(0);
    assertTrue(m.containsKey("EMPID"));
    assertTrue(m.containsKey("AMOUNT"));
    assertTrue(m.containsKey("ENDDATE"));
    assertEquals(8, m.size());
    list = s.createSQLQuery(getEmploymentSQLMixedScalarEntity()).addScalar("employerid").addEntity(Employment.class).list();
    assertEquals(1, list.size());
    o = (Object[]) list.get(0);
    assertEquals(2, o.length);
    assertClassAssignability(Number.class, o[0].getClass());
    assertClassAssignability(Employment.class, o[1].getClass());
    Query queryWithCollection = s.getNamedQuery("organizationEmploymentsExplicitAliases");
    queryWithCollection.setLong("id", jboss.getId());
    list = queryWithCollection.list();
    assertEquals(list.size(), 1);
    s.clear();
    list = s.createSQLQuery(getOrganizationJoinEmploymentSQL()).addEntity("org", Organization.class).addJoin("emp", "org.employments").list();
    assertEquals(2, list.size());
    s.clear();
    list = s.createSQLQuery(getOrganizationFetchJoinEmploymentSQL()).addEntity("org", Organization.class).addJoin("emp", "org.employments").list();
    assertEquals(2, list.size());
    s.clear();
    // TODO : why twice?
    s.getNamedQuery("organizationreturnproperty").list();
    list = s.getNamedQuery("organizationreturnproperty").list();
    assertEquals(2, list.size());
    s.clear();
    list = s.getNamedQuery("organizationautodetect").list();
    assertEquals(2, list.size());
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    s.delete(emp2);
    s.delete(jboss);
    s.delete(gavin);
    s.delete(ifa);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    Dimension dim = new Dimension(3, 30);
    s.save(dim);
    list = s.createSQLQuery("select d_len * d_width as surface, d_len * d_width * 10 as volume from Dimension").list();
    s.delete(dim);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    SpaceShip enterprise = new SpaceShip();
    enterprise.setModel("USS");
    enterprise.setName("Entreprise");
    enterprise.setSpeed(50d);
    Dimension d = new Dimension(45, 10);
    enterprise.setDimensions(d);
    s.save(enterprise);
    Object[] result = (Object[]) s.getNamedQuery("spaceship").uniqueResult();
    assertEquals("expecting 3 result values", 3, result.length);
    enterprise = (SpaceShip) result[0];
    assertTrue(50d == enterprise.getSpeed());
    assertTrue(450d == extractDoubleValue(result[1]));
    assertTrue(4500d == extractDoubleValue(result[2]));
    s.delete(enterprise);
    t.commit();
    s.close();
}
Also used : Serializable(java.io.Serializable) Organization(org.hibernate.test.sql.hand.Organization) SQLQuery(org.hibernate.SQLQuery) Query(org.hibernate.Query) Dimension(org.hibernate.test.sql.hand.Dimension) Transaction(org.hibernate.Transaction) SpaceShip(org.hibernate.test.sql.hand.SpaceShip) 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 10 with Organization

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

the class CustomSQLTestSupport method testHandSQL.

@Test
public void testHandSQL() {
    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);
    s.save(ifa);
    s.save(gavin);
    s.save(emp);
    t.commit();
    t = s.beginTransaction();
    Person christian = new Person("Christian");
    s.save(christian);
    Employment emp2 = new Employment(christian, jboss, "EU");
    s.save(emp2);
    t.commit();
    s.close();
    sessionFactory().getCache().evictEntityRegion(Organization.class);
    sessionFactory().getCache().evictEntityRegion(Person.class);
    sessionFactory().getCache().evictEntityRegion(Employment.class);
    s = openSession();
    t = s.beginTransaction();
    jboss = (Organization) s.get(Organization.class, orgId);
    assertEquals(jboss.getEmployments().size(), 2);
    assertEquals(jboss.getName(), "JBOSS");
    emp = (Employment) jboss.getEmployments().iterator().next();
    gavin = emp.getEmployee();
    assertEquals("GAVIN", gavin.getName());
    assertEquals(LockMode.UPGRADE, s.getCurrentLockMode(gavin));
    emp.setEndDate(new Date());
    Employment emp3 = new Employment(gavin, jboss, "US");
    s.save(emp3);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    Iterator itr = s.getNamedQuery("allOrganizationsWithEmployees").list().iterator();
    assertTrue(itr.hasNext());
    Organization o = (Organization) itr.next();
    assertEquals(o.getEmployments().size(), 3);
    Iterator itr2 = o.getEmployments().iterator();
    while (itr2.hasNext()) {
        Employment e = (Employment) itr2.next();
        s.delete(e);
    }
    itr2 = o.getEmployments().iterator();
    while (itr2.hasNext()) {
        Employment e = (Employment) itr2.next();
        s.delete(e.getEmployee());
    }
    s.delete(o);
    assertFalse(itr.hasNext());
    s.delete(ifa);
    t.commit();
    s.close();
}
Also used : Serializable(java.io.Serializable) Organization(org.hibernate.test.sql.hand.Organization) Transaction(org.hibernate.Transaction) Employment(org.hibernate.test.sql.hand.Employment) Iterator(java.util.Iterator) Person(org.hibernate.test.sql.hand.Person) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

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