EJB-447 : Implement JPA 2.0 criteria apis

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17228 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2009-08-04 19:13:33 +00:00
parent 152dc5abf9
commit bd4c85ce10
12 changed files with 132 additions and 42 deletions

View File

@ -23,6 +23,7 @@ package org.hibernate.ejb.criteria;
import javax.persistence.criteria.CollectionJoin;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Fetch;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ListJoin;
@ -42,7 +43,9 @@ import javax.persistence.metamodel.SingularAttribute;
*
* @author Steve Ebersole
*/
public abstract class AbstractBasicPluralJoin<O,C,E> extends JoinImpl<O,E> implements PluralJoin<O,C,E> {
public abstract class AbstractBasicPluralJoin<O,C,E>
extends JoinImpl<O,E>
implements PluralJoin<O,C,E>, Fetch<O,E> {
public AbstractBasicPluralJoin(
QueryBuilderImpl queryBuilder,

View File

@ -21,7 +21,6 @@
*/
package org.hibernate.ejb.criteria;
import javax.persistence.criteria.CollectionJoin;
import javax.persistence.criteria.JoinType;
import javax.persistence.metamodel.CollectionAttribute;
@ -33,7 +32,7 @@ import javax.persistence.metamodel.CollectionAttribute;
*/
public class BasicCollectionJoinImpl<O,E>
extends AbstractBasicPluralJoin<O,java.util.Collection<E>,E>
implements CollectionJoin<O,E> {
implements JoinImplementors.CollectionJoinImplementor<O,E> {
public BasicCollectionJoinImpl(
QueryBuilderImpl queryBuilder,

View File

@ -23,7 +23,6 @@ package org.hibernate.ejb.criteria;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ListJoin;
import javax.persistence.metamodel.ListAttribute;
import org.hibernate.ejb.criteria.expression.ListIndexExpression;
@ -35,7 +34,7 @@ import org.hibernate.ejb.criteria.expression.ListIndexExpression;
*/
public class BasicListJoinImpl<O,E>
extends AbstractBasicPluralJoin<O,java.util.List<E>,E>
implements ListJoin<O,E> {
implements JoinImplementors.ListJoinImplementor<O,E> {
public BasicListJoinImpl(
QueryBuilderImpl queryBuilder,

View File

@ -26,7 +26,6 @@ import java.util.Map.Entry;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.MapJoin;
import javax.persistence.criteria.Path;
import javax.persistence.metamodel.MapAttribute;
import javax.persistence.metamodel.Type.PersistenceType;
@ -43,7 +42,7 @@ import javax.persistence.metamodel.Type.PersistenceType;
*/
public class BasicMapJoinImpl<O,K,V>
extends AbstractBasicPluralJoin<O,java.util.Map<K,V>,V>
implements MapJoin<O,K,V> {
implements JoinImplementors.MapJoinImplementor<O,K,V> {
public BasicMapJoinImpl(
QueryBuilderImpl queryBuilder,

View File

@ -22,7 +22,6 @@
package org.hibernate.ejb.criteria;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.SetJoin;
import javax.persistence.metamodel.SetAttribute;
/**
@ -33,7 +32,7 @@ import javax.persistence.metamodel.SetAttribute;
*/
public class BasicSetJoinImpl<O,E>
extends AbstractBasicPluralJoin<O,java.util.Set<E>,E>
implements SetJoin<O,E> {
implements JoinImplementors.SetJoinImplementor<O,E> {
public BasicSetJoinImpl(
QueryBuilderImpl queryBuilder,

View File

@ -21,7 +21,6 @@
*/
package org.hibernate.ejb.criteria;
import javax.persistence.criteria.CollectionJoin;
import javax.persistence.criteria.JoinType;
import javax.persistence.metamodel.CollectionAttribute;
@ -31,7 +30,9 @@ import javax.persistence.metamodel.CollectionAttribute;
*
* @author Steve Ebersole
*/
public class CollectionJoinImpl<O,E> extends JoinImpl<O,E> implements CollectionJoin<O,E> {
public class CollectionJoinImpl<O,E>
extends JoinImpl<O,E>
implements JoinImplementors.CollectionJoinImplementor<O,E> {
public CollectionJoinImpl(
QueryBuilderImpl queryBuilder,

View File

@ -124,10 +124,6 @@ public abstract class FromImpl<Z,X> extends PathImpl<X> implements From<Z,X> {
return joins;
}
protected void addJoin(Join<X,?> join) {
getJoinsInternal().add( join );
}
/**
* {@inheritDoc}
*/
@ -139,6 +135,12 @@ public abstract class FromImpl<Z,X> extends PathImpl<X> implements From<Z,X> {
* {@inheritDoc}
*/
public <Y> Join<X, Y> join(SingularAttribute<? super X, Y> attribute, JoinType jt) {
Join<X, Y> join = constructJoin( attribute, jt );
getJoinsInternal().add( join );
return join;
}
private <Y> JoinImplementors.JoinImplementor<X, Y> constructJoin(SingularAttribute<? super X, Y> attribute, JoinType jt) {
if ( PersistenceType.BASIC.equals( attribute.getType().getPersistenceType() ) ) {
throw new BasicPathUsageException( "Cannot join to attribute of basic type", attribute );
}
@ -150,17 +152,14 @@ public abstract class FromImpl<Z,X> extends PathImpl<X> implements From<Z,X> {
}
final Class<Y> attributeType = attribute.getBindableJavaType();
final JoinImpl<X, Y> join = new JoinImpl<X, Y>(
return new JoinImpl<X, Y>(
queryBuilder(),
attributeType,
this,
attribute,
jt
);
joins.add( join );
return join;
}
/**
* {@inheritDoc}
*/
@ -172,12 +171,18 @@ public abstract class FromImpl<Z,X> extends PathImpl<X> implements From<Z,X> {
* {@inheritDoc}
*/
public <Y> CollectionJoin<X, Y> join(CollectionAttribute<? super X, Y> collection, JoinType jt) {
final CollectionJoin<X, Y> join = constructJoin( collection, jt );
getJoinsInternal().add( join );
return join;
}
private <Y> JoinImplementors.CollectionJoinImplementor<X, Y> constructJoin(CollectionAttribute<? super X, Y> collection, JoinType jt) {
if ( jt.equals( JoinType.RIGHT ) ) {
throw new UnsupportedOperationException( "RIGHT JOIN not supported" );
}
final Class<Y> attributeType = collection.getBindableJavaType();
final CollectionJoin<X, Y> join;
final JoinImplementors.CollectionJoinImplementor<X, Y> join;
if ( isBasicCollection( collection ) ) {
join = new BasicCollectionJoinImpl<X, Y>(
queryBuilder(),
@ -196,7 +201,6 @@ public abstract class FromImpl<Z,X> extends PathImpl<X> implements From<Z,X> {
jt
);
}
joins.add( join );
return join;
}
@ -215,19 +219,24 @@ public abstract class FromImpl<Z,X> extends PathImpl<X> implements From<Z,X> {
* {@inheritDoc}
*/
public <Y> SetJoin<X, Y> join(SetAttribute<? super X, Y> set, JoinType jt) {
final SetJoin<X, Y> join = constructJoin( set, jt );
getJoinsInternal().add( join );
return join;
}
private <Y> JoinImplementors.SetJoinImplementor<X, Y> constructJoin(SetAttribute<? super X, Y> set, JoinType jt) {
if ( jt.equals( JoinType.RIGHT ) ) {
throw new UnsupportedOperationException( "RIGHT JOIN not supported" );
}
final Class<Y> attributeType = set.getBindableJavaType();
final SetJoin<X, Y> join;
final JoinImplementors.SetJoinImplementor<X, Y> join;
if ( isBasicCollection( set ) ) {
join = new BasicSetJoinImpl<X, Y>( queryBuilder(), attributeType, this, set, jt );
}
else {
join = new SetJoinImpl<X, Y>( queryBuilder(), attributeType, this, set, jt );
}
joins.add( join );
return join;
}
@ -242,19 +251,24 @@ public abstract class FromImpl<Z,X> extends PathImpl<X> implements From<Z,X> {
* {@inheritDoc}
*/
public <Y> ListJoin<X, Y> join(ListAttribute<? super X, Y> list, JoinType jt) {
final ListJoin<X, Y> join = constructJoin( list, jt );
getJoinsInternal().add( join );
return join;
}
private <Y> JoinImplementors.ListJoinImplementor<X, Y> constructJoin(ListAttribute<? super X, Y> list, JoinType jt) {
if ( jt.equals( JoinType.RIGHT ) ) {
throw new UnsupportedOperationException( "RIGHT JOIN not supported" );
}
final Class<Y> attributeType = list.getBindableJavaType();
final ListJoin<X, Y> join;
final JoinImplementors.ListJoinImplementor<X, Y> join;
if ( isBasicCollection( list ) ) {
join = new BasicListJoinImpl<X, Y>( queryBuilder(), attributeType, this, list, jt );
}
else {
join = new ListJoinImpl<X, Y>( queryBuilder(), attributeType, this, list, jt );
}
joins.add( join );
return join;
}
@ -269,19 +283,24 @@ public abstract class FromImpl<Z,X> extends PathImpl<X> implements From<Z,X> {
* {@inheritDoc}
*/
public <K, V> MapJoin<X, K, V> join(MapAttribute<? super X, K, V> map, JoinType jt) {
final MapJoin<X, K, V> join = constructJoin( map, jt );
getJoinsInternal().add( join );
return join;
}
private <K, V> JoinImplementors.MapJoinImplementor<X, K, V> constructJoin(MapAttribute<? super X, K, V> map, JoinType jt) {
if ( jt.equals( JoinType.RIGHT ) ) {
throw new UnsupportedOperationException( "RIGHT JOIN not supported" );
}
final Class<V> attributeType = map.getBindableJavaType();
final MapJoin<X, K, V> join;
final JoinImplementors.MapJoinImplementor<X, K, V> join;
if ( isBasicCollection( map ) ) {
join = new BasicMapJoinImpl<X,K,V>( queryBuilder(), attributeType, this, map, jt );
}
else {
join = new MapJoinImpl<X,K,V>( queryBuilder(), attributeType, this, map, jt );
}
joins.add( join );
return join;
}
@ -444,9 +463,10 @@ public abstract class FromImpl<Z,X> extends PathImpl<X> implements From<Z,X> {
return fetch( singularAttribute, DEFAULT_JOIN_TYPE );
}
public <Y> Fetch<X, Y> fetch(SingularAttribute<? super X, Y> singularAttribute, JoinType jt) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
public <Y> Fetch<X, Y> fetch(SingularAttribute<? super X, Y> attribute, JoinType jt) {
Fetch<X, Y> fetch = constructJoin( attribute, jt );
getFetchesInternal().add( fetch );
return fetch;
}
public <Y> Fetch<X, Y> fetch(PluralAttribute<? super X, ?, Y> pluralAttribute) {
@ -454,8 +474,22 @@ public abstract class FromImpl<Z,X> extends PathImpl<X> implements From<Z,X> {
}
public <Y> Fetch<X, Y> fetch(PluralAttribute<? super X, ?, Y> pluralAttribute, JoinType jt) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
final Fetch<X, Y> fetch;
// TODO : combine Fetch and Join hierarchies (JoinImplementor extends Join,Fetch???)
if ( CollectionType.COLLECTION.equals( pluralAttribute.getCollectionType() ) ) {
fetch = constructJoin( (CollectionAttribute<X,Y>) pluralAttribute, jt );
}
else if ( CollectionType.LIST.equals( pluralAttribute.getCollectionType() ) ) {
fetch = constructJoin( (ListAttribute<X,Y>) pluralAttribute, jt );
}
else if ( CollectionType.SET.equals( pluralAttribute.getCollectionType() ) ) {
fetch = constructJoin( (SetAttribute<X,Y>) pluralAttribute, jt );
}
else {
fetch = constructJoin( (MapAttribute<X,?,Y>) pluralAttribute, jt );
}
getFetchesInternal().add( fetch );
return fetch;
}
public <Y> Fetch<X, Y> fetch(String attributeName) {
@ -463,8 +497,13 @@ public abstract class FromImpl<Z,X> extends PathImpl<X> implements From<Z,X> {
}
public <Y> Fetch<X, Y> fetch(String attributeName, JoinType jt) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
Attribute<X,?> attribute = getAttribute( attributeName );
if ( attribute.isCollection() ) {
return fetch( (PluralAttribute<X,?,Y>)attribute, jt );
}
else {
return fetch( (SingularAttribute<X,Y>) attribute, jt );
}
}

View File

@ -22,7 +22,6 @@
package org.hibernate.ejb.criteria;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.ManagedType;
@ -32,7 +31,7 @@ import javax.persistence.metamodel.ManagedType;
*
* @author Steve Ebersole
*/
public class JoinImpl<Z, X> extends FromImpl<Z, X> implements Join<Z, X> {
public class JoinImpl<Z, X> extends FromImpl<Z, X> implements JoinImplementors.JoinImplementor<Z,X> {
// TODO : keep track or whether any non-identifier properties get dereferenced
// for join optimization like in HQL

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2009, 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.ejb.criteria;
import javax.persistence.criteria.CollectionJoin;
import javax.persistence.criteria.Fetch;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.ListJoin;
import javax.persistence.criteria.MapJoin;
import javax.persistence.criteria.SetJoin;
/**
* Consolidates the {@link Join} and {@link Fetch} hierarchies since that is how we implement them.
* This allows us to treat them polymorphically.
*
* @author Steve Ebersole
*/
public interface JoinImplementors {
public static interface JoinImplementor<Z,X> extends Join<Z,X>, Fetch<Z,X> {
}
public static interface CollectionJoinImplementor<Z,X> extends CollectionJoin<Z,X>, Fetch<Z,X> {
}
public static interface SetJoinImplementor<Z,X> extends SetJoin<Z,X>, Fetch<Z,X> {
}
public static interface ListJoinImplementor<Z,X> extends ListJoin<Z,X>, Fetch<Z,X> {
}
public static interface MapJoinImplementor<Z,K,V> extends MapJoin<Z,K,V>, Fetch<Z,V> {
}
}

View File

@ -23,7 +23,6 @@ package org.hibernate.ejb.criteria;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ListJoin;
import javax.persistence.metamodel.ListAttribute;
import org.hibernate.ejb.criteria.expression.ListIndexExpression;
@ -33,7 +32,7 @@ import org.hibernate.ejb.criteria.expression.ListIndexExpression;
*
* @author Steve Ebersole
*/
public class ListJoinImpl<O,E> extends JoinImpl<O,E> implements ListJoin<O,E> {
public class ListJoinImpl<O,E> extends JoinImpl<O,E> implements JoinImplementors.ListJoinImplementor<O,E> {
public ListJoinImpl(
QueryBuilderImpl queryBuilder,
Class<E> javaType,

View File

@ -26,7 +26,6 @@ import java.util.Map.Entry;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.MapJoin;
import javax.persistence.criteria.Path;
import javax.persistence.metamodel.MapAttribute;
import javax.persistence.metamodel.Type.PersistenceType;
@ -39,7 +38,7 @@ import javax.persistence.metamodel.Type.PersistenceType;
*/
public class MapJoinImpl<O,K,V>
extends JoinImpl<O,V>
implements MapJoin<O,K,V> {
implements JoinImplementors.MapJoinImplementor<O,K,V> {
public MapJoinImpl(
QueryBuilderImpl queryBuilder,

View File

@ -22,7 +22,6 @@
package org.hibernate.ejb.criteria;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.SetJoin;
import javax.persistence.metamodel.SetAttribute;
/**
@ -31,7 +30,10 @@ import javax.persistence.metamodel.SetAttribute;
*
* @author Steve Ebersole
*/
public class SetJoinImpl<O,E> extends JoinImpl<O,E> implements SetJoin<O,E> {
public class SetJoinImpl<O,E>
extends JoinImpl<O,E>
implements JoinImplementors.SetJoinImplementor<O,E> {
public SetJoinImpl(
QueryBuilderImpl queryBuilder,
Class<E> javaType,