Search in sources :

Example 26 with SQLQuery

use of org.hibernate.SQLQuery in project JessMA by ldcsaa.

the class HibernateFacade method sqlQuery4.

/**
	 * 
	 * 根据起始记录和最大记录数执行SQL查询,并根据Entities和Joins返回查询结果
	 * 
	 * @param firstResult	: 起始记录索引
	 * @param maxResults	: 最大记录数
	 * @param sql			: SQL 查询语句
	 * @param entities		: 要绑定的查询实体集合
	 * @param joins			: 要绑定的连接实体集合
	 * @param params		: 查询参数
	 * @return				: 查询结果
	 * 
	 */
protected <T> List<T> sqlQuery4(int firstResult, int maxResults, String sql, KV<String, Object>[] entities, KV<String, String>[] joins, Object... params) {
    SQLQuery sqlQuery = getSession().createSQLQuery(sql);
    for (int i = 0; i < params.length; i++) sqlQuery.setParameter(i, params[i]);
    if (entities != null) {
        for (int i = 0; i < entities.length; i++) {
            KV<String, Object> entity = entities[i];
            String key = entity.getKey();
            Object value = entity.getValue();
            Class<?> v1 = (value instanceof Class) ? (Class<?>) value : null;
            String v2 = v1 == null ? (String) value : null;
            if (key == null || key.length() == 0) {
                if (v1 != null)
                    sqlQuery.addEntity(v1);
                else
                    sqlQuery.addEntity(v2);
            } else {
                if (v1 != null)
                    sqlQuery.addEntity(key, v1);
                else
                    sqlQuery.addEntity(key, v2);
            }
        }
    }
    if (joins != null) {
        for (KV<String, String> join : joins) sqlQuery.addJoin(join.getKey(), (String) join.getValue());
    }
    if (firstResult > 0)
        sqlQuery.setFirstResult(firstResult);
    if (maxResults > 0)
        sqlQuery.setMaxResults(maxResults);
    return sqlQuery.list();
}
Also used : SQLQuery(org.hibernate.SQLQuery)

Example 27 with SQLQuery

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

the class DeleteTest method delete0003.

@Test
public void delete0003() throws Exception {
    PrismObject<ShadowType> shadow = prismContext.parseObject(new File(FOLDER_BASE, "delete/shadow.xml"));
    OperationResult result = new OperationResult("add shadow");
    final String oid = repositoryService.addObject(shadow, null, result);
    PrismObject<ShadowType> repoShadow = repositoryService.getObject(ShadowType.class, oid, null, result);
    shadow = prismContext.parseObject(new File(FOLDER_BASE, "delete/shadow.xml"));
    AssertJUnit.assertEquals(shadow, repoShadow);
    repositoryService.deleteObject(ShadowType.class, oid, result);
    result.recomputeStatus();
    AssertJUnit.assertTrue(result.isSuccess());
    Session session = getFactory().openSession();
    try {
        SQLQuery query = session.createSQLQuery("select count(*) from m_trigger where owner_oid = ?");
        query.setString(0, oid);
        Number count = (Number) query.uniqueResult();
        AssertJUnit.assertEquals(count.longValue(), 0L);
    } finally {
        session.close();
    }
}
Also used : ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) SQLQuery(org.hibernate.SQLQuery) File(java.io.File) Session(org.hibernate.Session) Test(org.testng.annotations.Test)

Example 28 with SQLQuery

use of org.hibernate.SQLQuery in project java-design-patterns by iluwatar.

the class QueryServiceImpl method getAuthorBooks.

@Override
public List<Book> getAuthorBooks(String username) {
    List<Book> bookDTos = null;
    try (Session session = sessionFactory.openSession()) {
        SQLQuery sqlQuery = session.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\"" + " FROM Author a , Book b where b.author_id = a.id and a.username=:username");
        sqlQuery.setParameter("username", username);
        bookDTos = sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).list();
    }
    return bookDTos;
}
Also used : Book(com.iluwatar.cqrs.dto.Book) SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session)

Example 29 with SQLQuery

use of org.hibernate.SQLQuery in project java-design-patterns by iluwatar.

the class QueryServiceImpl method getAuthorByUsername.

@Override
public Author getAuthorByUsername(String username) {
    Author authorDTo = null;
    try (Session session = sessionFactory.openSession()) {
        SQLQuery sqlQuery = session.createSQLQuery("SELECT a.username as \"username\", a.name as \"name\", a.email as \"email\"" + "FROM Author a where a.username=:username");
        sqlQuery.setParameter("username", username);
        authorDTo = (Author) sqlQuery.setResultTransformer(Transformers.aliasToBean(Author.class)).uniqueResult();
    }
    return authorDTo;
}
Also used : Author(com.iluwatar.cqrs.dto.Author) SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session)

Example 30 with SQLQuery

use of org.hibernate.SQLQuery in project java-design-patterns by iluwatar.

the class QueryServiceImpl method getBook.

@Override
public Book getBook(String title) {
    Book bookDTo = null;
    try (Session session = sessionFactory.openSession()) {
        SQLQuery sqlQuery = session.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\"" + " FROM Book b where b.title=:title");
        sqlQuery.setParameter("title", title);
        bookDTo = (Book) sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).uniqueResult();
    }
    return bookDTo;
}
Also used : Book(com.iluwatar.cqrs.dto.Book) SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session)

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