下面列出了怎么用org.hibernate.query.internal.AbstractProducedQuery的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Object load(Serializable id, Object optionalObject, SharedSessionContractImplementor 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 AbstractProducedQuery query = (AbstractProducedQuery) session.getNamedQuery( queryName );
if ( query.getParameterMetadata().hasNamedParameters() ) {
query.setParameter( query.getNamedParameters()[0], id, persister.getIdentifierType() );
}
else {
query.setParameter( position, 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 ) );
}
static <T> T convertQueryException(T result, Throwable e,
AbstractProducedQuery<?> query) {
if ( e instanceof QueryExecutionRequestException) {
throw new IllegalStateException( e );
}
if ( e instanceof TypeMismatchException) {
throw new IllegalStateException( e );
}
if ( e instanceof HibernateException) {
throw query.getProducer().getExceptionConverter()
.convert( (HibernateException) e, query.getLockOptions() );
}
return CompletionStages.returnOrRethrow( e, result );
}
static <R> R extractUniqueResult(List<R> list, AbstractProducedQuery<R> query) {
try {
if ( list.size() == 0 ) {
throw new NoResultException( "No entity found for query" );
}
return AbstractProducedQuery.uniqueElement( list );
}
catch (HibernateException e) {
throw query.getProducer().getExceptionConverter()
.convert( e, query.getLockOptions() );
}
}
/**
* Get the underlying SQL generated by the provided JPA query.
*
* @param query JPA query
* @return the underlying SQL generated by the provided JPA query
*/
public static String from(Query query) {
AbstractProducedQuery abstractProducedQuery = query.unwrap(AbstractProducedQuery.class);
String[] sqls = abstractProducedQuery
.getProducer()
.getFactory()
.getQueryPlanCache()
.getHQLQueryPlan(abstractProducedQuery.getQueryString(), false, Collections.emptyMap())
.getSqlStrings();
return sqls.length > 0 ? sqls[0] : null;
}
@Override
public Object uniqueResult() throws HibernateException {
return AbstractProducedQuery.uniqueElement( list() );
}