1 package org.hibernate.test.cascade; 3 4 import org.hibernate.test.TestCase; 5 import org.hibernate.Session; 6 import org.hibernate.Transaction; 7 8 import java.util.Date ; 9 import java.util.Iterator ; 10 import java.sql.Connection ; 11 import java.sql.PreparedStatement ; 12 13 import junit.framework.Test; 14 import junit.framework.TestSuite; 15 16 21 public class RefreshTest extends TestCase { 22 23 public RefreshTest(String name) { 24 super( name ); 25 } 26 27 protected String [] getMappings() { 28 return new String [] { 29 "cascade/Job.hbm.xml", 30 "cascade/JobBatch.hbm.xml" 31 }; 32 } 33 34 public static Test suite() { 35 return new TestSuite( RefreshTest.class ); 36 } 37 38 public void testRefreshCascade() throws Throwable { 39 Session session = openSession(); 40 Transaction txn = session.beginTransaction(); 41 42 JobBatch batch = new JobBatch( new Date () ); 43 batch.createJob().setProcessingInstructions( "Just do it!" ); 44 batch.createJob().setProcessingInstructions( "I know you can do it!" ); 45 46 session.persist( batch ); 48 session.flush(); 49 50 updateStatuses( session.connection() ); 52 53 session.refresh( batch ); 55 56 Iterator itr = batch.getJobs().iterator(); 57 while( itr.hasNext() ) { 58 Job job = ( Job ) itr.next(); 59 assertEquals( "Jobs not refreshed!", 1, job.getStatus() ); 60 } 61 62 txn.rollback(); 63 session.close(); 64 } 65 66 private void updateStatuses(Connection connection) throws Throwable { 67 68 PreparedStatement stmnt = null; 69 try { 70 stmnt = connection.prepareStatement( "UPDATE t_job SET job_status = 1" ); 71 stmnt.executeUpdate(); 72 } 73 finally { 74 if ( stmnt != null ) { 75 try { 76 stmnt.close(); 77 } 78 catch( Throwable ignore ) { 79 } 80 } 81 } 82 } 83 } 84 | Popular Tags |