Search in sources :

Example 6 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 7 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 8 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 9 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 10 with SQLQuery

use of org.hibernate.SQLQuery in project midpoint by Evolveum.

the class ObjectUpdater method nonOverwriteAddObjectAttempt.

private <T extends ObjectType> String nonOverwriteAddObjectAttempt(PrismObject<T> object, RObject rObject, String originalOid, Session session, OrgClosureManager.Context closureContext) throws ObjectAlreadyExistsException, SchemaException, DtoTranslationException {
    // check name uniqueness (by type)
    if (StringUtils.isNotEmpty(originalOid)) {
        LOGGER.trace("Checking oid uniqueness.");
        //todo improve this table name bullshit
        Class hqlType = ClassMapper.getHQLTypeClass(object.getCompileTimeClass());
        SQLQuery query = session.createSQLQuery("select count(*) from " + RUtil.getTableName(hqlType) + " where oid=:oid");
        query.setString("oid", object.getOid());
        Number count = (Number) query.uniqueResult();
        if (count != null && count.longValue() > 0) {
            throw new ObjectAlreadyExistsException("Object '" + object.getCompileTimeClass().getSimpleName() + "' with oid '" + object.getOid() + "' already exists.");
        }
    }
    updateFullObject(rObject, object);
    LOGGER.trace("Saving object (non overwrite).");
    String oid = (String) session.save(rObject);
    lookupTableHelper.addLookupTableRows(session, rObject, false);
    caseHelper.addCertificationCampaignCases(session, rObject, false);
    if (closureManager.isEnabled()) {
        Collection<ReferenceDelta> modifications = createAddParentRefDelta(object);
        closureManager.updateOrgClosure(null, modifications, session, oid, object.getCompileTimeClass(), OrgClosureManager.Operation.ADD, closureContext);
    }
    return oid;
}
Also used : ReferenceDelta(com.evolveum.midpoint.prism.delta.ReferenceDelta) SQLQuery(org.hibernate.SQLQuery) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)

Aggregations

SQLQuery (org.hibernate.SQLQuery)35 Session (org.hibernate.Session)22 Test (org.junit.Test)13 List (java.util.List)6 Paint (javafx.scene.paint.Paint)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 BigInteger (java.math.BigInteger)3 SimpleDateFormat (java.text.SimpleDateFormat)3 ArrayList (java.util.ArrayList)3 Insets (javafx.geometry.Insets)3 ScrollPane (javafx.scene.control.ScrollPane)3 Tooltip (javafx.scene.control.Tooltip)3 GridPane (javafx.scene.layout.GridPane)3 Book (com.iluwatar.cqrs.dto.Book)2 OctIconView (de.jensd.fx.glyphs.octicons.OctIconView)2 Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2