org.hibernate.Hibernate.initialize()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(277)

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

Hibernate.initialize介绍

[英]Force initialization of a proxy or persistent collection.

Note: This only ensures intialization of a proxy object or collection; it is not guaranteed that the elements INSIDE the collection will be initialized/materialized.
[中]强制初始化代理或持久集合。
注意:这仅确保代理对象或集合的初始化;不能保证集合中的元素将被初始化/物化。

代码示例

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

@Override
public void initialize(Object proxy) throws DataAccessException {
  try {
    Hibernate.initialize(proxy);
  }
  catch (HibernateException ex) {
    throw SessionFactoryUtils.convertHibernateAccessException(ex);
  }
}

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

/**
   * Force initialization of a proxy or persistent collection.
   * <p/>
   * Note: This only ensures initialization of a proxy object or collection;
   * it is not guaranteed that the elements INSIDE the collection will be initialized/materialized.
   *
   * @param proxy a persistable object, proxy, persistent collection or {@code null}
   * @throws HibernateException if we can't initialize the proxy at this time, eg. the {@link Session} was closed
   */
  protected <T> T initialize(T proxy) throws HibernateException {
    if (!Hibernate.isInitialized(proxy)) {
      Hibernate.initialize(proxy);
    }
    return proxy;
  }
}

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

@Override
public void initialize(Object proxy) throws DataAccessException {
  try {
    Hibernate.initialize(proxy);
  }
  catch (HibernateException ex) {
    throw SessionFactoryUtils.convertHibernateAccessException(ex);
  }
}

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

public void addQuestionAndInitializeLazyList(int index, Question question) {
    question.setQuizz(this);
    questions.add(index, question);
    Hibernate.initialize( questions );
  }
}

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

@Override
  public boolean onPreUpdate(PreUpdateEvent event) {
    executed = true;
    final Object[] oldValues = event.getOldState();
    final String[] properties = event.getPersister().getPropertyNames();
    // Iterate through all fields of the updated object
    for ( int i = 0; i < properties.length; i++ ) {
      if ( oldValues != null && oldValues[i] != null ) {
        if ( ! Hibernate.isInitialized( oldValues[i] ) ) {
          // force any proxies and/or collections to initialize to illustrate HHH-2763
          foundAny = true;
          Hibernate.initialize( oldValues );
        }
      }
    }
    return true;
  }
}

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

private void checkTestOrderByResult(Object result,
                    Zoo zooExpected,
                    Set<Zoo> zoosUnordered) {
    assertTrue( result instanceof Object[] );
    Object[] resultArray = ( Object[] ) result;
    assertEquals( 2,  resultArray.length );
    Hibernate.initialize( ( ( Address ) resultArray[ 1 ] ).getStateProvince() );
    if ( zooExpected == null ) {
      Zoo zooResult = new Zoo();
      zooResult.setName( ( String ) resultArray[ 0 ] );
      zooResult.setAddress( ( Address ) resultArray[ 1 ] );
      assertTrue( zoosUnordered.remove( zooResult ) );
    }
    else {
      assertEquals( zooExpected.getName(), ( ( Object[] ) result )[ 0 ] );
      assertEquals( zooExpected.getAddress(), ( ( Object[] ) result )[ 1 ] );
    }
  }
}

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

@Test
public void testReferencedOneToManyDifferentRevisions() {
  List queryResult = getAuditReader().createQuery().forRevisionsOfEntity( SetRefIngEntity.class, false, true )
      .add( AuditEntity.id().eq( 4 ) )
      .add( AuditEntity.revisionType().eq( RevisionType.DEL ) )
      .getResultList();
  Object[] objArray = (Object[]) queryResult.get( 0 );
  Assert.assertEquals( 4, getRevisionNumber( objArray[1] ) );
  SetRefIngEntity refIngEntity = (SetRefIngEntity) objArray[0];
  Assert.assertEquals( "Example Data 2", refIngEntity.getData() );
  Hibernate.initialize( refIngEntity.getReference() );
  Assert.assertEquals( "Demo Data 2", refIngEntity.getReference().getData() );
}

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

@Test
public void testReferencedOneToManySameRevision() {
  List queryResult = getAuditReader().createQuery().forRevisionsOfEntity( SetRefIngEntity.class, false, true )
      .add( AuditEntity.id().eq( 2 ) )
      .add( AuditEntity.revisionType().eq( RevisionType.DEL ) )
      .getResultList();
  Object[] objArray = (Object[]) queryResult.get( 0 );
  Assert.assertEquals( 2, getRevisionNumber( objArray[1] ) );
  SetRefIngEntity refIngEntity = (SetRefIngEntity) objArray[0];
  Assert.assertEquals( "Example Data 1", refIngEntity.getData() );
  Hibernate.initialize( refIngEntity.getReference() );
  Assert.assertEquals( "Demo Data 1", refIngEntity.getReference().getData() );
}

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

