Java 类org.hibernate.annotations.QueryHints 实例源码

项目:appengine-hibernate    文件:TestResources.java   
@GET
@Path("greetings/latest")
@Produces(MediaType.TEXT_PLAIN)
public Response queryLatestGreetings(@QueryParam("maxResults") int maxResults) {

    EntityManager em = Hibernate.createEntityManager();
    try {
        final List<Tuple> greetings = em
                .createQuery("SELECT g.id, g.date, g.author.name, g.content FROM Greeting g ORDER by g.date DESC", Tuple.class)
                .setHint(QueryHints.READ_ONLY, "true")
                .setMaxResults(maxResults == 0 ? 500 : maxResults)
                .getResultList();
        em.close();


        return RequestStats.wrap(Response.ok(toString(greetings)));

    } catch (QueryTimeoutException e) {
        LOGGER.severe("Caught QueryTimeoutException");

        return Response.status(Response.Status.SERVICE_UNAVAILABLE).build();
    }
}
项目:lams    文件:QueryHintDefinition.java   
public CacheMode getCacheMode(String query) {
    String hitName = QueryHints.CACHE_MODE;
    String value =(String) hintsMap.get( hitName );
    if ( value == null ) {
        return null;
    }
    try {
        return CacheMode.interpretExternalSetting( value );
    }
    catch ( MappingException e ) {
        throw new AnnotationException( "Unknown CacheMode in hint: " + query + ":" + hitName, e );
    }
}
项目:lams    文件:QueryHintDefinition.java   
public FlushMode getFlushMode(String query) {
    String hitName = QueryHints.FLUSH_MODE;
    String value =(String)  hintsMap.get( hitName );
    if ( value == null ) {
        return null;
    }
    try {
        return FlushMode.interpretExternalSetting( value );
    }
    catch ( MappingException e ) {
        throw new AnnotationException( "Unknown FlushMode in hint: " + query + ":" + hitName, e );
    }
}
项目:lams    文件:QueryHintDefinition.java   
public Integer getTimeout(String queryName) {
    Integer timeout = getInteger( queryName, QueryHints.TIMEOUT_JPA );

    if ( timeout != null ) {
        // convert milliseconds to seconds
        timeout = (int) Math.round( timeout.doubleValue() / 1000.0 );
    }
    else {
        // timeout is already in seconds
        timeout = getInteger( queryName, QueryHints.TIMEOUT_HIBERNATE );
    }
    return timeout;
}
项目:lams    文件:QueryBinder.java   
public static void bindQuery(NamedQuery queryAnn, Mappings mappings, boolean isDefault) {
    if ( queryAnn == null ) return;
    if ( BinderHelper.isEmptyAnnotationValue( queryAnn.name() ) ) {
        throw new AnnotationException( "A named query must have a name when used in class or package level" );
    }
    //EJBQL Query
    QueryHintDefinition hints = new QueryHintDefinition( queryAnn.hints() );
    String queryName = queryAnn.query();
    NamedQueryDefinition queryDefinition = new NamedQueryDefinitionBuilder( queryAnn.name() )
            .setLockOptions( hints.determineLockOptions( queryAnn ) )
            .setQuery( queryName )
            .setCacheable( hints.getBoolean( queryName, QueryHints.CACHEABLE ) )
            .setCacheRegion( hints.getString( queryName, QueryHints.CACHE_REGION ) )
            .setTimeout( hints.getTimeout( queryName ) )
            .setFetchSize( hints.getInteger( queryName, QueryHints.FETCH_SIZE ) )
            .setFlushMode( hints.getFlushMode( queryName ) )
            .setCacheMode( hints.getCacheMode( queryName ) )
            .setReadOnly( hints.getBoolean( queryName, QueryHints.READ_ONLY ) )
            .setComment( hints.getString( queryName, QueryHints.COMMENT ) )
            .setParameterTypes( null )
            .createNamedQueryDefinition();

    if ( isDefault ) {
        mappings.addDefaultQuery( queryDefinition.getName(), queryDefinition );
    }
    else {
        mappings.addQuery( queryDefinition.getName(), queryDefinition );
    }
    if ( LOG.isDebugEnabled() ) {
        LOG.debugf( "Binding named query: %s => %s", queryDefinition.getName(), queryDefinition.getQueryString() );
    }
}
项目:lams    文件:QueryBinder.java   
private static Integer getTimeout(AnnotationInstance[] hints, String query) {
    Integer timeout = getInteger( hints, QueryHints.TIMEOUT_JPA, query );
    if ( timeout == null ) {
        return getInteger( hints, QueryHints.TIMEOUT_HIBERNATE, query ); // timeout is already in seconds
    }
    return ( ( timeout + 500 ) / 1000 ); // convert milliseconds to seconds (rounded)
}
项目:mycore    文件:MCRCategLinkServiceImpl.java   
@Override
public Collection<MCRCategLinkReference> getReferences(String type) {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MCRCategLinkReference> query = cb.createQuery(MCRCategLinkReference.class);
    Root<MCRCategoryLinkImpl> li = query.from(LINK_CLASS);
    Path<MCRCategLinkReference> objectReferencePath = li.get(MCRCategoryLinkImpl_.objectReference);
    return em
        .createQuery(
            query.select(objectReferencePath)
                .where(cb.equal(objectReferencePath.get(MCRCategLinkReference_.type), type)))
        .setHint(QueryHints.READ_ONLY, "true")
        .getResultList();
}
项目:mycore    文件:MCRCategLinkServiceImpl.java   
@Override
public Collection<String> getTypes() {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<String> query = cb.createQuery(String.class);
    Root<MCRCategoryLinkImpl> li = query.from(LINK_CLASS);
    return em
        .createQuery(
            query
                .distinct(true)
                .select(li.get(MCRCategoryLinkImpl_.objectReference).get(MCRCategLinkReference_.type)))
        .setHint(QueryHints.READ_ONLY, "true")
        .getResultList();
}
项目:mycore    文件:MCRCategLinkServiceImpl.java   
@Override
public Collection<MCRCategoryLink> getLinks(String type) {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MCRCategoryLink> query = cb.createQuery(MCRCategoryLink.class);
    Root<MCRCategoryLinkImpl> li = query.from(LINK_CLASS);
    return em
        .createQuery(
            query
                .where(
                    cb.equal(li.get(MCRCategoryLinkImpl_.objectReference).get(MCRCategLinkReference_.type), type)))
        .setHint(QueryHints.READ_ONLY, "true")
        .getResultList();
}
项目:appengine-hibernate    文件:TestResources.java   
@GET
@Path("greetings")
@Produces(MediaType.TEXT_PLAIN)
public Response queryGreetings(@QueryParam("maxResults") int maxResults) {
    EntityManager em = Hibernate.createEntityManager();
    final List<Tuple> greetings = em
            .createQuery("SELECT g.id, g.date, g.author.name, g.content FROM Greeting g ", Tuple.class)
            .setHint(QueryHints.READ_ONLY, "true")
            .setMaxResults(maxResults == 0 ? 500 : maxResults)
            .getResultList();
    em.close();

    return RequestStats.wrap(Response.ok(toString(greetings)));
}
项目:stdlib    文件:HibernateDao.java   
@Override
@Transactional(readOnly = true)
public T getById(ID id)
{
    if (id == null)
        throw new IllegalArgumentException("Must supply an id to retrieve!");

    Map<String, Object> hints = Collections.singletonMap(QueryHints.FETCHGRAPH, getQEntity().getDefaultGraph(getSession()));

    return getSession().find(clazz, id, hints);
}
项目:lams    文件:QueryBinder.java   
/**
 * Binds {@link javax.persistence.NamedQuery} as well as {@link org.hibernate.annotations.NamedQuery}.
 *
 * @param metadata the current metadata
 * @param annotation the named query annotation
 */
