refreshtest.java

来自「好东西,hibernate-3.2.0,他是一开元的树杖hibernate-3.」· Java 代码 · 共 84 行

JAVA
84
字号
// $Id: RefreshTest.java 8991 2006-01-09 11:14:27Z maxcsaucdk $
package org.hibernate.test.cascade;

import org.hibernate.test.TestCase;
import org.hibernate.Session;
import org.hibernate.Transaction;

import java.util.Date;
import java.util.Iterator;
import java.sql.Connection;
import java.sql.PreparedStatement;

import junit.framework.Test;
import junit.framework.TestSuite;

/**
 * Implementation of RefreshTest.
 *
 * @author Steve Ebersole
 */
public class RefreshTest extends TestCase {

	public RefreshTest(String name) {
		super( name );
	}

	protected String[] getMappings() {
		return new String[] {
			"cascade/Job.hbm.xml",
			"cascade/JobBatch.hbm.xml"
		};
	}

	public static Test suite() {
		return new TestSuite( RefreshTest.class );
	}

	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( session.connection() );

		// 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();
	}

	private void updateStatuses(Connection connection) throws Throwable {

		PreparedStatement stmnt = null;
		try {
			stmnt = connection.prepareStatement( "UPDATE T_JOB SET JOB_STATUS = 1" );
			stmnt.executeUpdate();
		}
		finally {
			if ( stmnt != null ) {
				try {
					stmnt.close();
				}
				catch( Throwable ignore ) {
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?