List of usage examples for org.hibernate SQLQuery addEntity
SQLQuery<T> addEntity(Class entityType);
From source file:org.snaker.engine.access.hibernate3.HibernateAccess.java
License:Apache License
@SuppressWarnings("unchecked") @Override/* www . j av a2 s .c o m*/ public <T> List<T> queryList(Page<T> page, Class<T> T, String sql, Object... args) { try { String countSQL = "select count(1) from (" + sql + ") c "; String querySQL = sql; if (page.isOrderBySetted()) { querySQL = querySQL + StringHelper.buildPageOrder(page.getOrder(), page.getOrderBy()); } SQLQuery countQuery = getSession().createSQLQuery(countSQL); SQLQuery pageQuery = getSession().createSQLQuery(querySQL); pageQuery.addEntity(T); if (args.length > 0) { for (int i = 0; i < args.length; i++) { pageQuery.setParameter(i, args[i]); countQuery.setParameter(i, args[i]); } } //???pageSize if (page.getPageSize() != Page.NON_PAGE) { pageQuery.setFirstResult((page.getPageNo() - 1) * page.getPageSize()); pageQuery.setMaxResults(page.getPageSize()); } List<T> list = pageQuery.list(); Object total = countQuery.uniqueResult(); page.setResult(list); page.setTotalCount(ClassHelper.castLong(total)); return list; } catch (RuntimeException e) { log.error(e.getMessage(), e); return Collections.emptyList(); } }
From source file:org.springframework.batch.item.database.support.HibernateNativeQueryProviderTests.java
License:Apache License
@Test public void testCreateQueryWithStatelessSession() { String sqlQuery = "select * from T_FOOS"; hibernateQueryProvider.setSqlQuery(sqlQuery); StatelessSession session = mock(StatelessSession.class); SQLQuery query = mock(SQLQuery.class); when(session.createSQLQuery(sqlQuery)).thenReturn(query); when(query.addEntity(Foo.class)).thenReturn(query); hibernateQueryProvider.setStatelessSession(session); Assert.notNull(hibernateQueryProvider.createQuery()); }
From source file:org.springframework.batch.item.database.support.HibernateNativeQueryProviderTests.java
License:Apache License
@Test public void shouldCreateQueryWithStatefulSession() { String sqlQuery = "select * from T_FOOS"; hibernateQueryProvider.setSqlQuery(sqlQuery); Session session = mock(Session.class); SQLQuery query = mock(SQLQuery.class); when(session.createSQLQuery(sqlQuery)).thenReturn(query); when(query.addEntity(Foo.class)).thenReturn(query); hibernateQueryProvider.setSession(session); Assert.notNull(hibernateQueryProvider.createQuery()); }
From source file:org.trpr.dataaccess.orm.handler.HibernateHandler.java
License:Apache License
/** * Helper method to execute a native SQL query and return the results as a List of PersistentEntity instances. * Note that this implementation doesnot support scalar values to be returned and instead expects all resultant data to be * mapped to a PersistentEntity defined for the purpose. This approach helps to maintain consistency in the {@link #findEntities(Criteria)} * interface implementation and also provide for standard Hibernate-to-Java entity type mapping semantics. *///from ww w .ja v a 2 s .co m @SuppressWarnings("unchecked") private List<PersistentEntity> findObjectBySQLQuery(final Criteria criteria) { return (List<PersistentEntity>) this.getTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { SQLQuery sqlQueryObject = session.createSQLQuery(criteria.getQuery()); sqlQueryObject.addEntity(criteria.getManagedClass()); sqlQueryObject.setFirstResult(criteria.getFirstResult()); if (criteria.getMaxResults() > 0) { sqlQueryObject.setMaxResults(criteria.getMaxResults()); } return sqlQueryObject.list(); } }); }
From source file:org.tsm.concharto.service.EventSearchServiceHib.java
License:Apache License
@SuppressWarnings("unchecked") private List<Event> searchInternal(int maxResults, int firstResult, Geometry boundingBox, SearchParams params) { LapTimer timer = new LapTimer(this.log); SQLQuery sqlQuery = createQuery(SQL_PREFIX_SEARCH, boundingBox, params); List<Event> events = sqlQuery.addEntity(Event.class).setMaxResults(maxResults).setFirstResult(firstResult) .list();//from w ww . j a v a 2s .c o m sortResults(events); timer.timeIt("search").logInfoTime(); return events; }
From source file:org.workin.persistence.hibernate.v3.dao.Hibernate3PersistenceDaoImpl.java
License:Apache License
@SuppressWarnings("unchecked") @Override// w w w. ja v a 2 s.co m public <R> R findUniqueByNativeQuery(final Class<R> resultClass, final String sql, final Object[] values) { return getHibernateTemplate().execute(new HibernateCallback<R>() { @Override public R doInHibernate(Session session) throws HibernateException, SQLException { SQLQuery query = session.createSQLQuery(sql); query.addEntity(resultClass); setQueryParameters(query, values); return (R) query.uniqueResult(); } }); }
From source file:org.workin.persistence.hibernate.v3.dao.Hibernate3PersistenceDaoImpl.java
License:Apache License
@SuppressWarnings("unchecked") @Override//from w ww . j a va2 s .co m public <R> R findUniqueByNativeQuery(final Class<R> resultClass, final String sql, final Map<String, ?> paramMap) { return getHibernateTemplate().execute(new HibernateCallback<R>() { @Override public R doInHibernate(Session session) throws HibernateException, SQLException { SQLQuery query = session.createSQLQuery(sql); query.addEntity(resultClass); setQueryNamedParameters(query, paramMap); return (R) query.uniqueResult(); } }); }
From source file:org.workin.persistence.hibernate.v3.dao.Hibernate3PersistenceDaoImpl.java
License:Apache License
@SuppressWarnings("unchecked") @Override//from w w w.j a v a 2 s .c o m public <R> List<R> findByNativeQuery(final Class<R> resultClass, final String sql, final Object[] values, final int start, final int maxRows) { return getHibernateTemplate().executeFind(new HibernateCallback<List<R>>() { @Override public List<R> doInHibernate(Session session) throws HibernateException, SQLException { SQLQuery query = session.createSQLQuery(sql); query.addEntity(resultClass); setQueryParameters(query, values, start, maxRows); return query.list(); } }); }
From source file:org.workin.persistence.hibernate.v4.dao.Hibernate4PersistenceDaoImpl.java
License:Apache License
@SuppressWarnings("unchecked") @Override/* w w w .j a va 2 s. com*/ public <R> R findUniqueByNativeQuery(final Class<R> resultClass, final String sql, final Object[] values) { SQLQuery query = getCurrentSession().createSQLQuery(sql); query.addEntity(resultClass); setQueryParameters(query, values); return (R) query.uniqueResult(); }
From source file:org.workin.persistence.hibernate.v4.dao.Hibernate4PersistenceDaoImpl.java
License:Apache License
@SuppressWarnings("unchecked") @Override//from ww w .j av a 2s . com public <R> R findUniqueByNativeQuery(final Class<R> resultClass, final String sql, final Map<String, ?> paramMap) { SQLQuery query = getCurrentSession().createSQLQuery(sql); query.addEntity(resultClass); setQueryNamedParameters(query, paramMap); return (R) query.uniqueResult(); }