@Override public void initialize(Object proxy) throws DataAccessException { try { Hibernate.initialize(proxy); } catch (HibernateException ex) { throw SessionFactoryUtils.convertHibernateAccessException(ex); } }
/** * 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; } }
@Override public void initialize(Object proxy) throws DataAccessException { try { Hibernate.initialize(proxy); } catch (HibernateException ex) { throw SessionFactoryUtils.convertHibernateAccessException(ex); } }
public void addQuestionAndInitializeLazyList(int index, Question question) { question.setQuizz(this); questions.add(index, question); Hibernate.initialize( questions ); } }
@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; } }
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 ] ); } } }
@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() ); }
@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() ); }
@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(); } } ); }
@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(); } } ); }
@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(); } } ); }
@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(); } } ); }
@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(); } } ); }
@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(); } } ); }
@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(); } } ); }
@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(); } } ); }
@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() ); }
@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() ); }
@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() ); }
@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() ); }