Search in sources :

Example 1 with SQLQuery

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();
}
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 2 with SQLQuery

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();
}
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 3 with SQLQuery

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();
}
Also used : SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with SQLQuery

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();
}
Also used : SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with SQLQuery

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();
}
Also used : SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

SQLQuery (org.hibernate.SQLQuery)58 Session (org.hibernate.Session)26 Test (org.junit.Test)16 List (java.util.List)13 Map (java.util.Map)9 ArrayList (java.util.ArrayList)7 Collection (java.util.Collection)5 Paint (javafx.scene.paint.Paint)5 Cohort (org.openmrs.Cohort)5 EvaluatedCohort (org.openmrs.module.reporting.cohort.EvaluatedCohort)5 HibernateException (org.hibernate.HibernateException)4 JFXButton (com.jfoenix.controls.JFXButton)3 MaterialDesignIconView (de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView)3 MaterialIconView (de.jensd.fx.glyphs.materialicons.MaterialIconView)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 Insets (javafx.geometry.Insets)3 ScrollPane (javafx.scene.control.ScrollPane)3 Tooltip (javafx.scene.control.Tooltip)3 GridPane (javafx.scene.layout.GridPane)3