org.hibernate.query.Query.setReadOnly()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(227)

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

Query.setReadOnly介绍

暂无

代码示例

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

/**
 * Creates a new {@link HibernateScrollableResultsIterator} for the given {@link Query}.
 *
 * @param jpaQuery must not be {@literal null}.
 */
HibernateScrollableResultsIterator(Query jpaQuery) {
  org.hibernate.query.Query<?> query = jpaQuery.unwrap(org.hibernate.query.Query.class);
  this.scrollableResults = query.setReadOnly(TransactionSynchronizationManager.isCurrentTransactionReadOnly())//
      .scroll(ScrollMode.FORWARD_ONLY);
}

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

@Test
  public void test_hql_read_only_entities_native_example() {

    doInJPA( this::entityManagerFactory, entityManager -> {
      //tag::hql-read-only-entities-native-example[]
      List<Call> calls = entityManager.createQuery(
        "select c " +
        "from Call c " +
        "join c.phone p " +
        "where p.number = :phoneNumber ", Call.class )
      .setParameter( "phoneNumber", "123-456-7890" )
      .unwrap( org.hibernate.query.Query.class )
      .setReadOnly( true )
      .getResultList();
      //end::hql-read-only-entities-native-example[]
    });
  }
}

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

int i = 0;
Iterator it = s.createQuery("from DataPoint dp order by dp.x asc")
    .setReadOnly( false )
    .iterate();
while ( it.hasNext() ) {

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

int i = 0;
Iterator it = s.createQuery("from DataPoint dp order by dp.x asc")
    .setReadOnly( true )
    .iterate();
while ( it.hasNext() ) {

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

int i = 0;
ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc")
    .setReadOnly( false )
    .scroll(ScrollMode.FORWARD_ONLY);
while ( sr.next() ) {

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

int i = 0;
ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc")
    .setReadOnly( true )
    .scroll(ScrollMode.FORWARD_ONLY);
while ( sr.next() ) {

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

@Test
public void testReadOnlyModeAutoFlushOnQuery() {
  clearCounts();
  Session s = openSession();
  Transaction t = s.beginTransaction();
  DataPoint dpFirst = null;
  for ( int i=0; i<100; i++ ) {
    DataPoint dp = new DataPoint();
    dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) );
    dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) );
    s.save(dp);
  }
  assertInsertCount( 0 );
  assertUpdateCount( 0 );
  ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc")
      .setReadOnly(true)
      .scroll(ScrollMode.FORWARD_ONLY);
  assertInsertCount( 100 );
  assertUpdateCount( 0 );
  clearCounts();
  while ( sr.next() ) {
    DataPoint dp = (DataPoint) sr.get(0);
    assertFalse( s.isReadOnly( dp ) );
    s.delete( dp );
  }
  t.commit();
  s.close();
  assertUpdateCount( 0 );
  assertDeleteCount( 100 );
}

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

int i = 0;
ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc")
    .setReadOnly(true)
    .scroll(ScrollMode.FORWARD_ONLY);
