Refine search
@Override public void initialize(Object proxy) throws DataAccessException { try { Hibernate.initialize(proxy); } catch (HibernateException ex) { throw SessionFactoryUtils.convertHibernateAccessException(ex); } }
@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 ); }
@Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("entity."); builder.append(Hibernate.getClass(this).getSimpleName()); builder.append("<"); builder.append(getId()); builder.append("-"); builder.append(super.toString()); builder.append(">"); return builder.toString(); } }
/** * 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() ); }
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(); } }
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 ); s.setReadOnly( dp, true ); checkReadOnly( s, dp,true ); assertFalse( Hibernate.isInitialized( dp )); Hibernate.initialize( dp ); assertTrue( Hibernate.isInitialized( dp )); checkReadOnly( s, dp,true ); s.setReadOnly( dp, false ); checkReadOnly( s, dp, false ); dp.setDescription( "changed" ); assertTrue( Hibernate.isInitialized( dp ) ); assertEquals( "changed", dp.getDescription() ); s.flush();
@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(); }
@Test @TestForIssue( jiraKey = "HHH-9637") public void testExplicitJoinAfterFetchJoins() { Session s = openSession(); s.getTransaction().begin(); Entity1 e1Queryied = (Entity1) s.createQuery( "select e1 from Entity1 e1 inner join fetch e1.entity2 e2 inner join fetch e2.entity3 inner join e1.entity2 e1Restrict where e1Restrict.value = 'entity2'" ) .uniqueResult(); assertEquals( "entity1", e1Queryied.getValue() ); assertTrue( Hibernate.isInitialized( e1Queryied.getEntity2() ) ); assertTrue( Hibernate.isInitialized( e1Queryied.getEntity2().getEntity3() ) ); s.getTransaction().commit(); s.close(); }
@Test public void testInitializedProxySerializationAfterSessionClosed() { Session s = openSession(); Transaction t = s.beginTransaction(); DataPoint dp = new DataPoint(); dp.setDescription("a data point"); dp.setX( new BigDecimal(1.0) ); dp.setY( new BigDecimal(2.0) ); s.persist(dp); s.flush(); s.clear(); dp = (DataPoint) s.load( DataPoint.class, new Long( dp.getId() ) ); assertFalse( Hibernate.isInitialized(dp) ); Hibernate.initialize( dp ); assertTrue( Hibernate.isInitialized(dp) ); s.close(); SerializationHelper.clone( dp ); s = openSession(); t = s.beginTransaction(); s.delete( dp ); t.commit(); s.close(); }
public void perform(TestData data) { Session session = openSession(); session.beginTransaction(); session.enableFetchProfile( "enrollment.details" ); Enrollment enrollment = ( Enrollment ) session.createCriteria( Enrollment.class ).uniqueResult(); assertEquals( 3, sessionFactory().getStatistics().getEntityLoadCount() ); // enrollment + (section + student) assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() ); assertTrue( Hibernate.isInitialized( enrollment.getOffering() ) ); assertTrue( Hibernate.isInitialized( enrollment.getStudent() ) ); assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() ); session.getTransaction().commit(); session.close(); } }
@Test public void testProxiedBridgeMethod() throws Exception { //bridge methods should not be proxied Session s = openSession(); Transaction tx = s.beginTransaction(); Hammer h = new Hammer(); s.save(h); s.flush(); s.clear(); assertNotNull( "The proxy creation failure is breaking things", h.getId() ); h = (Hammer) s.load( Hammer.class, h.getId() ); assertFalse( Hibernate.isInitialized( h ) ); tx.rollback(); s.close(); }
@Test public void testEntityKeyElementTarget() throws Exception { Session s = openSession(); Transaction tx = s.beginTransaction(); Atmosphere atm = new Atmosphere(); Gas o2 = new Gas(); o2.name = "oxygen"; atm.composition.put( o2, 94.3 ); s.persist( o2 ); s.persist( atm ); s.flush(); s.clear(); atm = (Atmosphere) s.get( Atmosphere.class, atm.id ); assertTrue( ! Hibernate.isInitialized( atm.composition ) ); assertEquals( 1, atm.composition.size() ); assertEquals( o2.name, atm.composition.keySet().iterator().next().name ); tx.rollback(); s.close(); }
sessionFactory().getStatistics().clear(); Session s = openSession(); Transaction t = s.beginTransaction(); Item i1 = new Item("foo"); Item i2 = new Item("bar"); s.persist("Item", i1); s.persist("Item", i2); t.commit(); s.close(); i1 = (Item) iter.next(); i2 = (Item) iter.next(); assertFalse( Hibernate.isInitialized(i1) ); assertFalse( Hibernate.isInitialized(i2) ); i1.getName(); i2.getName(); assertFalse( Hibernate.isInitialized(i1) ); assertFalse( Hibernate.isInitialized(i2) ); assertEquals( i1.getName(), "foo" ); assertEquals( i2.getName(), "bar" ); Hibernate.initialize(i1); assertFalse( iter.hasNext() ); s.delete(i1);
@Test public void test() { doInHibernate( this::sessionFactory, session -> { Lazy p = new Lazy(); p.id = ID; User u = new User(); u.id = ID; u.setLazy( p ); session.persist( u ); } ); doInHibernate( this::sessionFactory, session -> { session.delete( session.get( Lazy.class, ID ) ); } ); doInHibernate( this::sessionFactory, session -> { User user = session.find( User.class, ID ); assertFalse( Hibernate.isPropertyInitialized( user, "lazy" ) ); assertNull( user.getLazy() ); assertTrue( Hibernate.isPropertyInitialized( user, "lazy" ) ); } ); }
@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 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 public void testManagedWithUninitializedAssociation() { // Delete the Parent doInHibernate( this::sessionFactory, s -> { Parent loadedParent = (Parent) s.createQuery( "SELECT p FROM Parent p WHERE name=:name" ) .setParameter( "name", "PARENT" ) .uniqueResult(); checkInterceptor( loadedParent, false ); assertFalse( Hibernate.isPropertyInitialized( loadedParent, "children" ) ); s.delete( loadedParent ); } ); // If the lazy relation is not fetch on cascade there is a constraint violation on commit }
@Test @TestForIssue(jiraKey = "HHH-13129") public void testDetachedWithInitializedAssociation() { final Parent detachedParent = doInHibernate( this::sessionFactory, s -> { Parent parent = s.get( Parent.class, originalParent.getId() ); assertFalse( Hibernate.isPropertyInitialized( parent, "children" ) ); // initialize collection before detaching parent.getChildren(); return parent; } ); assertTrue( Hibernate.isPropertyInitialized( detachedParent, "children" ) ); checkInterceptor( detachedParent, false ); // Delete the detached Parent with initialized children doInHibernate( this::sessionFactory, s -> { s.delete( detachedParent ); } ); // If the lazy relation is not fetch on cascade there is a constraint violation on commit }
@Test public void testBlob() throws SQLException { final int id = doInHibernate( this::sessionFactory, s -> { FileBlob file = new FileBlob(); file.setBlob( s.getLobHelper().createBlob( "TEST CASE".getBytes() ) ); // merge transient entity file = (FileBlob) s.merge( file ); return file.getId(); } ); doInHibernate( this::sessionFactory, s -> { FileBlob file = s.get( FileBlob.class, id ); assertFalse( Hibernate.isPropertyInitialized( file, "blob" ) ); Blob blob = file.getBlob(); try { assertTrue( Arrays.equals( "TEST CASE".getBytes(), blob.getBytes( 1, (int) file.getBlob().length() ) ) ); } catch (SQLException ex) { fail( "could not determine Lob length" ); } }); }