org.hibernate.Criteria.setCacheable()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(131)

本文整理了Java中org.hibernate.Criteria.setCacheable()方法的一些代码示例,展示了Criteria.setCacheable()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Criteria.setCacheable()方法的具体详情如下:
包路径:org.hibernate.Criteria
类名称:Criteria
方法名:setCacheable

Criteria.setCacheable介绍

[英]Enable caching of this query result, provided query caching is enabled for the underlying session factory.
[中]启用此查询结果的缓存,前提是为基础会话工厂启用了查询缓存。

代码示例

代码示例来源:origin: gocd/gocd

@Override
  public Object doInTransaction(TransactionStatus transactionStatus) {
    return sessionFactory.getCurrentSession()
        .createCriteria(PipelineState.class)
        .add(Restrictions.eq("pipelineName", pipelineName))
        .setCacheable(false).uniqueResult();
  }
});

代码示例来源:origin: gocd/gocd

@Override
  public Object doInTransaction(TransactionStatus status) {
    PropertyProjection pipelineName = Projections.property("pipelineName");
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PipelineState.class).setProjection(pipelineName).add(
        Restrictions.eq("locked", true));
    criteria.setCacheable(false);
    List<String> list = criteria.list();
    return list;
  }
});

代码示例来源:origin: gocd/gocd

@Override
public VersionInfo findByComponentName(final String name) {
  return (VersionInfo) transactionTemplate.execute((TransactionCallback) transactionStatus -> sessionFactory.getCurrentSession()
      .createCriteria(VersionInfo.class)
      .add(Restrictions.eq("componentName", name))
      .setCacheable(true).uniqueResult());
}

代码示例来源:origin: gocd/gocd

@Override
public JobAgentMetadata load(final Long jobId) {
  return (JobAgentMetadata) transactionTemplate.execute((TransactionCallback) transactionStatus -> sessionFactory.getCurrentSession()
      .createCriteria(JobAgentMetadata.class)
      .add(Restrictions.eq("jobId", jobId))
      .setCacheable(true).uniqueResult());
}

代码示例来源:origin: gocd/gocd

public Users findNotificationSubscribingUsers() {
  return (Users) transactionTemplate.execute((TransactionCallback) transactionStatus -> {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class);
    criteria.setCacheable(true);
    criteria.add(Restrictions.isNotEmpty("notificationFilters"));
    criteria.add(Restrictions.eq("enabled", true));
    return new Users(criteria.list());
  });
}

代码示例来源:origin: gocd/gocd

public User findUser(final String userName) {
  return (User) transactionTemplate.execute((TransactionCallback) transactionStatus -> {
    User user = (User) sessionFactory.getCurrentSession()
        .createCriteria(User.class)
        .add(Restrictions.eq("name", userName))
        .setCacheable(true).uniqueResult();
    return user == null ? new NullUser() : user;
  });
}

代码示例来源:origin: gocd/gocd

public EnvironmentVariables load(final Long entityId, final EnvironmentVariableType type) {
  List<EnvironmentVariable> result = (List<EnvironmentVariable>) transactionTemplate.execute((TransactionCallback) transactionStatus -> {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(EnvironmentVariable.class).add(Restrictions.eq("entityId", entityId)).add(
        Restrictions.eq("entityType", type.toString())).addOrder(Order.asc("id"));
    criteria.setCacheable(true);
    return criteria.list();
  });
  return new EnvironmentVariables(result);
}

代码示例来源:origin: hibernate/hibernate-orm

private State getState(Session s, String name) {
  Criteria criteria = s.createCriteria( State.class );
  criteria.add( Restrictions.eq( "name", name ) );
  criteria.setCacheable( true );
  return (State) criteria.list().get( 0 );
}

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Use a Criteria query - see FORGE-247
 */
public List listEventsWithCriteria() {
  Session session = sessionFactory.getCurrentSession();
  session.beginTransaction();
  List result = session.createCriteria(Event.class)
    .setCacheable(true)
    .list();
  session.getTransaction().commit();
  return result;
}

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Use a Criteria query - see FORGE-247
 */
