HHH-1088 : Add support for projections using composite keys and components
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19110 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
186e9fc115
commit
3ad551a88a
|
@ -31,7 +31,7 @@ import org.hibernate.type.Type;
|
||||||
/**
|
/**
|
||||||
* @author Gavin King
|
* @author Gavin King
|
||||||
*/
|
*/
|
||||||
public class AliasedProjection implements Projection {
|
public class AliasedProjection implements EnhancedProjection {
|
||||||
|
|
||||||
private final Projection projection;
|
private final Projection projection;
|
||||||
private final String alias;
|
private final String alias;
|
||||||
|
@ -64,7 +64,13 @@ public class AliasedProjection implements Projection {
|
||||||
return projection.getColumnAliases(loc);
|
return projection.getColumnAliases(loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type[] getTypes(String alias, Criteria criteria, CriteriaQuery criteriaQuery)
|
public String[] getColumnAliases(int loc, Criteria criteria, CriteriaQuery criteriaQuery) {
|
||||||
|
return projection instanceof EnhancedProjection ?
|
||||||
|
( ( EnhancedProjection ) projection ).getColumnAliases( loc, criteria, criteriaQuery ) :
|
||||||
|
getColumnAliases( loc );
|
||||||
|
}
|
||||||
|
|
||||||
|
public Type[] getTypes(String alias, Criteria criteria, CriteriaQuery criteriaQuery)
|
||||||
throws HibernateException {
|
throws HibernateException {
|
||||||
return this.alias.equals(alias) ?
|
return this.alias.equals(alias) ?
|
||||||
getTypes(criteria, criteriaQuery) :
|
getTypes(criteria, criteriaQuery) :
|
||||||
|
@ -77,6 +83,12 @@ public class AliasedProjection implements Projection {
|
||||||
null;
|
null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String[] getColumnAliases(String alias, int loc, Criteria criteria, CriteriaQuery criteriaQuery) {
|
||||||
|
return this.alias.equals(alias) ?
|
||||||
|
getColumnAliases( loc, criteria, criteriaQuery ) :
|
||||||
|
null;
|
||||||
|
}
|
||||||
|
|
||||||
public String[] getAliases() {
|
public String[] getAliases() {
|
||||||
return new String[]{ alias };
|
return new String[]{ alias };
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ import org.hibernate.type.Type;
|
||||||
/**
|
/**
|
||||||
* @author Gavin King
|
* @author Gavin King
|
||||||
*/
|
*/
|
||||||
public class Distinct implements Projection {
|
public class Distinct implements EnhancedProjection {
|
||||||
|
|
||||||
private final Projection projection;
|
private final Projection projection;
|
||||||
|
|
||||||
|
@ -63,10 +63,22 @@ public class Distinct implements Projection {
|
||||||
return projection.getColumnAliases(loc);
|
return projection.getColumnAliases(loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String[] getColumnAliases(int loc, Criteria criteria, CriteriaQuery criteriaQuery) {
|
||||||
|
return projection instanceof EnhancedProjection ?
|
||||||
|
( ( EnhancedProjection ) projection ).getColumnAliases( loc, criteria, criteriaQuery ) :
|
||||||
|
getColumnAliases( loc );
|
||||||
|
}
|
||||||
|
|
||||||
public String[] getColumnAliases(String alias, int loc) {
|
public String[] getColumnAliases(String alias, int loc) {
|
||||||
return projection.getColumnAliases(alias, loc);
|
return projection.getColumnAliases(alias, loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String[] getColumnAliases(String alias, int loc, Criteria criteria, CriteriaQuery criteriaQuery) {
|
||||||
|
return projection instanceof EnhancedProjection ?
|
||||||
|
( ( EnhancedProjection ) projection ).getColumnAliases( alias, loc, criteria, criteriaQuery ) :
|
||||||
|
getColumnAliases( alias, loc );
|
||||||
|
}
|
||||||
|
|
||||||
public String[] getAliases() {
|
public String[] getAliases() {
|
||||||
return projection.getAliases();
|
return projection.getAliases();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,70 @@
|
||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
|
||||||
|
* indicated by the @author tags or express copyright attribution
|
||||||
|
* statements applied by the authors. All third-party contributions are
|
||||||
|
* distributed under license by Red Hat Middleware LLC.
|
||||||
|
*
|
||||||
|
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||||
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
* Lesser General Public License, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this distribution; if not, write to:
|
||||||
|
* Free Software Foundation, Inc.
|
||||||
|
* 51 Franklin Street, Fifth Floor
|
||||||
|
* Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.hibernate.criterion;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import org.hibernate.Criteria;
|
||||||
|
import org.hibernate.HibernateException;
|
||||||
|
import org.hibernate.type.Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An "enhanced" Projection for a {@link Criteria} query.
|
||||||
|
*
|
||||||
|
* @author Gail Badner
|
||||||
|
* @see Projection
|
||||||
|
* @see Criteria
|
||||||
|
*/
|
||||||
|
public interface EnhancedProjection extends Projection {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the SQL column aliases used by this projection for the columns it writes for inclusion into the
|
||||||
|
* <tt>SELECT</tt> clause ({@link #toSqlString}. Hibernate always uses column aliases to extract data from the
|
||||||
|
* JDBC {@link java.sql.ResultSet}, so it is important that these be implemented correctly in order for
|
||||||
|
* Hibernate to be able to extract these val;ues correctly.
|
||||||
|
*
|
||||||
|
* @param position Just as in {@link #toSqlString}, represents the number of <b>columns</b> rendered
|
||||||
|
* prior to this projection.
|
||||||
|
* @param criteria The local criteria to which this project is attached (for resolution).
|
||||||
|
* @param criteriaQuery The overall criteria query instance.
|
||||||
|
* @return The columns aliases.
|
||||||
|
*/
|
||||||
|
public String[] getColumnAliases(int position, Criteria criteria, CriteriaQuery criteriaQuery);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the SQL column aliases used by this projection for the columns it writes for inclusion into the
|
||||||
|
* <tt>SELECT</tt> clause ({@link #toSqlString} <i>for a particular criteria-level alias</i>.
|
||||||
|
*
|
||||||
|
* @param alias The criteria-level alias
|
||||||
|
* @param position Just as in {@link #toSqlString}, represents the number of <b>columns</b> rendered
|
||||||
|
* prior to this projection.
|
||||||
|
* @param criteria The local criteria to which this project is attached (for resolution).
|
||||||
|
* @param criteriaQuery The overall criteria query instance.
|
||||||
|
* @return The columns aliases pertaining to a particular criteria-level alias; expected to return null if
|
||||||
|
* this projection does not understand this alias.
|
||||||
|
*/
|
||||||
|
public String[] getColumnAliases(String alias, int position, Criteria criteria, CriteriaQuery criteriaQuery);
|
||||||
|
}
|
|
@ -63,6 +63,8 @@ public class IdentifierProjection extends SimpleProjection {
|
||||||
.append(" as y")
|
.append(" as y")
|
||||||
.append(position + i)
|
.append(position + i)
|
||||||
.append('_');
|
.append('_');
|
||||||
|
if (i < cols.length -1)
|
||||||
|
buf.append(", ");
|
||||||
}
|
}
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,7 @@ import org.hibernate.util.ArrayHelper;
|
||||||
/**
|
/**
|
||||||
* @author Gavin King
|
* @author Gavin King
|
||||||
*/
|
*/
|
||||||
public class ProjectionList implements Projection {
|
public class ProjectionList implements EnhancedProjection {
|
||||||
|
|
||||||
private List elements = new ArrayList();
|
private List elements = new ArrayList();
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ public class ProjectionList implements Projection {
|
||||||
for ( int i=0; i<getLength(); i++ ) {
|
for ( int i=0; i<getLength(); i++ ) {
|
||||||
Projection proj = getProjection(i);
|
Projection proj = getProjection(i);
|
||||||
buf.append( proj.toSqlString(criteria, loc, criteriaQuery) );
|
buf.append( proj.toSqlString(criteria, loc, criteriaQuery) );
|
||||||
loc += proj.getColumnAliases(loc).length;
|
loc += getColumnAliases(loc, criteria, criteriaQuery, proj ).length;
|
||||||
if ( i<elements.size()-1 ) buf.append(", ");
|
if ( i<elements.size()-1 ) buf.append(", ");
|
||||||
}
|
}
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
|
@ -100,6 +100,16 @@ public class ProjectionList implements Projection {
|
||||||
return ArrayHelper.toStringArray(result);
|
return ArrayHelper.toStringArray(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String[] getColumnAliases(int loc, Criteria criteria, CriteriaQuery criteriaQuery) {
|
||||||
|
List result = new ArrayList( getLength() );
|
||||||
|
for ( int i=0; i<getLength(); i++ ) {
|
||||||
|
String[] colAliases = getColumnAliases( loc, criteria, criteriaQuery, getProjection( i ) );
|
||||||
|
ArrayHelper.addAll(result, colAliases);
|
||||||
|
loc+=colAliases.length;
|
||||||
|
}
|
||||||
|
return ArrayHelper.toStringArray(result);
|
||||||
|
}
|
||||||
|
|
||||||
public String[] getColumnAliases(String alias, int loc) {
|
public String[] getColumnAliases(String alias, int loc) {
|
||||||
for ( int i=0; i<getLength(); i++ ) {
|
for ( int i=0; i<getLength(); i++ ) {
|
||||||
String[] result = getProjection(i).getColumnAliases(alias, loc);
|
String[] result = getProjection(i).getColumnAliases(alias, loc);
|
||||||
|
@ -109,6 +119,27 @@ public class ProjectionList implements Projection {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String[] getColumnAliases(String alias, int loc, Criteria criteria, CriteriaQuery criteriaQuery) {
|
||||||
|
for ( int i=0; i<getLength(); i++ ) {
|
||||||
|
String[] result = getColumnAliases( alias, loc, criteria, criteriaQuery, getProjection(i) );
|
||||||
|
if (result!=null) return result;
|
||||||
|
loc += getColumnAliases( loc, criteria, criteriaQuery, getProjection( i ) ).length;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String[] getColumnAliases(int loc, Criteria criteria, CriteriaQuery criteriaQuery, Projection projection) {
|
||||||
|
return projection instanceof EnhancedProjection ?
|
||||||
|
( ( EnhancedProjection ) projection ).getColumnAliases( loc, criteria, criteriaQuery ) :
|
||||||
|
projection.getColumnAliases( loc );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String[] getColumnAliases(String alias, int loc, Criteria criteria, CriteriaQuery criteriaQuery, Projection projection) {
|
||||||
|
return projection instanceof EnhancedProjection ?
|
||||||
|
( ( EnhancedProjection ) projection ).getColumnAliases( alias, loc, criteria, criteriaQuery ) :
|
||||||
|
projection.getColumnAliases( alias, loc );
|
||||||
|
}
|
||||||
|
|
||||||
public Type[] getTypes(String alias, Criteria criteria, CriteriaQuery criteriaQuery) {
|
public Type[] getTypes(String alias, Criteria criteria, CriteriaQuery criteriaQuery) {
|
||||||
for ( int i=0; i<getLength(); i++ ) {
|
for ( int i=0; i<getLength(); i++ ) {
|
||||||
Type[] result = getProjection(i).getTypes(alias, criteria, criteriaQuery);
|
Type[] result = getProjection(i).getTypes(alias, criteria, criteriaQuery);
|
||||||
|
@ -134,7 +165,7 @@ public class ProjectionList implements Projection {
|
||||||
public int getLength() {
|
public int getLength() {
|
||||||
return elements.size();
|
return elements.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return elements.toString();
|
return elements.toString();
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,12 +61,17 @@ public class PropertyProjection extends SimpleProjection {
|
||||||
|
|
||||||
public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery)
|
public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery)
|
||||||
throws HibernateException {
|
throws HibernateException {
|
||||||
return new StringBuffer()
|
StringBuffer buf = new StringBuffer();
|
||||||
.append( criteriaQuery.getColumn(criteria, propertyName) )
|
String[] cols = criteriaQuery.getColumnsUsingProjection( criteria, propertyName );
|
||||||
.append(" as y")
|
for ( int i=0; i<cols.length; i++ ) {
|
||||||
.append(position)
|
buf.append( cols[i] )
|
||||||
.append('_')
|
.append(" as y")
|
||||||
.toString();
|
.append(position + i)
|
||||||
|
.append('_');
|
||||||
|
if (i < cols.length -1)
|
||||||
|
buf.append(", ");
|
||||||
|
}
|
||||||
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isGrouped() {
|
public boolean isGrouped() {
|
||||||
|
|
|
@ -24,6 +24,8 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.criterion;
|
package org.hibernate.criterion;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
import org.hibernate.Criteria;
|
import org.hibernate.Criteria;
|
||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
import org.hibernate.type.Type;
|
import org.hibernate.type.Type;
|
||||||
|
@ -33,7 +35,7 @@ import org.hibernate.type.Type;
|
||||||
* A single-column projection that may be aliased
|
* A single-column projection that may be aliased
|
||||||
* @author Gavin King
|
* @author Gavin King
|
||||||
*/
|
*/
|
||||||
public abstract class SimpleProjection implements Projection {
|
public abstract class SimpleProjection implements EnhancedProjection {
|
||||||
|
|
||||||
public Projection as(String alias) {
|
public Projection as(String alias) {
|
||||||
return Projections.alias(this, alias);
|
return Projections.alias(this, alias);
|
||||||
|
@ -42,7 +44,11 @@ public abstract class SimpleProjection implements Projection {
|
||||||
public String[] getColumnAliases(String alias, int loc) {
|
public String[] getColumnAliases(String alias, int loc) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String[] getColumnAliases(String alias, int loc, Criteria criteria, CriteriaQuery criteriaQuery) {
|
||||||
|
return getColumnAliases( alias, loc );
|
||||||
|
}
|
||||||
|
|
||||||
public Type[] getTypes(String alias, Criteria criteria, CriteriaQuery criteriaQuery)
|
public Type[] getTypes(String alias, Criteria criteria, CriteriaQuery criteriaQuery)
|
||||||
throws HibernateException {
|
throws HibernateException {
|
||||||
return null;
|
return null;
|
||||||
|
@ -51,7 +57,26 @@ public abstract class SimpleProjection implements Projection {
|
||||||
public String[] getColumnAliases(int loc) {
|
public String[] getColumnAliases(int loc) {
|
||||||
return new String[] { "y" + loc + "_" };
|
return new String[] { "y" + loc + "_" };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getColumnCount(Criteria criteria, CriteriaQuery criteriaQuery) {
|
||||||
|
Type types[] = getTypes( criteria, criteriaQuery );
|
||||||
|
int count = 0;
|
||||||
|
for ( int i=0; i<types.length; i++ ) {
|
||||||
|
count += types[ i ].getColumnSpan( criteriaQuery.getFactory() );
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getColumnAliases(int loc, Criteria criteria, CriteriaQuery criteriaQuery) {
|
||||||
|
int numColumns = getColumnCount( criteria, criteriaQuery );
|
||||||
|
String[] aliases = new String[ numColumns ];
|
||||||
|
for (int i = 0; i < numColumns; i++) {
|
||||||
|
aliases[i] = "y" + loc + "_";
|
||||||
|
loc++;
|
||||||
|
}
|
||||||
|
return aliases;
|
||||||
|
}
|
||||||
|
|
||||||
public String[] getAliases() {
|
public String[] getAliases() {
|
||||||
return new String[1];
|
return new String[1];
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,7 @@ import org.hibernate.persister.entity.OuterJoinLoadable;
|
||||||
import org.hibernate.persister.entity.Lockable;
|
import org.hibernate.persister.entity.Lockable;
|
||||||
import org.hibernate.transform.ResultTransformer;
|
import org.hibernate.transform.ResultTransformer;
|
||||||
import org.hibernate.type.Type;
|
import org.hibernate.type.Type;
|
||||||
|
import org.hibernate.util.ArrayHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A <tt>Loader</tt> for <tt>Criteria</tt> queries. Note that criteria queries are
|
* A <tt>Loader</tt> for <tt>Criteria</tt> queries. Note that criteria queries are
|
||||||
|
@ -127,8 +128,16 @@ public class CriteriaLoader extends OuterJoinLoader {
|
||||||
Type[] types = translator.getProjectedTypes();
|
Type[] types = translator.getProjectedTypes();
|
||||||
result = new Object[types.length];
|
result = new Object[types.length];
|
||||||
String[] columnAliases = translator.getProjectedColumnAliases();
|
String[] columnAliases = translator.getProjectedColumnAliases();
|
||||||
for ( int i=0; i<result.length; i++ ) {
|
for ( int i=0, pos=0; i<result.length; i++ ) {
|
||||||
result[i] = types[i].nullSafeGet(rs, columnAliases[i], session, null);
|
int numColumns = types[i].getColumnSpan( session.getFactory() );
|
||||||
|
if ( numColumns > 1 ) {
|
||||||
|
String[] typeColumnAliases = ArrayHelper.slice( columnAliases, pos, numColumns );
|
||||||
|
result[i] = types[i].nullSafeGet(rs, typeColumnAliases, session, null);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result[i] = types[i].nullSafeGet(rs, columnAliases[pos], session, null);
|
||||||
|
}
|
||||||
|
pos += numColumns;
|
||||||
}
|
}
|
||||||
aliases = translator.getProjectedAliases();
|
aliases = translator.getProjectedAliases();
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@ import org.hibernate.LockMode;
|
||||||
import org.hibernate.MappingException;
|
import org.hibernate.MappingException;
|
||||||
import org.hibernate.QueryException;
|
import org.hibernate.QueryException;
|
||||||
import org.hibernate.LockOptions;
|
import org.hibernate.LockOptions;
|
||||||
|
import org.hibernate.criterion.EnhancedProjection;
|
||||||
import org.hibernate.hql.ast.util.SessionFactoryHelper;
|
import org.hibernate.hql.ast.util.SessionFactoryHelper;
|
||||||
import org.hibernate.criterion.CriteriaQuery;
|
import org.hibernate.criterion.CriteriaQuery;
|
||||||
import org.hibernate.criterion.Criterion;
|
import org.hibernate.criterion.Criterion;
|
||||||
|
@ -362,7 +363,9 @@ public class CriteriaQueryTranslator implements CriteriaQuery {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] getProjectedColumnAliases() {
|
public String[] getProjectedColumnAliases() {
|
||||||
return rootCriteria.getProjection().getColumnAliases( 0 );
|
return rootCriteria.getProjection() instanceof EnhancedProjection ?
|
||||||
|
( ( EnhancedProjection ) rootCriteria.getProjection() ).getColumnAliases( 0, rootCriteria, this ) :
|
||||||
|
rootCriteria.getProjection().getColumnAliases( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] getProjectedAliases() {
|
public String[] getProjectedAliases() {
|
||||||
|
@ -426,10 +429,13 @@ public class CriteriaQueryTranslator implements CriteriaQuery {
|
||||||
|
|
||||||
//first look for a reference to a projection alias
|
//first look for a reference to a projection alias
|
||||||
final Projection projection = rootCriteria.getProjection();
|
final Projection projection = rootCriteria.getProjection();
|
||||||
String[] projectionColumns = projection == null ?
|
String[] projectionColumns = null;
|
||||||
null :
|
if ( projection != null ) {
|
||||||
projection.getColumnAliases( propertyName, 0 );
|
projectionColumns = ( projection instanceof EnhancedProjection ?
|
||||||
|
( ( EnhancedProjection ) projection ).getColumnAliases( propertyName, 0, rootCriteria, this ) :
|
||||||
|
projection.getColumnAliases( propertyName, 0 )
|
||||||
|
);
|
||||||
|
}
|
||||||
if ( projectionColumns == null ) {
|
if ( projectionColumns == null ) {
|
||||||
//it does not refer to an alias of a projection,
|
//it does not refer to an alias of a projection,
|
||||||
//look for a property
|
//look for a property
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2010, Red Hat Middleware LLC or third-party contributors as
|
||||||
|
* indicated by the @author tags or express copyright attribution
|
||||||
|
* statements applied by the authors. All third-party contributions are
|
||||||
|
* distributed under license by Red Hat Middleware LLC.
|
||||||
|
*
|
||||||
|
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||||
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
* Lesser General Public License, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this distribution; if not, write to:
|
||||||
|
* Free Software Foundation, Inc.
|
||||||
|
* 51 Franklin Street, Fifth Floor
|
||||||
|
* Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.hibernate.test.criteria;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Gail Badner
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class CityState {
|
||||||
|
private String city;
|
||||||
|
private String state;
|
||||||
|
|
||||||
|
public CityState() {}
|
||||||
|
|
||||||
|
public CityState(String city, String state) {
|
||||||
|
this.city = city;
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCity() {
|
||||||
|
return city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCity(String city) {
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getState() {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setState(String state) {
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,12 +1,17 @@
|
||||||
//$Id: Course.java 5686 2005-02-12 07:27:32Z steveebersole $
|
//$Id: Course.java 5686 2005-02-12 07:27:32Z steveebersole $
|
||||||
package org.hibernate.test.criteria;
|
package org.hibernate.test.criteria;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Gavin King
|
* @author Gavin King
|
||||||
*/
|
*/
|
||||||
public class Course {
|
public class Course {
|
||||||
private String courseCode;
|
private String courseCode;
|
||||||
private String description;
|
private String description;
|
||||||
|
private Set courseMeetings = new HashSet();
|
||||||
|
|
||||||
public String getCourseCode() {
|
public String getCourseCode() {
|
||||||
return courseCode;
|
return courseCode;
|
||||||
}
|
}
|
||||||
|
@ -19,4 +24,10 @@ public class Course {
|
||||||
public void setDescription(String description) {
|
public void setDescription(String description) {
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
public Set getCourseMeetings() {
|
||||||
|
return courseMeetings;
|
||||||
|
}
|
||||||
|
public void setCourseMeetings(Set courseMeetings) {
|
||||||
|
this.courseMeetings = courseMeetings;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package org.hibernate.test.criteria;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Gail Badner
|
||||||
|
*/
|
||||||
|
public class CourseMeeting {
|
||||||
|
private CourseMeetingId id;
|
||||||
|
private Course course;
|
||||||
|
|
||||||
|
public CourseMeeting() {}
|
||||||
|
|
||||||
|
public CourseMeeting(Course course, String day, int period, String location) {
|
||||||
|
this.id = new CourseMeetingId( course, day, period, location );
|
||||||
|
this.course = course;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CourseMeetingId getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
public void setId(CourseMeetingId id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
public Course getCourse() {
|
||||||
|
return course;
|
||||||
|
}
|
||||||
|
public void setCourse(Course course) {
|
||||||
|
this.course = course;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package org.hibernate.test.criteria;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Gail Badner
|
||||||
|
*/
|
||||||
|
public class CourseMeetingId implements Serializable {
|
||||||
|
private String courseCode;
|
||||||
|
private String day;
|
||||||
|
private int period;
|
||||||
|
private String location;
|
||||||
|
|
||||||
|
public CourseMeetingId() {}
|
||||||
|
|
||||||
|
public CourseMeetingId(Course course, String day, int period, String location) {
|
||||||
|
this.courseCode = course.getCourseCode();
|
||||||
|
this.day = day;
|
||||||
|
this.period = period;
|
||||||
|
this.location = location;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCourseCode() {
|
||||||
|
return courseCode;
|
||||||
|
}
|
||||||
|
public void setCourseCode(String courseCode) {
|
||||||
|
this.courseCode = courseCode;
|
||||||
|
}
|
||||||
|
public String getDay() {
|
||||||
|
return day;
|
||||||
|
}
|
||||||
|
public void setDay(String day) {
|
||||||
|
this.day = day;
|
||||||
|
}
|
||||||
|
public int getPeriod() {
|
||||||
|
return period;
|
||||||
|
}
|
||||||
|
public void setPeriod(int period) {
|
||||||
|
this.period = period;
|
||||||
|
}
|
||||||
|
public String getLocation() {
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
public void setLocation(String location) {
|
||||||
|
this.location = location;
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,6 +19,7 @@ import org.hibernate.criterion.Example;
|
||||||
import org.hibernate.criterion.MatchMode;
|
import org.hibernate.criterion.MatchMode;
|
||||||
import org.hibernate.criterion.Order;
|
import org.hibernate.criterion.Order;
|
||||||
import org.hibernate.criterion.Projection;
|
import org.hibernate.criterion.Projection;
|
||||||
|
import org.hibernate.criterion.ProjectionList;
|
||||||
import org.hibernate.criterion.Projections;
|
import org.hibernate.criterion.Projections;
|
||||||
import org.hibernate.criterion.Property;
|
import org.hibernate.criterion.Property;
|
||||||
import org.hibernate.criterion.Restrictions;
|
import org.hibernate.criterion.Restrictions;
|
||||||
|
@ -742,6 +743,152 @@ public class CriteriaQueryTest extends FunctionalTestCase {
|
||||||
s.close();
|
s.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testProjectedEmbeddedCompositeId() {
|
||||||
|
Session s = openSession();
|
||||||
|
Transaction t = s.beginTransaction();
|
||||||
|
|
||||||
|
Course course = new Course();
|
||||||
|
course.setCourseCode("HIB");
|
||||||
|
course.setDescription("Hibernate Training");
|
||||||
|
s.save(course);
|
||||||
|
|
||||||
|
Student gavin = new Student();
|
||||||
|
gavin.setName("Gavin King");
|
||||||
|
gavin.setStudentNumber(667);
|
||||||
|
s.save(gavin);
|
||||||
|
|
||||||
|
Student xam = new Student();
|
||||||
|
xam.setName("Max Rydahl Andersen");
|
||||||
|
xam.setStudentNumber(101);
|
||||||
|
s.save(xam);
|
||||||
|
|
||||||
|
Enrolment enrolment = new Enrolment();
|
||||||
|
enrolment.setCourse(course);
|
||||||
|
enrolment.setCourseCode(course.getCourseCode());
|
||||||
|
enrolment.setSemester((short) 1);
|
||||||
|
enrolment.setYear((short) 1999);
|
||||||
|
enrolment.setStudent(xam);
|
||||||
|
enrolment.setStudentNumber(xam.getStudentNumber());
|
||||||
|
xam.getEnrolments().add(enrolment);
|
||||||
|
s.save(enrolment);
|
||||||
|
|
||||||
|
enrolment = new Enrolment();
|
||||||
|
enrolment.setCourse(course);
|
||||||
|
enrolment.setCourseCode(course.getCourseCode());
|
||||||
|
enrolment.setSemester((short) 3);
|
||||||
|
enrolment.setYear((short) 1998);
|
||||||
|
enrolment.setStudent(gavin);
|
||||||
|
enrolment.setStudentNumber(gavin.getStudentNumber());
|
||||||
|
gavin.getEnrolments().add(enrolment);
|
||||||
|
s.save(enrolment);
|
||||||
|
|
||||||
|
s.flush();
|
||||||
|
|
||||||
|
List enrolments = ( List ) s.createCriteria( Enrolment.class).setProjection( Projections.id() ).list();
|
||||||
|
t.rollback();
|
||||||
|
s.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testProjectedCompositeId() {
|
||||||
|
Session s = openSession();
|
||||||
|
Transaction t = s.beginTransaction();
|
||||||
|
|
||||||
|
Course course = new Course();
|
||||||
|
course.setCourseCode("HIB");
|
||||||
|
course.setDescription("Hibernate Training");
|
||||||
|
course.getCourseMeetings().add( new CourseMeeting( course, "Monday", 1, "1313 Mockingbird Lane" ) );
|
||||||
|
s.save(course);
|
||||||
|
s.flush();
|
||||||
|
|
||||||
|
List data = ( List ) s.createCriteria( CourseMeeting.class).setProjection( Projections.id() ).list();
|
||||||
|
t.rollback();
|
||||||
|
s.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testProjectedComponent() {
|
||||||
|
Session s = openSession();
|
||||||
|
Transaction t = s.beginTransaction();
|
||||||
|
|
||||||
|
Student gaith = new Student();
|
||||||
|
gaith.setName("Gaith Bell");
|
||||||
|
gaith.setStudentNumber(123);
|
||||||
|
gaith.setCityState( new CityState( "Chicago", "Illinois" ) );
|
||||||
|
s.save( gaith );
|
||||||
|
s.flush();
|
||||||
|
|
||||||
|
List cityStates = ( List ) s.createCriteria( Student.class).setProjection( Projections.property( "cityState" )).list();
|
||||||
|
t.rollback();
|
||||||
|
s.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testProjectedListIncludesComponent() {
|
||||||
|
Session s = openSession();
|
||||||
|
Transaction t = s.beginTransaction();
|
||||||
|
|
||||||
|
Student gaith = new Student();
|
||||||
|
gaith.setName("Gaith Bell");
|
||||||
|
gaith.setStudentNumber(123);
|
||||||
|
gaith.setCityState( new CityState( "Chicago", "Illinois" ) );
|
||||||
|
s.save(gaith);
|
||||||
|
s.flush();
|
||||||
|
List data = ( List ) s.createCriteria( Student.class)
|
||||||
|
.setProjection( Projections.projectionList()
|
||||||
|
.add( Projections.property( "cityState" ) )
|
||||||
|
.add( Projections.property("name") ) )
|
||||||
|
.list();
|
||||||
|
t.rollback();
|
||||||
|
s.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testProjectedListIncludesEmbeddedCompositeId() {
|
||||||
|
Session s = openSession();
|
||||||
|
Transaction t = s.beginTransaction();
|
||||||
|
|
||||||
|
Course course = new Course();
|
||||||
|
course.setCourseCode("HIB");
|
||||||
|
course.setDescription("Hibernate Training");
|
||||||
|
s.save(course);
|
||||||
|
|
||||||
|
Student gavin = new Student();
|
||||||
|
gavin.setName("Gavin King");
|
||||||
|
gavin.setStudentNumber(667);
|
||||||
|
s.save(gavin);
|
||||||
|
|
||||||
|
Student xam = new Student();
|
||||||
|
xam.setName("Max Rydahl Andersen");
|
||||||
|
xam.setStudentNumber(101);
|
||||||
|
s.save(xam);
|
||||||
|
|
||||||
|
Enrolment enrolment = new Enrolment();
|
||||||
|
enrolment.setCourse(course);
|
||||||
|
enrolment.setCourseCode(course.getCourseCode());
|
||||||
|
enrolment.setSemester((short) 1);
|
||||||
|
enrolment.setYear((short) 1999);
|
||||||
|
enrolment.setStudent(xam);
|
||||||
|
enrolment.setStudentNumber(xam.getStudentNumber());
|
||||||
|
xam.getEnrolments().add(enrolment);
|
||||||
|
s.save(enrolment);
|
||||||
|
|
||||||
|
enrolment = new Enrolment();
|
||||||
|
enrolment.setCourse(course);
|
||||||
|
enrolment.setCourseCode(course.getCourseCode());
|
||||||
|
enrolment.setSemester((short) 3);
|
||||||
|
enrolment.setYear((short) 1998);
|
||||||
|
enrolment.setStudent(gavin);
|
||||||
|
enrolment.setStudentNumber(gavin.getStudentNumber());
|
||||||
|
gavin.getEnrolments().add(enrolment);
|
||||||
|
s.save(enrolment);
|
||||||
|
s.flush();
|
||||||
|
List data = ( List ) s.createCriteria( Enrolment.class)
|
||||||
|
.setProjection( Projections.projectionList()
|
||||||
|
.add( Projections.property( "semester" ) )
|
||||||
|
.add( Projections.property("year") )
|
||||||
|
.add( Projections.id() ) )
|
||||||
|
.list();
|
||||||
|
t.rollback();
|
||||||
|
s.close();
|
||||||
|
}
|
||||||
|
|
||||||
public void testSubcriteriaJoinTypes() {
|
public void testSubcriteriaJoinTypes() {
|
||||||
Session session = openSession();
|
Session session = openSession();
|
||||||
Transaction t = session.beginTransaction();
|
Transaction t = session.beginTransaction();
|
||||||
|
|
|
@ -10,21 +10,41 @@
|
||||||
<generator class="assigned"/>
|
<generator class="assigned"/>
|
||||||
</id>
|
</id>
|
||||||
<property name="description"/>
|
<property name="description"/>
|
||||||
|
<set name="courseMeetings" inverse="true" cascade="all-delete-orphan">
|
||||||
|
<key column="courseCode"/>
|
||||||
|
<one-to-many class="CourseMeeting"/>
|
||||||
|
</set>
|
||||||
</class>
|
</class>
|
||||||
|
|
||||||
|
<class name="CourseMeeting">
|
||||||
|
<composite-id name="id" class="CourseMeetingId">
|
||||||
|
<key-property name="courseCode"/>
|
||||||
|
<key-property name="day"/>
|
||||||
|
<key-property name="period"/>
|
||||||
|
<key-property name="location"/>
|
||||||
|
</composite-id>
|
||||||
|
<many-to-one name="course" insert="false" update="false">
|
||||||
|
<column name="courseCode"/>
|
||||||
|
</many-to-one>
|
||||||
|
</class>
|
||||||
|
|
||||||
<class name="Student">
|
<class name="Student">
|
||||||
<id name="studentNumber">
|
<id name="studentNumber">
|
||||||
<column name="studentId"/>
|
<column name="studentId"/>
|
||||||
<generator class="assigned"/>
|
<generator class="assigned"/>
|
||||||
</id>
|
</id>
|
||||||
<property name="name" not-null="true"/>
|
<property name="name" not-null="true"/>
|
||||||
|
<component name="cityState">
|
||||||
|
<property name="city" column="address_city"/>
|
||||||
|
<property name="state" column="address_state"/>
|
||||||
|
</component>
|
||||||
<set name="enrolments" inverse="true" cascade="delete">
|
<set name="enrolments" inverse="true" cascade="delete">
|
||||||
<key column="studentId"/>
|
<key column="studentId"/>
|
||||||
<one-to-many class="Enrolment"/>
|
<one-to-many class="Enrolment"/>
|
||||||
</set>
|
</set>
|
||||||
<many-to-one name="preferredCourse" column="preferredCourseCode"/>
|
<many-to-one name="preferredCourse" column="preferredCourseCode"/>
|
||||||
</class>
|
</class>
|
||||||
|
|
||||||
<class name="Enrolment">
|
<class name="Enrolment">
|
||||||
<composite-id>
|
<composite-id>
|
||||||
<key-property name="studentNumber">
|
<key-property name="studentNumber">
|
||||||
|
|
|
@ -10,6 +10,7 @@ import java.util.Set;
|
||||||
public class Student {
|
public class Student {
|
||||||
private long studentNumber;
|
private long studentNumber;
|
||||||
private String name;
|
private String name;
|
||||||
|
private CityState cityState;
|
||||||
private Course preferredCourse;
|
private Course preferredCourse;
|
||||||
private Set enrolments = new HashSet();
|
private Set enrolments = new HashSet();
|
||||||
|
|
||||||
|
@ -29,6 +30,14 @@ public class Student {
|
||||||
this.studentNumber = studentNumber;
|
this.studentNumber = studentNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CityState getCityState() {
|
||||||
|
return cityState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCityState(CityState cityState) {
|
||||||
|
this.cityState = cityState;
|
||||||
|
}
|
||||||
|
|
||||||
public Course getPreferredCourse() {
|
public Course getPreferredCourse() {
|
||||||
return preferredCourse;
|
return preferredCourse;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue