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
|
||||
*/
|
||||
public class AliasedProjection implements Projection {
|
||||
public class AliasedProjection implements EnhancedProjection {
|
||||
|
||||
private final Projection projection;
|
||||
private final String alias;
|
||||
|
@ -64,6 +64,12 @@ public class AliasedProjection implements Projection {
|
|||
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 Type[] getTypes(String alias, Criteria criteria, CriteriaQuery criteriaQuery)
|
||||
throws HibernateException {
|
||||
return this.alias.equals(alias) ?
|
||||
|
@ -77,6 +83,12 @@ public class AliasedProjection implements Projection {
|
|||
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() {
|
||||
return new String[]{ alias };
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ import org.hibernate.type.Type;
|
|||
/**
|
||||
* @author Gavin King
|
||||
*/
|
||||
public class Distinct implements Projection {
|
||||
public class Distinct implements EnhancedProjection {
|
||||
|
||||
private final Projection projection;
|
||||
|
||||
|
@ -63,10 +63,22 @@ public class Distinct implements Projection {
|
|||
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) {
|
||||
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() {
|
||||
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(position + i)
|
||||
.append('_');
|
||||
if (i < cols.length -1)
|
||||
buf.append(", ");
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ import org.hibernate.util.ArrayHelper;
|
|||
/**
|
||||
* @author Gavin King
|
||||
*/
|
||||
public class ProjectionList implements Projection {
|
||||
public class ProjectionList implements EnhancedProjection {
|
||||
|
||||
private List elements = new ArrayList();
|
||||
|
||||
|
@ -70,7 +70,7 @@ public class ProjectionList implements Projection {
|
|||
for ( int i=0; i<getLength(); i++ ) {
|
||||
Projection proj = getProjection(i);
|
||||
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(", ");
|
||||
}
|
||||
return buf.toString();
|
||||
|
@ -100,6 +100,16 @@ public class ProjectionList implements Projection {
|
|||
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) {
|
||||
for ( int i=0; i<getLength(); i++ ) {
|
||||
String[] result = getProjection(i).getColumnAliases(alias, loc);
|
||||
|
@ -109,6 +119,27 @@ public class ProjectionList implements Projection {
|
|||
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) {
|
||||
for ( int i=0; i<getLength(); i++ ) {
|
||||
Type[] result = getProjection(i).getTypes(alias, criteria, criteriaQuery);
|
||||
|
|
|
@ -61,12 +61,17 @@ public class PropertyProjection extends SimpleProjection {
|
|||
|
||||
public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery)
|
||||
throws HibernateException {
|
||||
return new StringBuffer()
|
||||
.append( criteriaQuery.getColumn(criteria, propertyName) )
|
||||
.append(" as y")
|
||||
.append(position)
|
||||
.append('_')
|
||||
.toString();
|
||||
StringBuffer buf = new StringBuffer();
|
||||
String[] cols = criteriaQuery.getColumnsUsingProjection( criteria, propertyName );
|
||||
for ( int i=0; i<cols.length; i++ ) {
|
||||
buf.append( cols[i] )
|
||||
.append(" as y")
|
||||
.append(position + i)
|
||||
.append('_');
|
||||
if (i < cols.length -1)
|
||||
buf.append(", ");
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public boolean isGrouped() {
|
||||
|
|
|
@ -24,6 +24,8 @@
|
|||
*/
|
||||
package org.hibernate.criterion;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.type.Type;
|
||||
|
@ -33,7 +35,7 @@ import org.hibernate.type.Type;
|
|||
* A single-column projection that may be aliased
|
||||
* @author Gavin King
|
||||
*/
|
||||
public abstract class SimpleProjection implements Projection {
|
||||
public abstract class SimpleProjection implements EnhancedProjection {
|
||||
|
||||
public Projection as(String alias) {
|
||||
return Projections.alias(this, alias);
|
||||
|
@ -43,6 +45,10 @@ public abstract class SimpleProjection implements Projection {
|
|||
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)
|
||||
throws HibernateException {
|
||||
return null;
|
||||
|
@ -52,6 +58,25 @@ public abstract class SimpleProjection implements Projection {
|
|||
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() {
|
||||
return new String[1];
|
||||
}
|
||||
|
|
|
@ -48,6 +48,7 @@ import org.hibernate.persister.entity.OuterJoinLoadable;
|
|||
import org.hibernate.persister.entity.Lockable;
|
||||
import org.hibernate.transform.ResultTransformer;
|
||||
import org.hibernate.type.Type;
|
||||
import org.hibernate.util.ArrayHelper;
|
||||
|
||||
/**
|
||||
* 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();
|
||||
result = new Object[types.length];
|
||||
String[] columnAliases = translator.getProjectedColumnAliases();
|
||||
for ( int i=0; i<result.length; i++ ) {
|
||||
result[i] = types[i].nullSafeGet(rs, columnAliases[i], session, null);
|
||||
for ( int i=0, pos=0; i<result.length; i++ ) {
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ import org.hibernate.LockMode;
|
|||
import org.hibernate.MappingException;
|
||||
import org.hibernate.QueryException;
|
||||
import org.hibernate.LockOptions;
|
||||
import org.hibernate.criterion.EnhancedProjection;
|
||||
import org.hibernate.hql.ast.util.SessionFactoryHelper;
|
||||
import org.hibernate.criterion.CriteriaQuery;
|
||||
import org.hibernate.criterion.Criterion;
|
||||
|
@ -362,7 +363,9 @@ public class CriteriaQueryTranslator implements CriteriaQuery {
|
|||
}
|
||||
|
||||
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() {
|
||||
|
@ -426,10 +429,13 @@ public class CriteriaQueryTranslator implements CriteriaQuery {
|
|||
|
||||
//first look for a reference to a projection alias
|
||||
final Projection projection = rootCriteria.getProjection();
|
||||
String[] projectionColumns = projection == null ?
|
||||
null :
|
||||
projection.getColumnAliases( propertyName, 0 );
|
||||
|
||||
String[] projectionColumns = null;
|
||||
if ( projection != null ) {
|
||||
projectionColumns = ( projection instanceof EnhancedProjection ?
|
||||
( ( EnhancedProjection ) projection ).getColumnAliases( propertyName, 0, rootCriteria, this ) :
|
||||
projection.getColumnAliases( propertyName, 0 )
|
||||
);
|
||||
}
|
||||
if ( projectionColumns == null ) {
|
||||
//it does not refer to an alias of a projection,
|
||||
//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 $
|
||||
package org.hibernate.test.criteria;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Gavin King
|
||||
*/
|
||||
public class Course {
|
||||
private String courseCode;
|
||||
private String description;
|
||||
private Set courseMeetings = new HashSet();
|
||||
|
||||
public String getCourseCode() {
|
||||
return courseCode;
|
||||
}
|
||||
|
@ -19,4 +24,10 @@ public class Course {
|
|||
public void setDescription(String 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.Order;
|
||||
import org.hibernate.criterion.Projection;
|
||||
import org.hibernate.criterion.ProjectionList;
|
||||
import org.hibernate.criterion.Projections;
|
||||
import org.hibernate.criterion.Property;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
|
@ -742,6 +743,152 @@ public class CriteriaQueryTest extends FunctionalTestCase {
|
|||
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() {
|
||||
Session session = openSession();
|
||||
Transaction t = session.beginTransaction();
|
||||
|
|
|
@ -10,14 +10,34 @@
|
|||
<generator class="assigned"/>
|
||||
</id>
|
||||
<property name="description"/>
|
||||
<set name="courseMeetings" inverse="true" cascade="all-delete-orphan">
|
||||
<key column="courseCode"/>
|
||||
<one-to-many class="CourseMeeting"/>
|
||||
</set>
|
||||
</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">
|
||||
<id name="studentNumber">
|
||||
<column name="studentId"/>
|
||||
<generator class="assigned"/>
|
||||
</id>
|
||||
<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">
|
||||
<key column="studentId"/>
|
||||
<one-to-many class="Enrolment"/>
|
||||
|
|
|
@ -10,6 +10,7 @@ import java.util.Set;
|
|||
public class Student {
|
||||
private long studentNumber;
|
||||
private String name;
|
||||
private CityState cityState;
|
||||
private Course preferredCourse;
|
||||
private Set enrolments = new HashSet();
|
||||
|
||||
|
@ -29,6 +30,14 @@ public class Student {
|
|||
this.studentNumber = studentNumber;
|
||||
}
|
||||
|
||||
public CityState getCityState() {
|
||||
return cityState;
|
||||
}
|
||||
|
||||
public void setCityState(CityState cityState) {
|
||||
this.cityState = cityState;
|
||||
}
|
||||
|
||||
public Course getPreferredCourse() {
|
||||
return preferredCourse;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue