Search in sources :

Example 31 with Session

use of org.hibernate.Session 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();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session) Test(org.junit.Test) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 32 with Session

use of org.hibernate.Session 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 33 with Session

use of org.hibernate.Session in project hibernate-orm by hibernate.

the class NativeSQLQueriesTest method testMixAndMatchEntityScalar.

@Test
public void testMixAndMatchEntityScalar() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Speech speech = new Speech();
    speech.setLength(new Double(23d));
    speech.setName("Mine");
    s.persist(speech);
    s.flush();
    s.clear();
    List l = s.createSQLQuery("select name, id, flength, name as scalarName from Speech").setResultSetMapping("speech").list();
    assertEquals(l.size(), 1);
    t.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Speech(org.hibernate.test.sql.hand.Speech) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 34 with Session

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

Example 35 with Session

use of org.hibernate.Session in project hibernate-orm by hibernate.

the class NativeSQLQueriesTest method testImageTypeInSQLQuery.

@SkipForDialect(value = AbstractHANADialect.class, comment = "On HANA, this returns a blob for the image column which doesn't get mapped to a byte[]")
@Test
public void testImageTypeInSQLQuery() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    byte[] photo = buildLongByteArray(15000, true);
    ImageHolder holder = new ImageHolder(photo);
    s.persist(holder);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    byte[] photoRead = (byte[]) s.createSQLQuery(getPhotosSQL()).uniqueResult();
    assertTrue(Arrays.equals(photo, photoRead));
    s.delete(holder);
    t.commit();
    s.close();
}
Also used : ImageHolder(org.hibernate.test.sql.hand.ImageHolder) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Aggregations

Session (org.hibernate.Session)3262 Test (org.junit.Test)2414 Transaction (org.hibernate.Transaction)1255 List (java.util.List)526 ArrayList (java.util.ArrayList)363 TestForIssue (org.hibernate.testing.TestForIssue)360 Query (org.hibernate.Query)224 Test (org.testng.annotations.Test)143 Iterator (java.util.Iterator)127 Date (java.util.Date)112 Map (java.util.Map)104 BigDecimal (java.math.BigDecimal)97 Criteria (org.hibernate.Criteria)92 HashSet (java.util.HashSet)90 HibernateException (org.hibernate.HibernateException)82 HashMap (java.util.HashMap)70 SQLException (java.sql.SQLException)66 SkipForDialect (org.hibernate.testing.SkipForDialect)66 Connection (java.sql.Connection)65 Serializable (java.io.Serializable)62