[hibernate-commits] Hibernate SVN: r21037 - in core/branches/Branch_3_3_2_GA_CP: testsuite/src/test/java/org/hibernate/test/joinedsubclass and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Jun 5 05:07:41 EDT 2012


Author: stliu
Date: 2012-06-05 05:07:40 -0400 (Tue, 05 Jun 2012)
New Revision: 21037

Modified:
   core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/hql/ast/tree/FromElementType.java
   core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java
Log:
JBPAPP-9227 Backport fix for HHH-1657 hql update generate wrong sql with joined subclass hierarchy to EAP5

Modified: core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/hql/ast/tree/FromElementType.java
===================================================================
--- core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/hql/ast/tree/FromElementType.java	2012-06-01 17:26:52 UTC (rev 21036)
+++ core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/hql/ast/tree/FromElementType.java	2012-06-05 09:07:40 UTC (rev 21037)
@@ -335,13 +335,13 @@
 			if ( forceAlias ) {
 				return propertyMapping.toColumns( tableAlias, path );
 			}
-			else if ( fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.SELECT ) {
+			if ( fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.SELECT ) {
 				return propertyMapping.toColumns( tableAlias, path );
 			}
-			else if ( fromElement.getWalker().getCurrentClauseType() == HqlSqlTokenTypes.SELECT ) {
+			if ( fromElement.getWalker().getCurrentClauseType() == HqlSqlTokenTypes.SELECT ) {
 				return propertyMapping.toColumns( tableAlias, path );
 			}
-			else if ( fromElement.getWalker().isSubQuery() ) {
+			if ( fromElement.getWalker().isSubQuery() ) {
 				// for a subquery, the alias to use depends on a few things (we
 				// already know this is not an overall SELECT):
 				//      1) if this FROM_ELEMENT represents a correlation to the
@@ -367,14 +367,29 @@
 					return propertyMapping.toColumns( tableAlias, path );
 				}
 			}
-			else {
-				String[] columns = propertyMapping.toColumns( path );
-				log.trace( "Using non-qualified column reference [" + path + " -> (" + ArrayHelper.toString( columns ) + ")]" );
-				return columns;
+			if ( isManipulationQuery() && isMultiTable() && inWhereClause() ) {
+				// the actual where-clause will end up being ripped out the update/delete and used in
+				// a select to populate the temp table, so its ok to use the table alias to qualify the table refs
+				// and safer to do so to protect from same-named columns
+				return propertyMapping.toColumns( tableAlias, path );
+
 			}
+			String[] columns = propertyMapping.toColumns( path );
+			log.trace( String.format( "Using non-qualified column reference [%s -> (%s)]", path, ArrayHelper.toString( columns ) ) );
+			return columns;
+
 		}
 	}
 
+	private boolean isManipulationQuery() {
+		return fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.UPDATE
+				|| fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.DELETE;
+	}
+
+	private boolean inWhereClause() {
+		return fromElement.getWalker().getCurrentTopLevelClauseType() == HqlSqlTokenTypes.WHERE;
+	}
+
 	private boolean isCorrelation() {
 		FromClause top = fromElement.getWalker().getFinalFromClause();
 		return fromElement.getFromClause() != fromElement.getWalker().getCurrentFromClause() &&

Modified: core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java
===================================================================
--- core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java	2012-06-01 17:26:52 UTC (rev 21036)
+++ core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java	2012-06-05 09:07:40 UTC (rev 21037)
@@ -33,6 +33,53 @@
 		return new FunctionalTestClassTestSuite( JoinedSubclassTest.class );
 	}
 
+	public void testHqlDeleteOnJoinedSubclass() {
+		Session s = openSession();
+		s.beginTransaction();
+		// syntax checking on the database...
+		s.createQuery( "delete from Employee" ).executeUpdate();
+		s.createQuery( "delete from Person" ).executeUpdate();
+		s.createQuery( "delete from Employee e" ).executeUpdate();
+		s.createQuery( "delete from Person p" ).executeUpdate();
+		s.createQuery( "delete from Employee where name like 'S%'" ).executeUpdate();
+		s.createQuery( "delete from Employee e where e.name like 'S%'" ).executeUpdate();
+		s.createQuery( "delete from Person where name like 'S%'" ).executeUpdate();
+		s.createQuery( "delete from Person p where p.name like 'S%'" ).executeUpdate();
+
+		// now the forms that actually fail from problem underlying HHH-1657
+		// which is limited to references to properties mapped to column names existing in both tables
+		// which is normally just the pks.  super critical ;)
+
+		s.createQuery( "delete from Employee where id = 1" ).executeUpdate();
+		s.createQuery( "delete from Employee e where e.id = 1" ).executeUpdate();
+		s.createQuery( "delete from Person where id = 1" ).executeUpdate();
+		s.createQuery( "delete from Person p where p.id = 1" ).executeUpdate();
+		s.getTransaction().commit();
+		s.close();
+	}
+
+	public void testHqlUpdateOnJoinedSubclass() {
+		Session s = openSession();
+		s.beginTransaction();
+		// syntax checking on the database...
+		s.createQuery( "update Employee set name = 'Some Other Name' where title like 'A%'" ).executeUpdate();
+		s.createQuery( "update Employee e set e.name = 'Some Other Name' where e.title like 'A%'" ).executeUpdate();
+		s.createQuery( "update Person set name = 'Some Other Name' where name like 'S%'" ).executeUpdate();
+		s.createQuery( "update Person p set p.name = 'Some Other Name' where p.name like 'S%'" ).executeUpdate();
+
+		// now the forms that actually fail from problem underlying HHH-1657
+		// which is limited to references to properties mapped to column names existing in both tables
+		// which is normally just the pks.  super critical ;)
+
+		s.createQuery( "update Employee set name = 'Some Other Name' where id = 1" ).executeUpdate();
+		s.createQuery( "update Employee e set e.name = 'Some Other Name' where e.id = 1" ).executeUpdate();
+		s.createQuery( "update Person set name = 'Some Other Name' where id = 1" ).executeUpdate();
+		s.createQuery( "update Person p set p.name = 'Some Other Name' where p.id = 1" ).executeUpdate();
+
+		s.getTransaction().commit();
+		s.close();
+	}
+
 	public void testJoinedSubclass() {
 		Session s = openSession();
 		Transaction t = s.beginTransaction();



More information about the hibernate-commits mailing list