@Override public boolean contains(final Object entity) throws DataAccessException { Boolean result = executeWithNativeSession(session -> session.contains(entity)); Assert.state(result != null, "No contains result"); return result; }
@Override public boolean contains(final Object entity) throws DataAccessException { Boolean result = executeWithNativeSession(session -> session.contains(entity)); Assert.state(result != null, "No contains result"); return result; }
public Object getCurrentRevisionData(Session session, boolean persist) { // Generating the revision data if not yet generated if ( revisionData == null ) { revisionData = revisionInfoGenerator.generate(); } // Saving the revision data, if not yet saved and persist is true if ( !session.contains( revisionData ) && persist ) { revisionInfoGenerator.saveRevisionData( session, revisionData ); } return revisionData; }
@Test public void testWriteAndFlushSunnyDayHibernate4() throws Exception { writer.setSessionFactory(factory); when(factory.getCurrentSession()).thenReturn(currentSession); when(currentSession.contains("foo")).thenReturn(true); when(currentSession.contains("bar")).thenReturn(false); currentSession.saveOrUpdate("bar"); currentSession.flush(); currentSession.clear(); List<String> items = Arrays.asList(new String[] { "foo", "bar" }); writer.write(items); }
@SuppressWarnings("deprecation") @Test public void testWriteAndFlushSunnyDayHibernate3() throws Exception { this.writer.setSessionFactory(this.factory); when(this.currentSession.contains("foo")).thenReturn(true); when(this.currentSession.contains("bar")).thenReturn(false); this.currentSession.saveOrUpdate("bar"); this.currentSession.flush(); this.currentSession.clear(); List<String> items = Arrays.asList(new String[] { "foo", "bar" }); writer.write(items); }
@Test public void testWriteAndFlushWithFailureHibernate4() throws Exception { writer.setSessionFactory(factory); final RuntimeException ex = new RuntimeException("ERROR"); when(factory.getCurrentSession()).thenReturn(currentSession); when(currentSession.contains("foo")).thenThrow(ex); try { writer.write(Collections.singletonList("foo")); fail("Expected RuntimeException"); } catch (RuntimeException e) { assertEquals("ERROR", e.getMessage()); } } }
@SuppressWarnings("deprecation") @Test public void testWriteAndFlushWithFailureHibernate3() throws Exception { this.writer.setSessionFactory(this.factory); final RuntimeException ex = new RuntimeException("ERROR"); when(this.currentSession.contains("foo")).thenThrow(ex); try { writer.write(Collections.singletonList("foo")); fail("Expected RuntimeException"); } catch (RuntimeException e) { assertEquals("ERROR", e.getMessage()); } }
@Test @TestForIssue( jiraKey = "HHH-7912" ) public void testNormalUsage() { Session session = openSession(); session.beginTransaction(); session.save( new IsolatedEvictableEntity( 1 ) ); session.getTransaction().commit(); session.close(); session = openSession(); session.beginTransaction(); IsolatedEvictableEntity entity = (IsolatedEvictableEntity) session.get( IsolatedEvictableEntity.class, 1 ); assertTrue( session.contains( entity ) ); session.evict( entity ); assertFalse( session.contains( entity ) ); session.getTransaction().commit(); session.close(); session = openSession(); session.beginTransaction(); session.delete( entity ); session.getTransaction().commit(); session.close(); }
@Test @TestForIssue( jiraKey = "HHH-7912" ) public void testEvictingDetachedEntity() { Session session = openSession(); session.beginTransaction(); session.save( new IsolatedEvictableEntity( 1 ) ); session.getTransaction().commit(); session.close(); session = openSession(); session.beginTransaction(); IsolatedEvictableEntity entity = (IsolatedEvictableEntity) session.get( IsolatedEvictableEntity.class, 1 ); assertTrue( session.contains( entity ) ); // detach the entity session.evict( entity ); assertFalse( session.contains( entity ) ); // evict it again the entity session.evict( entity ); assertFalse( session.contains( entity ) ); session.getTransaction().commit(); session.close(); session = openSession(); session.beginTransaction(); session.delete( entity ); session.getTransaction().commit(); session.close(); }
@Test public void testRemoveContains() throws Exception { Session s = openSession(); s.beginTransaction(); Baz baz = new Baz(); baz.setDefaults(); s.save( baz ); s.flush(); assertTrue( s.contains(baz) ); s.evict( baz ); assertFalse( s.contains(baz) ); Baz baz2 = (Baz) s.load( Baz.class, baz.getCode() ); assertFalse( baz == baz2 ); s.delete(baz2); s.getTransaction().commit(); s.close(); }
tooth = mouth.teeth.iterator().next(); s.evict( mouth ); assertFalse( s.contains( tooth ) ); tx.commit(); s.close();
@Test public void testSaveOrUpdateManaged() { Session s = openSession(); Transaction tx = s.beginTransaction(); NumberedNode root = new NumberedNode( "root" ); s.saveOrUpdate( root ); tx.commit(); tx = s.beginTransaction(); NumberedNode child = new NumberedNode( "child" ); root.addChild( child ); s.saveOrUpdate( root ); assertFalse( s.contains( child ) ); s.flush(); assertTrue( s.contains( child ) ); tx.commit(); assertTrue( root.getChildren().contains( child ) ); assertEquals( root.getChildren().size(), 1 ); tx = s.beginTransaction(); assertEquals( Long.valueOf( 2 ), s.createCriteria( NumberedNode.class ) .setProjection( Projections.rowCount() ) .uniqueResult() ); s.delete( root ); s.delete( child ); tx.commit(); s.close(); }
@Test public void testSaveOrUpdateCopyAny() throws Exception { Session s = openSession(); s.beginTransaction(); Bar bar = new Bar(); One one = new One(); bar.setObject(one); s.save(bar); GlarchProxy g = bar.getComponent().getGlarch(); bar.getComponent().setGlarch(null); s.delete(g); s.flush(); assertTrue( s.contains(one) ); s.getTransaction().commit(); s.close(); s = openSession(); s.beginTransaction(); Bar bar2 = (Bar) s.merge( bar ); s.flush(); s.delete(bar2); s.flush(); s.getTransaction().commit(); s.close(); }
@Test public void testDetachedIsReadOnlyAfterEvictViaSession() { 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 ); assertTrue( s.contains( dp ) ); s.evict( dp ); assertFalse( s.contains( dp ) ); assertNull( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getSession() ); try { s.isReadOnly( dp ); fail( "should have failed because proxy was detached" ); } catch ( TransientObjectException ex) { // expected assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { s.delete( dp ); s.getTransaction().commit(); s.close(); } }
@Test public void testDetachedSetReadOnlyAfterEvictViaSession() { 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 ); assertTrue( s.contains( dp ) ); s.evict( dp ); assertFalse( s.contains( dp ) ); assertNull( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getSession() ); try { s.setReadOnly( dp, true ); fail( "should have failed because proxy was detached" ); } catch ( TransientObjectException ex) { // expected assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { s.delete( dp ); s.getTransaction().commit(); s.close(); } }
@Test public void testIsReadOnlyAfterSessionClosedViaLazyInitializer() { 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 ); s.getTransaction().commit(); assertTrue( s.contains( dp ) ); s.close(); assertNull( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getSession() ); try { ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnly(); fail( "should have failed because session was detached" ); } catch ( TransientObjectException ex) { // expected assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { s = openSession(); s.beginTransaction(); s.delete( dp ); s.getTransaction().commit(); s.close(); } }
@Test public void testSetReadOnlyAfterSessionClosedViaLazyInitializer() { 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 ); s.getTransaction().commit(); assertTrue( s.contains( dp ) ); s.close(); assertNull( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getSession() ); try { ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().setReadOnly( true ); fail( "should have failed because session was detached" ); } catch ( TransientObjectException ex) { // expected assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { s = openSession(); s.beginTransaction(); s.delete( dp ); s.getTransaction().commit(); s.close(); } }
@Test public void testDetachedSetReadOnlyAfterEvictViaLazyInitializer() { 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 ); s.evict( dp ); assertFalse( s.contains( dp ) ); assertNull( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getSession() ); try { ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().setReadOnly( true ); fail( "should have failed because proxy was detached" ); } catch ( TransientObjectException ex) { // expected assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { s.delete( dp ); s.getTransaction().commit(); s.close(); } }
@Test public void testDetachedIsReadOnlyAfterEvictViaLazyInitializer() { 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 ); s.evict( dp ); assertFalse( s.contains( dp ) ); assertNull( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().getSession() ); try { ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnly(); fail( "should have failed because proxy was detached" ); } catch ( TransientObjectException ex) { // expected assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() ); } finally { s.delete( dp ); s.getTransaction().commit(); s.close(); } }
assertNull( managedParent.$$_hibernate_getNextManagedEntity() ); assertTrue( s.contains( managedParent ) ); s.evict( managedParent ); assertFalse( s.contains( managedParent ) ); assertNotNull( managedParent.$$_hibernate_getEntityInstance() ); assertNull( managedParent.$$_hibernate_getEntityEntry() ); assertFalse( s.contains( managedParent ) ); assertNotNull( managedParent.$$_hibernate_getEntityInstance() ); assertNull( managedParent.$$_hibernate_getEntityEntry() );