[hibernate-commits] Hibernate SVN: r10846 - in branches/Branch_3_2/Hibernate3: src/org/hibernate src/org/hibernate/impl test/org/hibernate/test/sql

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Nov 17 23:21:07 EST 2006


Author: steve.ebersole at jboss.com
Date: 2006-11-17 23:21:05 -0500 (Fri, 17 Nov 2006)
New Revision: 10846

Modified:
   branches/Branch_3_2/Hibernate3/src/org/hibernate/SQLQuery.java
   branches/Branch_3_2/Hibernate3/src/org/hibernate/impl/SQLQueryImpl.java
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/sql/GeneralTest.java
Log:
HHH-2130 : SQLQuery + query space synchronization

Modified: branches/Branch_3_2/Hibernate3/src/org/hibernate/SQLQuery.java
===================================================================
--- branches/Branch_3_2/Hibernate3/src/org/hibernate/SQLQuery.java	2006-11-18 04:20:30 UTC (rev 10845)
+++ branches/Branch_3_2/Hibernate3/src/org/hibernate/SQLQuery.java	2006-11-18 04:21:05 UTC (rev 10846)
@@ -60,4 +60,34 @@
 	 * Use a predefined named ResultSetMapping
 	 */
 	public SQLQuery setResultSetMapping(String name);
+
+	/**
+	 * Adds a query space for auto-flush synchronization.
+	 *
+	 * @param querySpace The query space to be auto-flushed for this query.
+	 * @return this, for method chaning
+	 */
+	public SQLQuery addSynchronizedQuerySpace(String querySpace);
+
+	/**
+	 * Adds an entity name or auto-flush synchronization.
+	 *
+	 * @param entityName The name of the entity upon whose defined
+	 * query spaces we should additionally synchronize.
+	 * @return this, for method chaning
+	 * @throws MappingException Indicates the given entity name could not be
+	 * resolved.
+	 */
+	public SQLQuery addSynchronizedEntityName(String entityName) throws MappingException;
+
+	/**
+	 * Adds an entity name or auto-flush synchronization.
+	 *
+	 * @param entityClass The class of the entity upon whose defined
+	 * query spaces we should additionally synchronize.
+	 * @return this, for method chaning
+	 * @throws MappingException Indicates the given entity class could not be
+	 * resolved.
+	 */
+	public SQLQuery addSynchronizedEntityClass(Class entityClass) throws MappingException;
 }

Modified: branches/Branch_3_2/Hibernate3/src/org/hibernate/impl/SQLQueryImpl.java
===================================================================
--- branches/Branch_3_2/Hibernate3/src/org/hibernate/impl/SQLQueryImpl.java	2006-11-18 04:20:30 UTC (rev 10845)
+++ branches/Branch_3_2/Hibernate3/src/org/hibernate/impl/SQLQueryImpl.java	2006-11-18 04:21:05 UTC (rev 10846)
@@ -7,6 +7,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.io.Serializable;
 
 import org.hibernate.FlushMode;
 import org.hibernate.HibernateException;
@@ -47,7 +48,7 @@
 public class SQLQueryImpl extends AbstractQueryImpl implements SQLQuery {
 
 	private final List queryReturns;
-	private final Collection querySpaces;
+	private Collection querySpaces;
 	private final boolean callable;
 	private boolean autodiscovertypes;
 
@@ -56,6 +57,7 @@
 	 *
 	 * @param queryDef The representation of the defined <sql-query/>.
 	 * @param session The session to which this SQLQueryImpl belongs.
+	 * @param parameterMetadata Metadata about parameters found in the query.
 	 */
 	SQLQueryImpl(NamedSQLQueryDefinition queryDef, SessionImplementor session, ParameterMetadata parameterMetadata) {
 		super( queryDef.getQueryString(), queryDef.getFlushMode(), session, parameterMetadata );
@@ -296,7 +298,35 @@
 		}
 		return this;
 	}