public List listEventsWithCriteria() {
  Session session = sessionFactory.getCurrentSession();
  session.beginTransaction();
  List result = session.createCriteria(Event.class)
    .setCacheable(true)
    .list();
  session.getTransaction().commit();
  return result;
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public List<Offer> readOffersByAutomaticDeliveryType() {
  //TODO change this to a JPA criteria
  Criteria criteria = ((HibernateEntityManager) em).getSession().createCriteria(OfferImpl.class);
  Date myDate = getCurrentDateAfterFactoringInDateResolution();
  Calendar c = Calendar.getInstance();
  c.setTime(myDate);
  c.add(Calendar.DATE, +1);
  criteria.add(Restrictions.lt("startDate", c.getTime()));
  c = Calendar.getInstance();
  c.setTime(myDate);
  c.add(Calendar.DATE, -1);
  criteria.add(Restrictions.or(Restrictions.isNull("endDate"), Restrictions.gt("endDate", c.getTime())));
  criteria.add(Restrictions.or(Restrictions.eq("archiveStatus.archived", 'N'),
      Restrictions.isNull("archiveStatus.archived")));
  
  criteria.add(Restrictions.eq("automaticallyAdded", true));
  criteria.setCacheable(true);
  criteria.setCacheRegion("query.Offer");
  return criteria.list();
}

代码示例来源:origin: gocd/gocd

public long enabledUserCount() {
  Long value = (Long) goCache.get(ENABLED_USER_COUNT_CACHE_KEY);
  if (value != null) {
    return value;
  }
  synchronized (ENABLED_USER_COUNT_CACHE_KEY) {
    value = (Long) goCache.get(ENABLED_USER_COUNT_CACHE_KEY);
    if (value == null) {
      value = hibernateTemplate().execute(session -> (Long) session.createCriteria(User.class).add(Restrictions.eq("enabled", true)).setProjection(Projections.rowCount()).setCacheable(true).uniqueResult());
      goCache.put(ENABLED_USER_COUNT_CACHE_KEY, value);
    }
    return value;
  }
}

代码示例来源:origin: hibernate/hibernate-orm

private Criteria getCriteria(Session s) {
  Criteria crit = s.createCriteria( A.class, "anAlias" );
  crit.add( Restrictions.naturalId().set( "name", "name1" ) );
  crit.setFlushMode( FlushMode.COMMIT );
  crit.setCacheable( true );
  return crit;
}

代码示例来源:origin: gocd/gocd

@Override
public Plugin findPlugin(final String pluginId) {
  String cacheKey = cacheKeyForPluginSettings(pluginId);
  Plugin plugin = (Plugin) goCache.get(cacheKey);
  if (plugin != null) {
    return plugin;
  }
  synchronized (cacheKey) {
    plugin = (Plugin) goCache.get(cacheKey);
    if (plugin != null) {
      return plugin;
    }
    plugin = (Plugin) transactionTemplate.execute((TransactionCallback) transactionStatus -> sessionFactory.getCurrentSession()
        .createCriteria(Plugin.class)
        .add(Restrictions.eq("pluginId", pluginId))
        .setCacheable(true).uniqueResult());
    if (plugin != null) {
      goCache.put(cacheKey, plugin);
      return plugin;
    }
    goCache.remove(cacheKey);
    return new NullPlugin();
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Override
  protected Object getResults(Session s, boolean isSingleResult) throws Exception {
    Criteria criteria = getCriteria( s ).setCacheable( getQueryCacheMode() != CacheMode.IGNORE ).setCacheMode( getQueryCacheMode() );
    return ( isSingleResult ? criteria.uniqueResult() : criteria.list() );
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testNaturalIdCriteria() {
  Session s = openSession();
  s.beginTransaction();
  Account u = new Account(new AccountId(1), "testAcct" );
  s.persist( u );
  s.getTransaction().commit();
  s.close();
  s = openSession();
  s.beginTransaction();
  u = ( Account ) s.createCriteria( Account.class )
      .add( Restrictions.naturalId().set( "shortCode", "testAcct" ) )
      .setCacheable( true )
      .uniqueResult();
  assertNotNull( u );
  s.getTransaction().commit();
  s.close();
  s = openSession();
  s.beginTransaction();
  s.createQuery( "delete Account" ).executeUpdate();
  s.getTransaction().commit();
  s.close();
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Prepare the given Criteria object, applying cache settings and/or
 * a transaction timeout.
 * @param criteria the Criteria object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 */
protected void prepareCriteria(Criteria criteria) {
  if (isCacheQueries()) {
    criteria.setCacheable(true);
    if (getQueryCacheRegion() != null) {
      criteria.setCacheRegion(getQueryCacheRegion());
    }
  }
  if (getFetchSize() > 0) {
    criteria.setFetchSize(getFetchSize());
  }
  if (getMaxResults() > 0) {
    criteria.setMaxResults(getMaxResults());
  }
  ResourceHolderSupport sessionHolder =
      (ResourceHolderSupport) TransactionSynchronizationManager.getResource(obtainSessionFactory());
  if (sessionHolder != null && sessionHolder.hasTimeout()) {
    criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds());
  }
}

代码示例来源:origin: hibernate/hibernate-orm

u = ( User ) s.createCriteria( User.class )
    .add( Restrictions.naturalId().set( "userName", "steve" ) )
    .setCacheable( true )
    .uniqueResult();
assertNotNull( u );
u = ( User ) s.createCriteria( User.class )
    .add( Restrictions.naturalId().set( "userName", "steve" ) )
    .setCacheable( true )
    .uniqueResult();
assertNotNull( u );

代码示例来源:origin: org.springframework/spring-orm

/**
 * Prepare the given Criteria object, applying cache settings and/or
 * a transaction timeout.
 * @param criteria the Criteria object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 */
protected void prepareCriteria(Criteria criteria) {
  if (isCacheQueries()) {
    criteria.setCacheable(true);
    if (getQueryCacheRegion() != null) {
      criteria.setCacheRegion(getQueryCacheRegion());
    }
  }
  if (getFetchSize() > 0) {
    criteria.setFetchSize(getFetchSize());
  }
  if (getMaxResults() > 0) {
    criteria.setMaxResults(getMaxResults());
  }
  ResourceHolderSupport sessionHolder =
      (ResourceHolderSupport) TransactionSynchronizationManager.getResource(obtainSessionFactory());
  if (sessionHolder != null && sessionHolder.hasTimeout()) {
    criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds());
  }
}

代码示例来源:origin: hibernate/hibernate-orm

Criteria criteria = s.createCriteria( Citizen.class );
criteria.add( Restrictions.naturalId().set( "ssn", "1234" ).set( "state", france ) );
criteria.setCacheable( true );

相关文章