@Override public Object load(Serializable id, Object optionalObject, SessionImplementor session) { LOG.debugf( "Loading entity: %s using named query: %s", persister.getEntityName(), queryName ); // IMPL NOTE: essentially we perform the named query (which loads the entity into the PC), and then // do an internal lookup of the entity from the PC. final AbstractQueryImpl query = (AbstractQueryImpl) session.getNamedQuery( queryName ); if ( query.hasNamedParameters() ) { query.setParameter( query.getNamedParameters()[0], id, persister.getIdentifierType() ); } else { query.setParameter( 0, id, persister.getIdentifierType() ); } query.setOptionalId( id ); query.setOptionalEntityName( persister.getEntityName() ); query.setOptionalObject( optionalObject ); query.setFlushMode( FlushMode.MANUAL ); query.list(); // now look up the object we are really interested in! // (this lets us correctly handle proxies and multi-row or multi-column queries) return session.getPersistenceContext().getEntity( session.generateEntityKey( id, persister ) ); }
public void initialize(Serializable key, SessionImplementor session) throws HibernateException { LOG.debugf("Initializing collection: %s using named query: %s", persister.getRole(), queryName); //TODO: is there a more elegant way than downcasting? AbstractQueryImpl query = (AbstractQueryImpl) session.getNamedSQLQuery(queryName); if ( query.getNamedParameters().length>0 ) { query.setParameter( query.getNamedParameters()[0], key, persister.getKeyType() ); } else { query.setParameter( 0, key, persister.getKeyType() ); } query.setCollectionKey( key ) .setFlushMode( FlushMode.MANUAL ) .list(); }
/** * 设置参数值到query的hql中 * * @param query Hibernate Query * @param values 参数值可变数组 */ protected void setQueryValues(Query query ,Object... values) { if (ArrayUtils.isEmpty(values)) { return ; } AbstractQueryImpl impl = (AbstractQueryImpl) query; String[] params = impl.getNamedParameters(); int methodParameterPosition = params.length - 1; if (impl.hasNamedParameters()) { for (String p : params) { Object o = values[methodParameterPosition--]; query.setParameter(p, o); } } else { for (Integer i = 0; i < values.length; i++) { query.setParameter(i, values[i]); } } }
private Map<String, TypedValue> getNamedParameterValues(final QueryImpl query) { try { final Method accessor = AbstractQueryImpl.class.getDeclaredMethod("getNamedParams"); accessor.setAccessible(true); return (Map<String, TypedValue>) accessor.invoke(query); } catch (ReflectiveOperationException e) { throw new IllegalArgumentException("Could not get named parameter values from query", e); } }
private List<Object> getPositionalParameterValues(final QueryImpl query) { try { final Method accessor = AbstractQueryImpl.class.getDeclaredMethod("getValues"); accessor.setAccessible(true); return (List<Object>) accessor.invoke(query); } catch (ReflectiveOperationException e) { throw new IllegalArgumentException("Could not get positional parameter values from query", e); } }
@Override protected TypedQuery<Long> getCountQuery(final Specification<T> spec) { final CriteriaBuilder builder = em.getCriteriaBuilder(); final CriteriaQuery<T> criteria = builder.createQuery(getDomainClass()); final Root<T> root = applySpecificationToQueryCriteria(spec, criteria); criteria.select(root); final TypedQuery<T> query = em.createQuery(criteria); final AbstractQueryImpl hibernateQuery = query.unwrap(AbstractQueryImpl.class); @SuppressWarnings("unchecked") final Map<String, TypedValue> pNamedParameters = (Map<String, TypedValue>) getField(hibernateQuery, AbstractQueryImpl.class, "namedParameters"); final String hql = hibernateQuery.getQueryString(); final ASTQueryTranslatorFactory queryTranslatorFactory = new ASTQueryTranslatorFactory(); final SessionImplementor hibernateSession = em.unwrap(SessionImplementor.class); final QueryTranslator queryTranslator = queryTranslatorFactory.createQueryTranslator("", hql, Collections.emptyMap(), hibernateSession.getFactory(), null); queryTranslator.compile(Collections.emptyMap(), false); final String sql = queryTranslator.getSQLString(); final ParameterTranslations paramTranslations = queryTranslator.getParameterTranslations(); final String countSql = String.format("select count(*) from (%s limit %d) as sqry", sql, maximumRecords + 1); final Query nativeQuery = em.createNativeQuery(countSql); final AbstractQueryImpl resultQuery = nativeQuery.unwrap(AbstractQueryImpl.class); if (pNamedParameters != null) { for (final Entry<String, TypedValue> entry : pNamedParameters.entrySet()) { for (final int index : paramTranslations.getNamedParameterSqlLocations(entry.getKey())) { resultQuery.setParameter(index, entry.getValue().getValue(), entry.getValue().getType()); } } } return new CustomCountQueryWrapper(nativeQuery); }
@Override public Object uniqueResult() throws HibernateException { return AbstractQueryImpl.uniqueElement(list()); }