-	
+
+	public SQLQuery addSynchronizedQuerySpace(String querySpace) {
+		if ( querySpaces == null ) {
+			querySpaces = new ArrayList();
+		}
+		querySpaces.add( querySpace );
+		return this;
+	}
+
+	public SQLQuery addSynchronizedEntityName(String entityName) {
+		return addQuerySpaces( getSession().getFactory().getEntityPersister( entityName ).getQuerySpaces() );
+	}
+
+	public SQLQuery addSynchronizedEntityClass(Class entityClass) {
+		return addQuerySpaces( getSession().getFactory().getEntityPersister( entityClass.getName() ).getQuerySpaces() );
+	}
+
+	private SQLQuery addQuerySpaces(Serializable[] spaces) {
+		if ( spaces != null ) {
+			if ( querySpaces == null ) {
+				querySpaces = new ArrayList();
+			}
+			for ( int i = 0; i < spaces.length; i++ ) {
+				querySpaces.add( spaces[i] );
+			}
+		}
+		return this;
+	}
+
 	public int executeUpdate() throws HibernateException {
 		Map namedParams = getNamedParams();
 		return getSession().executeNativeUpdate(generateQuerySpecification( namedParams ), getQueryParameters( namedParams ));

Modified: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/sql/GeneralTest.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/sql/GeneralTest.java	2006-11-18 04:20:30 UTC (rev 10845)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/sql/GeneralTest.java	2006-11-18 04:21:05 UTC (rev 10846)
@@ -15,6 +15,8 @@
 import org.hibernate.Query;
 import org.hibernate.Session;
 import org.hibernate.Transaction;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
 import org.hibernate.test.TestCase;
 import org.hibernate.transform.DistinctRootEntityResultTransformer;
 import org.hibernate.transform.Transformers;
@@ -28,6 +30,19 @@
 		super( x );
 	}
 
+	public static Test suite() {
+		return new TestSuite(GeneralTest.class);
+	}
+
+	protected String[] getMappings() {
+		return new String[] { "sql/General.hbm.xml" };
+	}
+
+	protected void configure(Configuration cfg) {
+		super.configure( cfg );
+		cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
+	}
+
 	protected String getOrganizationFetchJoinEmploymentSQL() {
 		return "SELECT org.ORGID as {org.id}, " +
 		       "        org.NAME as {org.name}, " +
@@ -67,11 +82,6 @@
 		       "    join PERSON pers on pers.PERID = emp.EMPLOYEE ";
 	}
 
-
-	protected String[] getMappings() {
-		return new String[] { "sql/General.hbm.xml" };
-	}
-
 	public void testFailOnNoAddEntityOrScalar() {
 		// Note: this passes, but for the wrong reason.
 		//      there is actually an exception thrown, but it is the database
@@ -92,6 +102,30 @@
 		}
 	}
 
+	public void testManualSynchronization() {
+		Session s = openSession();
+		s.beginTransaction();
+
+		sfi().getStatistics().clear();
+
+		// create an Organization...
+		Organization jboss = new Organization( "JBoss" );
+		s.persist( jboss );
+
+		// now query on Employment, this should not cause an auto-flush
+		s.createSQLQuery( getEmploymentSQL() ).list();
+		assertEquals( 0, sfi().getStatistics().getEntityInsertCount() );
+
+		// now try to query on Employment but this time add Organization as a synchronized query space...
+		s.createSQLQuery( getEmploymentSQL() ).addSynchronizedEntityClass( Organization.class ).list();
+		assertEquals( 1, sfi().getStatistics().getEntityInsertCount() );
+
+		// clean up
+		s.delete( jboss );
+		s.getTransaction().commit();
+		s.close();
+	}
+
 	public void testSQLQueryInterface() {
 		Session s = openSession();
 		Transaction t = s.beginTransaction();
@@ -372,7 +406,7 @@
 				"        {prod_orders}.orgid as orgid3_1_,\r\n" +
 				"        {prod_orders}.ordernumber as ordernum2_3_1_,\r\n" +
 				"        product.name as {product.name}," +
-				"        {prod_orders.element.*}," +
+				"        {prod_orders.element.*}" +
 				/*"        orders.PROD_NO as PROD4_3_1_,\r\n" +
 				"        orders.person as person3_1_,\r\n" +
 				"        orders.PROD_ORGID as PROD3_0__,\r\n" +
@@ -561,8 +595,4 @@
 		}
 	}
 
-	public static Test suite() {
-		return new TestSuite(GeneralTest.class);
-	}
-
 }




More information about the hibernate-commits mailing list