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();
}
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();
}
}
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;
}
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;
}
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;
}
Aggregations