@Test
public void testManyToOneSelectException() {
  setupTest( PersonManyToOneSelectException.class, 1L, false );
  doInHibernate(
      this::sessionFactory, session -> {
        Person pCheck = session.find( PersonManyToOneSelectException.class, 1L );
        assertNotNull( pCheck );
        assertFalse( Hibernate.isInitialized( pCheck.getCity() ) );
        try {
          Hibernate.initialize( pCheck.getCity() );
          fail( "Should have thrown ObjectNotFoundException" );
        }
        catch (ObjectNotFoundException expected) {
          session.getTransaction().setRollbackOnly();
        }
      }
  );
}

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

@Test
public void testPkjcOneToOneSelectException() {
  setupTest( PersonPkjcSelectException.class, 1L, false );
  doInHibernate(
      this::sessionFactory, session -> {
        Person pCheck = session.find( PersonPkjcSelectException.class, 1L );
        assertNotNull( pCheck );
        assertFalse( Hibernate.isInitialized( pCheck.getCity() ) );
        try {
          Hibernate.initialize( pCheck.getCity() );
          fail( "Should have thrown ObjectNotFoundException" );
        }
        catch (ObjectNotFoundException expected) {
          session.getTransaction().setRollbackOnly();
        }
      }
  );
}

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

@Test
public void testOneToOneSelectException() {
  setupTest( PersonOneToOneSelectException.class, 1L, false );
  doInHibernate(
      this::sessionFactory, session -> {
        Person pCheck = session.find( PersonOneToOneSelectException.class, 1L );
        assertNotNull( pCheck );
        assertFalse( Hibernate.isInitialized( pCheck.getCity() ) );
        try {
          Hibernate.initialize( pCheck.getCity() );
          fail( "Should have thrown ObjectNotFoundException" );
        }
        catch (ObjectNotFoundException expected) {
          session.getTransaction().setRollbackOnly();
        }
      }
  );
}

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

@Test
public void testMapsIdOneToOneSelectException() {
  setupTest( PersonMapsIdSelectException.class, 1L, true );
  doInHibernate(
      this::sessionFactory, session -> {
        Person pCheck = session.find( PersonMapsIdSelectException.class, 1L );
        assertNotNull( pCheck );
        assertFalse( Hibernate.isInitialized( pCheck.getCity() ) );
        try {
          Hibernate.initialize( pCheck.getCity() );
          fail( "Should have thrown ObjectNotFoundException" );
        }
        catch (ObjectNotFoundException expected) {
          session.getTransaction().setRollbackOnly();
        }
      }
  );
}

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

@Test
public void testMapsIdJoinColumnOneToOneSelectException() {
  setupTest( PersonMapsIdColumnSelectException.class, 1L, true );
  doInHibernate(
      this::sessionFactory, session -> {
        Person pCheck = session.find( PersonMapsIdColumnSelectException.class, 1L );
        assertNotNull( pCheck );
        assertFalse( Hibernate.isInitialized( pCheck.getCity() ) );
        try {
          Hibernate.initialize( pCheck.getCity() );
          fail( "Should have thrown ObjectNotFoundException" );
        }
        catch (ObjectNotFoundException expected) {
          session.getTransaction().setRollbackOnly();
        }
      }
  );
}

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

@Test
public void testMapsIdJoinColumnOneToOneSelectException() {
  setupTest( PersonMapsIdColumnSelectException.class, 1L, true );
  doInHibernate(
      this::sessionFactory, session -> {
        Person pCheck = session.find( PersonMapsIdColumnSelectException.class, 1L );
        assertNotNull( pCheck );
        assertFalse( Hibernate.isInitialized( pCheck.getCity() ) );
        try {
          Hibernate.initialize( pCheck.getCity() );
          fail( "Should have thrown ObjectNotFoundException" );
        }
        catch (ObjectNotFoundException expected) {
          session.getTransaction().setRollbackOnly();
        }
      }
  );
}

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

@Test
public void testManyToOneSelectException() {
  setupTest( PersonManyToOneSelectException.class, 1L, false );
  doInHibernate(
      this::sessionFactory, session -> {
        Person pCheck = session.find( PersonManyToOneSelectException.class, 1L );
        assertNotNull( pCheck );
        assertFalse( Hibernate.isInitialized( pCheck.getCity() ) );
        try {
          Hibernate.initialize( pCheck.getCity() );
          fail( "Should have thrown ObjectNotFoundException" );
        }
        catch (ObjectNotFoundException expected) {
          session.getTransaction().setRollbackOnly();
        }
      }
  );
}

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