private static void bindNamedQuery(MetadataImplementor metadata, AnnotationInstance annotation) {
    String name = JandexHelper.getValue( annotation, "name", String.class );
    if ( StringHelper.isEmpty( name ) ) {
        throw new AnnotationException( "A named query must have a name when used in class or package level" );
    }

    String query = JandexHelper.getValue( annotation, "query", String.class );

    AnnotationInstance[] hints = JandexHelper.getValue( annotation, "hints", AnnotationInstance[].class );

    String cacheRegion = getString( hints, QueryHints.CACHE_REGION );
    if ( StringHelper.isEmpty( cacheRegion ) ) {
        cacheRegion = null;
    }

    Integer timeout = getTimeout( hints, query );
    if ( timeout != null && timeout < 0 ) {
        timeout = null;
    }

    Integer fetchSize = getInteger( hints, QueryHints.FETCH_SIZE, name );
    if ( fetchSize != null && fetchSize < 0 ) {
        fetchSize = null;
    }

    String comment = getString( hints, QueryHints.COMMENT );
    if ( StringHelper.isEmpty( comment ) ) {
        comment = null;
    }

    metadata.addNamedQuery(
            new NamedQueryDefinitionBuilder().setName( name ).setQuery( query ).setCacheable(
                    getBoolean(
                            hints,
                            QueryHints.CACHEABLE,
                            name
                    )
            ).setCacheRegion( cacheRegion ).setTimeout( timeout ).setFetchSize( fetchSize ).setFlushMode(
                    getFlushMode( hints, QueryHints.FLUSH_MODE, name )
            ).setCacheMode( getCacheMode( hints, QueryHints.CACHE_MODE, name ) ).setReadOnly(
                    getBoolean(
                            hints,
                            QueryHints.READ_ONLY,
                            name
                    )
            ).setComment( comment ).setParameterTypes( null ).createNamedQueryDefinition()
    );
    LOG.debugf( "Binding named query: %s => %s", name, query );
}