[hibernate-commits] Hibernate SVN: r21119 - in core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate: impl and 3 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Tue May 14 14:01:28 EDT 2013
Author: brmeyer
Date: 2013-05-14 14:01:28 -0400 (Tue, 14 May 2013)
New Revision: 21119
Modified:
core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/Criteria.java
core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/impl/CriteriaImpl.java
core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/AbstractEntityJoinWalker.java
core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/JoinWalker.java
core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/OuterJoinableAssociation.java
core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/collection/BasicCollectionJoinWalker.java
core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/collection/OneToManyJoinWalker.java
core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java
core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/criteria/CriteriaQueryTranslator.java
Log:
HHH-2308 Adding predicates to the join condition using Criteria Query
Modified: core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/Criteria.java
===================================================================
--- core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/Criteria.java 2013-05-14 16:10:28 UTC (rev 21118)
+++ core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/Criteria.java 2013-05-14 18:01:28 UTC (rev 21119)
@@ -183,6 +183,24 @@
*/
public Criteria createAlias(String associationPath, String alias, int joinType) throws HibernateException;
+ /**
+ * Join an association using the specified join-type, assigning an alias
+ * to the joined association.
+ * <p/>
+ * The joinType is expected to be one of {@link #INNER_JOIN} (the default),
+ * {@link #FULL_JOIN}, or {@link #LEFT_JOIN}.
+ *
+ * @param associationPath A dot-seperated property path
+ * @param alias The alias to assign to the joined association (for later reference).
+ * @param joinType The type of join to use.
+ * @param withClause The criteria to be added to the join condition (<tt>ON</tt> clause)
+ *
+ * @return this (for method chaining)
+ *
+ * @throws HibernateException Indicates a problem creating the sub criteria
+ */
+ public Criteria createAlias(String associationPath, String alias, int joinType, Criterion withClause) throws HibernateException;
+
/**
* Create a new <tt>Criteria</tt>, "rooted" at the associated entity.
* <p/>
@@ -228,6 +246,21 @@
*/
public Criteria createCriteria(String associationPath, String alias, int joinType) throws HibernateException;
+ /**
+ * Create a new <tt>Criteria</tt>, "rooted" at the associated entity,
+ * assigning the given alias and using the specified join type.
+ *
+ * @param associationPath A dot-seperated property path
+ * @param alias The alias to assign to the joined association (for later reference).
+ * @param joinType The type of join to use.
+ * @param withClause The criteria to be added to the join condition (<tt>ON</tt> clause)
+ *
+ * @return the created "sub criteria"
+ *
+ * @throws HibernateException Indicates a problem creating the sub criteria
+ */
+ public Criteria createCriteria(String associationPath, String alias, int joinType, Criterion withClause) throws HibernateException;
+
/**
* Set a strategy for handling the query results. This determines the
* "shape" of the query result.
Modified: core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/impl/CriteriaImpl.java
===================================================================
--- core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/impl/CriteriaImpl.java 2013-05-14 16:10:28 UTC (rev 21118)
+++ core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/impl/CriteriaImpl.java 2013-05-14 18:01:28 UTC (rev 21119)
@@ -201,6 +201,12 @@
new Subcriteria( this, associationPath, alias, joinType );
return this;
}
+
+ public Criteria createAlias(String associationPath, String alias,
+ int joinType, Criterion withClause) {
+ new Subcriteria(this, associationPath, alias, joinType, withClause);
+ return this;
+ }
public Criteria createCriteria(String associationPath) {
return createCriteria( associationPath, INNER_JOIN );
@@ -217,6 +223,12 @@
public Criteria createCriteria(String associationPath, String alias, int joinType) {
return new Subcriteria( this, associationPath, alias, joinType );
}
+
+ public Criteria createCriteria(String associationPath, String alias,
+ int joinType, Criterion withClause) {
+ return new Subcriteria(this, associationPath, alias, joinType,
+ withClause);
+ }
public ResultTransformer getResultTransformer() {
return resultTransformer;
@@ -375,17 +387,23 @@
private LockMode lockMode;
private int joinType;
private boolean hasRestriction;
+ private Criterion withClause;
// Constructors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- private Subcriteria(Criteria parent, String path, String alias, int joinType) {
+ private Subcriteria(Criteria parent, String path, String alias, int joinType, Criterion withClause) {
this.alias = alias;
this.path = path;
this.parent = parent;
this.joinType = joinType;
+ this.withClause = withClause;
CriteriaImpl.this.subcriteriaList.add(this);
}
+
+ private Subcriteria(Criteria parent, String path, String alias, int joinType) {
+ this( parent, path, alias, joinType, null );
+ }
private Subcriteria(Criteria parent, String path, int joinType) {
this( parent, path, null, joinType );
@@ -432,6 +450,9 @@
public boolean hasRestriction(){
return hasRestriction;
}
+ public Criterion getWithClause() {
+ return this.withClause;
+ }
// Criteria impl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -455,6 +476,11 @@
new Subcriteria( this, associationPath, alias, joinType );
return this;
}
+
+ public Criteria createAlias(String associationPath, String alias, int joinType, Criterion withClause) throws HibernateException {
+ new Subcriteria( this, associationPath, alias, joinType, withClause );
+ return this;
+ }
public Criteria createCriteria(String associationPath) {
return createCriteria( associationPath, INNER_JOIN );
@@ -471,6 +497,10 @@
public Criteria createCriteria(String associationPath, String alias, int joinType) throws HibernateException {
return new Subcriteria( Subcriteria.this, associationPath, alias, joinType );
}
+
+ public Criteria createCriteria(String associationPath, String alias, int joinType, Criterion withClause) throws HibernateException {
+ return new Subcriteria( this, associationPath, alias, joinType, withClause );
+ }
public Criteria setCacheable(boolean cacheable) {
CriteriaImpl.this.setCacheable(cacheable);
@@ -498,8 +528,7 @@
return CriteriaImpl.this.uniqueResult();
}
- public Criteria setFetchMode(String associationPath, FetchMode mode)
- throws HibernateException {
+ public Criteria setFetchMode(String associationPath, FetchMode mode) {
CriteriaImpl.this.setFetchMode( StringHelper.qualify(path, associationPath), mode);
return this;
}
Modified: core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/AbstractEntityJoinWalker.java
===================================================================
--- core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/AbstractEntityJoinWalker.java 2013-05-14 16:10:28 UTC (rev 21118)
+++ core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/AbstractEntityJoinWalker.java 2013-05-14 18:01:28 UTC (rev 21119)
@@ -75,6 +75,7 @@
null,
alias,
JoinFragment.LEFT_OUTER_JOIN,
+ null,
getFactory(),
CollectionHelper.EMPTY_MAP
) );
Modified: core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/JoinWalker.java
===================================================================
--- core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/JoinWalker.java 2013-05-14 16:10:28 UTC (rev 21118)
+++ core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/JoinWalker.java 2013-05-14 18:01:28 UTC (rev 21119)
@@ -204,6 +204,10 @@
}
}
+
+ protected String getWithClause(String path) {
+ return "";
+ }
/**
* Add on association (one-to-one, many-to-one, or a collection) to a list
@@ -232,6 +236,7 @@
aliasedLhsColumns,
subalias,
joinType,
+ getWithClause(path),
hasRestriction( path ),
getFactory(),
enabledFilters
Modified: core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/OuterJoinableAssociation.java
===================================================================
--- core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/OuterJoinableAssociation.java 2013-05-14 16:10:28 UTC (rev 21118)
+++ core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/OuterJoinableAssociation.java 2013-05-14 18:01:28 UTC (rev 21119)
@@ -53,17 +53,20 @@
String[] lhsColumns,
String rhsAlias,
int joinType,
+ String withClause,
SessionFactoryImplementor factory,
Map enabledFilters)
throws MappingException {
- this(joinableType,lhsAlias,lhsColumns,rhsAlias,joinType,false,factory,enabledFilters);
+ this(joinableType,lhsAlias,lhsColumns,rhsAlias,joinType,withClause,false,factory,enabledFilters);
}
public OuterJoinableAssociation(
AssociationType joinableType,
String lhsAlias,
String[] lhsColumns,
String rhsAlias,
- int joinType, boolean hasRestriction,
+ int joinType,
+ String withClause,
+ boolean hasRestriction,
SessionFactoryImplementor factory,
Map enabledFilters)
throws MappingException {
@@ -74,7 +77,8 @@
this.joinType = joinType;
this.joinable = joinableType.getAssociatedJoinable(factory);
this.rhsColumns = JoinHelper.getRHSColumnNames(joinableType, factory);
- this.on = joinableType.getOnCondition(rhsAlias, factory, enabledFilters);
+ this.on = joinableType.getOnCondition(rhsAlias, factory, enabledFilters)
+ + ( withClause == null || withClause.trim().length() == 0 ? "" : " and ( " + withClause + " )" );
this.hasRestriction = hasRestriction;
this.enabledFilters = enabledFilters; // needed later for many-to-many/filter application
}
Modified: core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/collection/BasicCollectionJoinWalker.java
===================================================================
--- core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/collection/BasicCollectionJoinWalker.java 2013-05-14 16:10:28 UTC (rev 21118)
+++ core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/collection/BasicCollectionJoinWalker.java 2013-05-14 18:01:28 UTC (rev 21119)
@@ -76,7 +76,8 @@
null,
null,
alias,
- JoinFragment.LEFT_OUTER_JOIN,
+ JoinFragment.LEFT_OUTER_JOIN,
+ null,
getFactory(),
CollectionHelper.EMPTY_MAP
) );
Modified: core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/collection/OneToManyJoinWalker.java
===================================================================
--- core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/collection/OneToManyJoinWalker.java 2013-05-14 16:10:28 UTC (rev 21118)
+++ core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/collection/OneToManyJoinWalker.java 2013-05-14 18:01:28 UTC (rev 21119)
@@ -86,7 +86,8 @@
null,
null,
alias,
- JoinFragment.LEFT_OUTER_JOIN,
+ JoinFragment.LEFT_OUTER_JOIN,
+ null,
getFactory(),
CollectionHelper.EMPTY_MAP
) );
Modified: core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java
===================================================================
--- core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java 2013-05-14 16:10:28 UTC (rev 21118)
+++ core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java 2013-05-14 18:01:28 UTC (rev 21119)
@@ -210,6 +210,10 @@
}
return super.generateTableAlias( n + translator.getSQLAliasCount(), path, joinable );
}
+
+ protected String getWithClause(String path) {
+ return translator.getWithClause(path);
+ }
protected String generateRootAlias(String tableName) {
return CriteriaQueryTranslator.ROOT_SQL_ALIAS;
Modified: core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/criteria/CriteriaQueryTranslator.java
===================================================================
--- core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/criteria/CriteriaQueryTranslator.java 2013-05-14 16:10:28 UTC (rev 21118)
+++ core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10746/core/src/main/java/org/hibernate/loader/criteria/CriteriaQueryTranslator.java 2013-05-14 18:01:28 UTC (rev 21119)
@@ -42,6 +42,7 @@
import org.hibernate.MappingException;
import org.hibernate.QueryException;
import org.hibernate.criterion.CriteriaQuery;
+import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projection;
import org.hibernate.engine.QueryParameters;
import org.hibernate.engine.RowSelection;
@@ -78,6 +79,8 @@
private final Map aliasCriteriaMap = new HashMap();
private final Map associationPathCriteriaMap = new LinkedHashMap();
private final Map associationPathJoinTypesMap = new LinkedHashMap();
+
+ private final Map withClauseMap = new HashMap();
private final SessionFactoryImplementor sessionFactory;
@@ -170,6 +173,10 @@
// TODO : not so sure this is needed...
throw new QueryException( "duplicate association path: " + wholeAssociationPath );
}
+ if (crit.getWithClause() != null) {
+ this.withClauseMap.put(wholeAssociationPath,
+ crit.getWithClause());
+ }
}
}
@@ -267,20 +274,6 @@
}
public QueryParameters getQueryParameters() {
- List values = new ArrayList();
- List types = new ArrayList();
- Iterator iter = rootCriteria.iterateExpressionEntries();
- while ( iter.hasNext() ) {
- CriteriaImpl.CriterionEntry ce = ( CriteriaImpl.CriterionEntry ) iter.next();
- TypedValue[] tv = ce.getCriterion().getTypedValues( ce.getCriteria(), this );
- for ( int i = 0; i < tv.length; i++ ) {
- values.add( tv[i].getValue() );
- types.add( tv[i].getType() );
- }
- }
- Object[] valueArray = values.toArray();
- Type[] typeArray = ArrayHelper.toTypeArray( types );
-
RowSelection selection = new RowSelection();
selection.setFirstRow( rootCriteria.getFirstResult() );
selection.setMaxRows( rootCriteria.getMaxResults() );
@@ -288,12 +281,14 @@
selection.setFetchSize( rootCriteria.getFetchSize() );
Map lockModes = new HashMap();
- iter = rootCriteria.getLockModes().entrySet().iterator();
+ Iterator iter = rootCriteria.getLockModes().entrySet().iterator();
while ( iter.hasNext() ) {
Map.Entry me = ( Map.Entry ) iter.next();
final Criteria subcriteria = getAliasedCriteria( ( String ) me.getKey() );
lockModes.put( getSQLAlias( subcriteria ), me.getValue() );
}
+ List values = new ArrayList();
+ List types = new ArrayList();
iter = rootCriteria.iterateSubcriteria();
while ( iter.hasNext() ) {
CriteriaImpl.Subcriteria subcriteria = ( CriteriaImpl.Subcriteria ) iter.next();
@@ -301,8 +296,36 @@
if ( lm != null ) {
lockModes.put( getSQLAlias( subcriteria ), lm );
}
+ if (subcriteria.getWithClause() != null) {
+ TypedValue[] tv = subcriteria.getWithClause().getTypedValues(
+ subcriteria, this);
+ for (int i = 0; i < tv.length; i++) {
+ values.add(tv[i].getValue());
+ types.add(tv[i].getType());
+ }
+ }
}
+
+ // Type and value gathering for the WHERE clause needs to come AFTER
+ // lock mode gathering,
+ // because the lock mode gathering loop now contains join clauses which
+ // can contain
+ // parameter bindings (as in the HQL WITH clause).
+ iter = rootCriteria.iterateExpressionEntries();
+ while (iter.hasNext()) {
+ CriteriaImpl.CriterionEntry ce = (CriteriaImpl.CriterionEntry) iter
+ .next();
+ TypedValue[] tv = ce.getCriterion().getTypedValues(
+ ce.getCriteria(), this);
+ for (int i = 0; i < tv.length; i++) {
+ values.add(tv[i].getValue());
+ types.add(tv[i].getType());
+ }
+ }
+ Object[] valueArray = values.toArray();
+ Type[] typeArray = ArrayHelper.toTypeArray(types);
+
return new QueryParameters(
typeArray,
valueArray,
@@ -570,6 +593,11 @@
}
return getSQLAlias( criteria );
}
+
+ public String getWithClause(String path) {
+ final Criterion crit = (Criterion) this.withClauseMap.get(path);
+ return crit == null ? null : crit.toSqlString(getCriteria(path), this);
+ }
public String getPropertyName(String propertyName) {
if ( propertyName.indexOf( '.' ) > 0 ) {
More information about the hibernate-commits
mailing list