@Test
public void testMapsIdOneToOneSelectException() {
  setupTest( PersonMapsIdSelectException.class, 1L, true );
  doInHibernate(
      this::sessionFactory, session -> {
        Person pCheck = session.find( PersonMapsIdSelectException.class, 1L );
        assertNotNull( pCheck );
        assertFalse( Hibernate.isInitialized( pCheck.getCity() ) );
        try {
          Hibernate.initialize( pCheck.getCity() );
          fail( "Should have thrown ObjectNotFoundException" );
        }
        catch (ObjectNotFoundException expected) {
          session.getTransaction().setRollbackOnly();
        }
      }
  );
}

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

@Test
public void testReferringOneToManySameRevision() {
  List queryResult = getAuditReader().createQuery().forRevisionsOfEntity( SetRefEdEntity.class, false, true )
      .add( AuditEntity.id().eq( 1 ) )
      .add( AuditEntity.revisionType().eq( RevisionType.DEL ) )
      .getResultList();
  Object[] objArray = (Object[]) queryResult.get( 0 );
  Assert.assertEquals( 2, getRevisionNumber( objArray[1] ) );
  SetRefEdEntity refEdEntity = (SetRefEdEntity) objArray[0];
  Assert.assertEquals( "Demo Data 1", refEdEntity.getData() );
  Hibernate.initialize( refEdEntity.getReffering() );
  Assert.assertEquals(
      TestTools.makeSet( new SetRefIngEntity( 2, "Example Data 1" ) ),
      refEdEntity.getReffering()
  );
}

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

@Test
public void testOwnedManyToManySameRevision() {
  List queryResult = getAuditReader().createQuery().forRevisionsOfEntity( SetOwningEntity.class, false, true )
      .add( AuditEntity.id().eq( 5 ) )
      .add( AuditEntity.revisionType().eq( RevisionType.DEL ) )
      .getResultList();
  Object[] objArray = (Object[]) queryResult.get( 0 );
  Assert.assertEquals( 7, getRevisionNumber( objArray[1] ) );
  SetOwningEntity setOwningEntity = (SetOwningEntity) objArray[0];
  Assert.assertEquals( "Demo Data 1", setOwningEntity.getData() );
  Hibernate.initialize( setOwningEntity.getReferences() );
  Assert.assertEquals(
      TestTools.makeSet( new SetOwnedEntity( 6, "Example Data 1" ) ),
      setOwningEntity.getReferences()
  );
}

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

@Test
public void testOwningManyToManySameRevision() {
  List queryResult = getAuditReader().createQuery().forRevisionsOfEntity( SetOwnedEntity.class, false, true )
      .add( AuditEntity.id().eq( 6 ) )
      .add( AuditEntity.revisionType().eq( RevisionType.DEL ) )
      .getResultList();
  Object[] objArray = (Object[]) queryResult.get( 0 );
  Assert.assertEquals( 7, getRevisionNumber( objArray[1] ) );
  SetOwnedEntity setOwnedEntity = (SetOwnedEntity) objArray[0];
  Assert.assertEquals( "Example Data 1", setOwnedEntity.getData() );
  Hibernate.initialize( setOwnedEntity.getReferencing() );
  Assert.assertEquals(
      TestTools.makeSet( new SetOwningEntity( 5, "Demo Data 1" ) ),
      setOwnedEntity.getReferencing()
  );
}

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

@Test
public void testOwnedManyToManyDifferentRevisions() {
  List queryResult = getAuditReader().createQuery().forRevisionsOfEntity( SetOwningEntity.class, false, true )
      .add( AuditEntity.id().eq( 7 ) )
      .add( AuditEntity.revisionType().eq( RevisionType.DEL ) )
      .getResultList();
  Object[] objArray = (Object[]) queryResult.get( 0 );
  Assert.assertEquals( 9, getRevisionNumber( objArray[1] ) );
  SetOwningEntity setOwningEntity = (SetOwningEntity) objArray[0];
  Assert.assertEquals( "Demo Data 2", setOwningEntity.getData() );
  Hibernate.initialize( setOwningEntity.getReferences() );
  Assert.assertEquals(
      TestTools.makeSet( new SetOwnedEntity( 8, "Example Data 2" ) ),
      setOwningEntity.getReferences()
  );
}

相关文章