protected boolean reassociateIfUninitializedProxy(Object object, SessionImplementor source) { if ( !Hibernate.isInitialized(object) ) { throw new PersistentObjectException("uninitialized proxy passed to save()"); } else { return false; } }
@Override @SuppressWarnings({ "unchecked" }) public final String toLoggableString(Object value, SessionFactoryImplementor factory) { if ( value == LazyPropertyInitializer.UNFETCHED_PROPERTY || !Hibernate.isInitialized( value ) ) { return "<uninitialized>"; } return javaTypeDescriptor.extractLoggableRepresentation( (T) value ); }
/** * 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; } }
public boolean isReachable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) { //lazy, don't load return Hibernate.isInitialized( traversableObject ) && Hibernate.isPropertyInitialized( traversableObject, traversableProperty.getName() ); }
@Override public boolean reassociateIfUninitializedProxy(Object value) throws MappingException { if ( !Hibernate.isInitialized( value ) ) { final HibernateProxy proxy = (HibernateProxy) value; final LazyInitializer li = proxy.getHibernateLazyInitializer(); reassociateProxy( li, proxy ); return true; } else { return false; } }
private boolean isFoundInParent( String property, Object childEntity, EntityPersister persister, CollectionPersister collectionPersister, Object potentialParent) { final Object collection = persister.getPropertyValue( potentialParent, property ); return collection != null && Hibernate.isInitialized( collection ) && collectionPersister.getCollectionType().contains( collection, childEntity, session ); }
private Object getIndexInParent( String property, Object childEntity, EntityPersister persister, CollectionPersister collectionPersister, Object potentialParent){ final Object collection = persister.getPropertyValue( potentialParent, property ); if ( collection != null && Hibernate.isInitialized( collection ) ) { return collectionPersister.getCollectionType().indexOf( collection, childEntity ); } else { return null; } }
@Override public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException { //TODO: terrible implementation! if ( value == null ) { return "null"; } if ( value == LazyPropertyInitializer.UNFETCHED_PROPERTY || !Hibernate.isInitialized( value ) ) { return "<uninitialized>"; } Class valueClass = HibernateProxyHelper.getClassWithoutInitializingProxy( value ); return factory.getTypeHelper().entity( valueClass ).toLoggableString( value, factory ); }
@Test public void testOneToOneLazyLoading() { doInHibernate( this::sessionFactory, s -> { PostDetails post = (PostDetails) s.createQuery("select a from PostDetails a").getResultList().get(0); assertFalse(isInitialized(post.post)); } ); }
public void check(Object results) { List resultList = ( List ) results; assertEquals( 2, resultList.size() ); assertEquals( courseMeetingExpected1, resultList.get( 0 ) ); assertEquals( courseMeetingExpected2, resultList.get( 1 ) ); assertTrue( Hibernate.isInitialized( ((CourseMeeting) resultList.get( 0 )).getCourse() ) ); assertTrue( Hibernate.isInitialized( ((CourseMeeting) resultList.get( 1 )).getCourse() ) ); assertEquals( courseExpected, ((CourseMeeting) resultList.get( 0 )).getCourse() ); assertEquals( courseExpected, ((CourseMeeting) resultList.get( 1 )).getCourse() ); } };
public void check(Object results) { List resultList = ( List ) results; assertEquals( 2, resultList.size() ); assertEquals( yogiExpected, resultList.get( 0 ) ); assertEquals( shermanExpected, resultList.get( 1 ) ); assertNotNull( ((Student) resultList.get( 0 )).getEnrolments() ); assertFalse( Hibernate.isInitialized( ((Student) resultList.get( 0 )).getEnrolments() ) ); assertNotNull( ( ( Student ) resultList.get( 1 ) ).getEnrolments() ); assertFalse( Hibernate.isInitialized( ( ( Student ) resultList.get( 1 ) ).getEnrolments() ) ); } };
@Test public void testRefreshLockInitializedProxy() { Session s = openSession(); Transaction t = s.beginTransaction(); DataPoint dp = newPersistentDataPoint( s ); dp = ( DataPoint ) s.load( DataPoint.class, new Long( dp.getId() ) ); dp.getX(); assertTrue( Hibernate.isInitialized( dp ) ); s.refresh( dp, LockOptions.UPGRADE ); assertSame( LockOptions.UPGRADE.getLockMode(), s.getCurrentLockMode( dp ) ); s.delete( dp ); t.commit(); s.close(); }
@Test public void testLockUninitializedProxy() { Session s = openSession(); Transaction t = s.beginTransaction(); DataPoint dp = newPersistentDataPoint( s ); dp = ( DataPoint) s.load( DataPoint.class, new Long( dp.getId() ) ); assertFalse( Hibernate.isInitialized( dp ) ); s.buildLockRequest( LockOptions.UPGRADE ).lock( dp ); assertSame( LockOptions.UPGRADE.getLockMode(), s.getCurrentLockMode( dp ) ); s.delete( dp ); t.commit(); s.close(); } }
@Test @FailureExpected( jiraKey = "HHH-1645", message = "Session.refresh with LockOptions does not work on uninitialized proxies" ) public void testRefreshLockUninitializedProxy() { Session s = openSession(); Transaction t = s.beginTransaction(); DataPoint dp = newPersistentDataPoint( s ); dp = ( DataPoint ) s.load( DataPoint.class, new Long( dp.getId() ) ); assertFalse( Hibernate.isInitialized( dp ) ); s.refresh( dp, LockOptions.UPGRADE ); assertSame( LockOptions.UPGRADE.getLockMode(), s.getCurrentLockMode( dp ) ); s.delete( dp ); t.commit(); s.close(); }
public void perform(TestData data) { Session session = openSession(); session.beginTransaction(); session.enableFetchProfile( "offering.details" ); CourseOffering section = ( CourseOffering ) session.get( CourseOffering.class, data.getSectionId() ); assertEquals( 3, sessionFactory().getStatistics().getEntityLoadCount() ); // section + (enrollments + course) assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() ); assertTrue( Hibernate.isInitialized( section.getEnrollments() ) ); session.getTransaction().commit(); session.close(); } }
public void perform(TestData data) { Session session = openSession(); session.beginTransaction(); session.enableFetchProfile( "course.details" ); Course course = ( Course ) session.get( Course.class, data.getCourseId() ); assertEquals( 2, sessionFactory().getStatistics().getEntityLoadCount() ); // course + department assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() ); assertTrue( Hibernate.isInitialized( course.getCode().getDepartment() ) ); session.getTransaction().commit(); session.close(); } }
private void checkProxyReadOnly(Session s, Object proxy, boolean expectedReadOnly) { assertTrue( proxy instanceof HibernateProxy ); LazyInitializer li = ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer(); assertSame( s, li.getSession() ); assertEquals( expectedReadOnly, s.isReadOnly( proxy ) ); assertEquals( expectedReadOnly, li.isReadOnly() ); assertEquals( Hibernate.isInitialized( proxy ), ! li.isUninitialized() ); if ( Hibernate.isInitialized( proxy ) ) { assertEquals( expectedReadOnly, s.isReadOnly( li.getImplementation() ) ); } }
private void checkReadOnly(Session s, Object proxy, boolean expectedReadOnly) { assertTrue( proxy instanceof HibernateProxy ); LazyInitializer li = ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer(); assertSame( s, li.getSession() ); assertEquals( expectedReadOnly, s.isReadOnly( proxy ) ); assertEquals( expectedReadOnly, li.isReadOnly() ); assertEquals( Hibernate.isInitialized( proxy ), ! li.isUninitialized() ); if ( Hibernate.isInitialized( proxy ) ) { assertEquals( expectedReadOnly, s.isReadOnly( li.getImplementation() ) ); } } }
public void check(Object results) { assertTrue( results instanceof Course ); assertEquals( courseExpected, results ); assertTrue( Hibernate.isInitialized( courseExpected.getCourseMeetings() ) ); assertEquals( courseExpected.getCourseMeetings(), courseExpected.getCourseMeetings() ); } };
public void check(Object results) { assertTrue( results instanceof Student ); assertEquals( shermanExpected, results ); assertNotNull( ((Student) results).getEnrolments() ); assertFalse( Hibernate.isInitialized( ((Student) results).getEnrolments() ) ); assertNull( ((Student) results).getPreferredCourse() ); } };