@Override public void refresh(Object object) { getSession().refresh(object); }
@Test // SPR-16956 @Transactional(readOnly = true) public void findSamWithReadOnlySession() { Person sam = personService.findByName(SAM); sam.setName("Vlad"); // By setting setDefaultReadOnly(true), the user can no longer modify any entity... Session session = sessionFactory.getCurrentSession(); session.flush(); session.refresh(sam); assertEquals("Sam", sam.getName()); }
@Test public void testRefreshUsingEntityName() { try (final Session session = openSession();) { session.refresh( "CustomName", customer ); } } }
@Test(expected = IllegalArgumentException.class) public void testRefreshDetachedInstance() { final Session session = openSession(); session.refresh( testEntity ); } }
@Test @TestForIssue(jiraKey = "HHH-12257") public void testRefreshLockedEntity() { doInHibernate( this::sessionFactory, session -> { A a = session.get( A.class, id, LockMode.PESSIMISTIC_READ ); checkLockMode( a, LockMode.PESSIMISTIC_READ, session ); session.refresh( a ); checkLockMode( a, LockMode.PESSIMISTIC_READ, session ); session.refresh( A.class.getName(), a ); checkLockMode( a, LockMode.PESSIMISTIC_READ, session ); session.refresh( a, Collections.emptyMap() ); checkLockMode( a, LockMode.PESSIMISTIC_READ, session ); session.refresh( a, null, Collections.emptyMap() ); checkLockMode( a, LockMode.PESSIMISTIC_READ, session ); } ); }
@Test @TestForIssue(jiraKey = "HHH-12257") public void testRefreshWithExplicitHigherLevelLockMode() { doInHibernate( this::sessionFactory, session -> { A a = session.get( A.class, id ); checkLockMode( a, LockMode.READ, session ); session.refresh( a, LockMode.UPGRADE_NOWAIT ); checkLockMode( a, LockMode.UPGRADE_NOWAIT, session ); session.refresh( a, LockModeType.PESSIMISTIC_READ ); checkLockMode( a, LockMode.PESSIMISTIC_READ, session ); session.refresh( a, LockModeType.PESSIMISTIC_WRITE, Collections.emptyMap() ); checkLockMode( a, LockMode.PESSIMISTIC_WRITE, session ); } ); }
@Test public void testRefresh() { doInHibernate( this::sessionFactory, s -> { Parent parent = s.find( Parent.class, parentID ); assertThat( parent, notNullValue() ); assertThat( parent, not( instanceOf( HibernateProxy.class ) ) ); assertFalse( isPropertyInitialized( parent, "children" ) ); checkDirtyTracking( parent ); s.refresh( parent ); s.flush(); } ); }
@Test @TestForIssue(jiraKey = "HHH-12257") public void testRefreshWithExplicitLowerLevelLockMode() { doInHibernate( this::sessionFactory, session -> { A a = session.get( A.class, id, LockMode.PESSIMISTIC_READ ); checkLockMode( a, LockMode.PESSIMISTIC_READ, session ); session.refresh( a, LockMode.READ ); checkLockMode( a, LockMode.PESSIMISTIC_READ, session ); session.refresh( a, LockModeType.READ ); checkLockMode( a, LockMode.PESSIMISTIC_READ, session ); session.refresh( a, LockModeType.READ, Collections.emptyMap() ); checkLockMode( a, LockMode.PESSIMISTIC_READ, session ); } ); }
@Test public void test() { doInHibernate( this::sessionFactory, s -> { s.refresh( myBar ); Assert.assertFalse( myBar.foos.isEmpty() ); // The issue is that currently, for some unknown reason, foos are deleted on flush } ); doInHibernate( this::sessionFactory, s -> { Bar bar = s.get( Bar.class, myBar.id ); Assert.assertFalse( bar.foos.isEmpty() ); } ); }
@Test public void testRefreshProxy() throws Exception { Session s = openSession(); s.beginTransaction(); Glarch g = new Glarch(); Serializable gid = s.save(g); s.flush(); s.clear(); GlarchProxy gp = (GlarchProxy) s.load(Glarch.class, gid); gp.getName(); //force init s.refresh(gp); s.delete(gp); s.flush(); s.getTransaction().commit(); s.close(); }
@Test @TestForIssue(jiraKey = "HHH-12257") public void testRefreshAfterUpdate() { doInHibernate( this::sessionFactory, session -> { A a = session.get( A.class, id ); checkLockMode( a, LockMode.READ, session ); a.setValue( "new value" ); session.flush(); checkLockMode( a, LockMode.WRITE, session ); session.refresh( a ); checkLockMode( a, LockMode.WRITE, session ); } ); }
@Test public void testUpdateOnComponent() { Session s = openSession(); Transaction t = s.beginTransaction(); Human human = new Human(); human.setName( new Name( "Stevee", 'X', "Ebersole" ) ); s.save( human ); s.flush(); t.commit(); String correctName = "Steve"; t = s.beginTransaction(); int count = s.createQuery( "update Human set name.first = :correction where id = :id" ) .setString( "correction", correctName ) .setLong( "id", human.getId().longValue() ) .executeUpdate(); assertEquals( "Incorrect update count", 1, count ); t.commit(); t = s.beginTransaction(); s.refresh( human ); assertEquals( "Update did not execute properly", correctName, human.getName().getFirst() ); s.createQuery( "delete Human" ).executeUpdate(); t.commit(); s.close(); }
@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 @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 @FailureExpected( jiraKey = "HHH-1645", message = "Session.refresh with LockOptions does not work on uninitialized proxies" ) public void testRefreshLockUninitializedProxyThenRead() { 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 ); dp.getX(); assertSame( LockOptions.UPGRADE.getLockMode(), s.getCurrentLockMode( dp ) ); s.delete( dp ); t.commit(); s.close(); }
@Test public void testRefreshCascade() throws Throwable { Session session = openSession(); Transaction txn = session.beginTransaction(); JobBatch batch = new JobBatch( new Date() ); batch.createJob().setProcessingInstructions( "Just do it!" ); batch.createJob().setProcessingInstructions( "I know you can do it!" ); // write the stuff to the database; at this stage all job.status values are zero session.persist( batch ); session.flush(); // behind the session's back, let's modify the statuses updateStatuses( (SessionImplementor)session ); // Now lets refresh the persistent batch, and see if the refresh cascaded to the jobs collection elements session.refresh( batch ); Iterator itr = batch.getJobs().iterator(); while( itr.hasNext() ) { Job job = ( Job ) itr.next(); assertEquals( "Jobs not refreshed!", 1, job.getStatus() ); } txn.rollback(); session.close(); }
@Test public void testOneToOneExplicitJoinColumn() throws Exception { assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", metadata() ) ); assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", metadata() ) ); assertTrue( ! SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", metadata() ) ); Session s = openSession(); s.getTransaction().begin(); Person e = new Person( FIRST_NAME, LAST_NAME ); s.persist( e ); MedicalHistory d = new MedicalHistory( e ); s.persist( d ); s.flush(); s.refresh( d ); s.getTransaction().commit(); s.close(); s = openSession(); s.getTransaction().begin(); PersonId pId = new PersonId( FIRST_NAME, LAST_NAME ); MedicalHistory d2 = (MedicalHistory) s.get( MedicalHistory.class, pId ); Person p2 = (Person) s.get( Person.class, pId ); assertEquals( pId.firstName, d2.patient.firstName ); assertEquals( pId.firstName, p2.firstName ); s.delete( d2 ); s.delete( p2 ); s.getTransaction().commit(); s.close(); }
@Test public void testTckLikeBehavior() throws Exception { assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", metadata() ) ); assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", metadata() ) ); assertTrue( ! SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", metadata() ) ); Session s = openSession(); s.getTransaction().begin(); Person e = new Person( FIRST_NAME, LAST_NAME ); s.persist( e ); MedicalHistory d = new MedicalHistory( e ); s.persist( d ); s.flush(); s.refresh( d ); s.getTransaction().commit(); // NOTE THAT WE LEAVE THE SESSION OPEN! s.getTransaction().begin(); PersonId pId = new PersonId( FIRST_NAME, LAST_NAME ); MedicalHistory d2 = (MedicalHistory) s.get( MedicalHistory.class, pId ); Person p2 = (Person) s.get( Person.class, pId ); assertEquals( pId.firstName, d2.patient.firstName ); assertEquals( pId.firstName, p2.firstName ); s.delete( d2 ); s.delete( p2 ); s.getTransaction().commit(); s.close(); }