while ( sr.next() ) {

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

assertFalse( s.isDefaultReadOnly() );
DataPoint dp = ( DataPoint ) s.createQuery( "select c.lazyDataPoints from Container c join c.lazyDataPoints where c.id=" + cOrig.getId() )
    .setReadOnly( true ).uniqueResult();
assertTrue( s.isReadOnly( dp ) );
t.commit();

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

int i = 0;
ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc")
    .setReadOnly(false)
    .scroll(ScrollMode.FORWARD_ONLY);
int nExpectedChanges = 0;

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

assertTrue( s.isDefaultReadOnly() );
Container c = ( Container ) s.createQuery( "from Container where id=" + cOrig.getId() )
    .setReadOnly( false ).uniqueResult();
expectedInitializedObjects = new HashSet(
    Arrays.asList(

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

int i = 0;
ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc")
    .setReadOnly(true)
    .scroll(ScrollMode.FORWARD_ONLY);
int nExpectedChanges = 0;

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

.setReadOnly(false)
    .scroll(ScrollMode.FORWARD_ONLY);
int nExpectedChanges = 0;

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

assertFalse( s.isDefaultReadOnly() );
Container c = ( Container ) s.createQuery( "from Container where id=" + cOrig.getId() )
    .setReadOnly( true ).uniqueResult();
expectedInitializedObjects = new HashSet(
    Arrays.asList(

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

@Test
public void testModifiableViaSessionBeforeInitByModifiableQuery() {
  DataPoint dpOrig = createDataPoint( CacheMode.IGNORE );
  Session s = openSession();
  s.setCacheMode(CacheMode.IGNORE);
  s.beginTransaction();
  DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) );
  assertTrue( dp instanceof HibernateProxy );
  assertFalse( Hibernate.isInitialized( dp ) );
  checkReadOnly( s, dp, false );
  DataPoint dpFromQuery = ( DataPoint ) s.createQuery( "from DataPoint where id=" + dpOrig.getId() ).setReadOnly( false ).uniqueResult();
  assertTrue( Hibernate.isInitialized( dpFromQuery ) );
  assertSame( dp, dpFromQuery );
  checkReadOnly( s, dp, false );
  dp.setDescription( "changed" );
  assertEquals( "changed", dp.getDescription() );
  s.flush();
  s.getTransaction().commit();
  s.close();
  s = openSession();
  s.beginTransaction();
  dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() );
  assertEquals( dpOrig.getId(), dp.getId() );
  assertEquals( "changed", dp.getDescription() );
  assertEquals( dpOrig.getX(), dp.getX() );
  assertEquals( dpOrig.getY(), dp.getY() );
  s.delete( dp );
  s.getTransaction().commit();
  s.close();
}

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

@Test
public void testModifiableViaSessionBeforeInitByReadOnlyQuery() {
  DataPoint dpOrig = createDataPoint( CacheMode.IGNORE );
  Session s = openSession();
  s.setCacheMode(CacheMode.IGNORE);
  s.beginTransaction();
  DataPoint dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpOrig.getId() ) );
  assertTrue( dp instanceof HibernateProxy );
  checkReadOnly( s, dp, false );
  assertFalse( Hibernate.isInitialized( dp ) );
  DataPoint dpFromQuery = ( DataPoint ) s.createQuery( "from DataPoint where id=" + dpOrig.getId() ).setReadOnly( true ).uniqueResult();
  assertTrue( Hibernate.isInitialized( dpFromQuery ) );
  assertSame( dp, dpFromQuery );
  checkReadOnly( s, dp, false );
  dp.setDescription( "changed" );
  assertEquals( "changed", dp.getDescription() );
  s.flush();
  s.getTransaction().commit();
  s.close();
  s = openSession();
  s.beginTransaction();
  dp = ( DataPoint ) s.get( DataPoint.class, dpOrig.getId() );
  assertEquals( dpOrig.getId(), dp.getId() );
  assertEquals( "changed", dp.getDescription() );
  assertEquals( dpOrig.getX(), dp.getX() );
  assertEquals( dpOrig.getY(), dp.getY() );
  s.delete( dp );
  s.getTransaction().commit();
  s.close();
}

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

s.setReadOnly( dp, true );
checkReadOnly( s, dp, true );
DataPoint dpFromQuery = ( DataPoint ) s.createQuery( "from DataPoint where id=" + dpOrig.getId() ).setReadOnly( true ).uniqueResult();
assertTrue( Hibernate.isInitialized( dpFromQuery ) );
assertSame( dp, dpFromQuery );

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

checkReadOnly( s, dp, true );
assertFalse( Hibernate.isInitialized( dp ) );
DataPoint dpFromQuery = ( DataPoint ) s.createQuery( "from DataPoint where id=" + dpOrig.getId() ).setReadOnly( false ).uniqueResult();
assertTrue( Hibernate.isInitialized( dpFromQuery ) );
assertSame( dp, dpFromQuery );

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

protected void initQueryFromNamedDefinition(Query query, NamedQueryDefinition nqd) {
  // todo : cacheable and readonly should be Boolean rather than boolean...
  query.setCacheable( nqd.isCacheable() );
  query.setCacheRegion( nqd.getCacheRegion() );
  query.setReadOnly( nqd.isReadOnly() );
  if ( nqd.getTimeout() != null ) {
    query.setTimeout( nqd.getTimeout() );
  }
  if ( nqd.getFetchSize() != null ) {
    query.setFetchSize( nqd.getFetchSize() );
  }
  if ( nqd.getCacheMode() != null ) {
    query.setCacheMode( nqd.getCacheMode() );
  }
  if ( nqd.getComment() != null ) {
    query.setComment( nqd.getComment() );
  }
  if ( nqd.getFirstResult() != null ) {
    query.setFirstResult( nqd.getFirstResult() );
  }
  if ( nqd.getMaxResults() != null ) {
    query.setMaxResults( nqd.getMaxResults() );
  }
  if ( nqd.getFlushMode() != null ) {
    query.setHibernateFlushMode( nqd.getFlushMode() );
  }
}

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

/**
 * Creates a new {@link HibernateScrollableResultsIterator} for the given {@link Query}.
 *
 * @param jpaQuery must not be {@literal null}.
 */
HibernateScrollableResultsIterator(Query jpaQuery) {
  org.hibernate.query.Query<?> query = jpaQuery.unwrap(org.hibernate.query.Query.class);
  this.scrollableResults = query.setReadOnly(TransactionSynchronizationManager.isCurrentTransactionReadOnly())//
      .scroll(ScrollMode.FORWARD_ONLY);
}

相关文章