EJB-455 : Implement EntityManager#getEntityManagerFactory;

EJB-447 : Implement JPA 2.0 criteria apis

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17222 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2009-07-31 17:53:18 +00:00
parent c05509be7c
commit 9fc25c8c8a
33 changed files with 3565 additions and 27 deletions

View File

@ -69,6 +69,7 @@ import org.hibernate.util.JTAHelper;
public abstract class AbstractEntityManagerImpl implements HibernateEntityManagerImplementor, Serializable {
private static final Logger log = LoggerFactory.getLogger( AbstractEntityManagerImpl.class );
private EntityManagerFactoryImpl entityManagerFactory;
protected transient TransactionImpl tx = new TransactionImpl( this );
protected PersistenceContextType persistenceContextType;
private FlushModeType flushModeType = FlushModeType.AUTO;
@ -76,7 +77,11 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
private Map properties;
protected AbstractEntityManagerImpl(
PersistenceContextType type, PersistenceUnitTransactionType transactionType, Map properties) {
EntityManagerFactoryImpl entityManagerFactory,
PersistenceContextType type,
PersistenceUnitTransactionType transactionType,
Map properties) {
this.entityManagerFactory = entityManagerFactory;
this.persistenceContextType = type;
this.transactionType = transactionType;
this.properties = properties != null ? properties : CollectionHelper.EMPTY_MAP;
@ -106,7 +111,20 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
}
public Query createQuery(CriteriaQuery criteriaQuery) {
return null; //To change body of implemented methods use File | Settings | File Templates.
// TODO-STEVE : here is the interpretation/compilation portion.
// One option is to build on top of the existing
// org.hibernate.loader.custom.CustomQuery infastructure
// (which is how native sql queries are implemented e.g.).
// If so, then here we could interpret the criteria into
// a CustomQuery instance which is passed into the
// Query instance returned here. We would then call into
// the various SessionImplementor methods for execution
// such as #listCustomQuery and #scrollCustomQuery.
//
// The drawback to this (^^) approach is that CustomQuery +
// SessionImplementor combo does not support #executeUpdate
// processing...
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public Query createNamedQuery(String name) {
@ -388,14 +406,18 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
return tx;
}
public EntityManagerFactory getEntityManagerFactory() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
/**
* {@inheritDoc}
*/
public EntityManagerFactoryImpl getEntityManagerFactory() {
return entityManagerFactory;
}
/**
* {@inheritDoc}
*/
public QueryBuilder getQueryBuilder() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
return getEntityManagerFactory().getQueryBuilder();
}
public Metamodel getMetamodel() {

View File

@ -18,11 +18,11 @@ import org.hibernate.util.JTAHelper;
*/
public class CurrentEntityManagerImpl extends AbstractEntityManagerImpl {
private SessionFactory sessionFactory;
public CurrentEntityManagerImpl(SessionFactory sessionFactory, PersistenceUnitTransactionType transactionType, Map properties) {
super( PersistenceContextType.TRANSACTION, transactionType, properties );
this.sessionFactory = sessionFactory;
public CurrentEntityManagerImpl(
EntityManagerFactoryImpl entityManagerFactory,
PersistenceUnitTransactionType transactionType,
Map properties) {
super( entityManagerFactory, PersistenceContextType.TRANSACTION, transactionType, properties );
postInit();
}
@ -33,14 +33,14 @@ public class CurrentEntityManagerImpl extends AbstractEntityManagerImpl {
* sure the conenctions are released. Be aware that the session will not be closed explicitly.
*/
SessionFactoryImplementor sfi = (SessionFactoryImplementor) getEntityManagerFactory().getSessionFactory();
Session s;
SessionFactoryImplementor sfi = (SessionFactoryImplementor) sessionFactory;
if ( !JTAHelper.isTransactionInProgress( sfi ) ) {
s = sfi.openTemporarySession();
( (SessionImplementor) s ).setAutoClear( true );
}
else {
s = sessionFactory.getCurrentSession();
s = sfi.getCurrentSession();
}
return s;
}

View File

@ -12,17 +12,18 @@ import javax.persistence.criteria.QueryBuilder;
import javax.persistence.spi.PersistenceUnitTransactionType;
import org.hibernate.SessionFactory;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* @author Gavin King
* @author Emmanuel Bernard
*/
public class EntityManagerFactoryImpl implements HibernateEntityManagerFactory {
private SessionFactory sessionFactory;
private PersistenceUnitTransactionType transactionType;
private boolean discardOnClose;
private Class sessionInterceptorClass;
private QueryBuilderImpl criteriaQueryBuilder;
public EntityManagerFactoryImpl(
SessionFactory sessionFactory,
@ -33,6 +34,7 @@ public class EntityManagerFactoryImpl implements HibernateEntityManagerFactory {
this.transactionType = transactionType;
this.discardOnClose = discardOnClose;
this.sessionInterceptorClass = sessionInterceptorClass;
this.criteriaQueryBuilder = new QueryBuilderImpl( this );
}
public EntityManager createEntityManager() {
@ -42,14 +44,13 @@ public class EntityManagerFactoryImpl implements HibernateEntityManagerFactory {
public EntityManager createEntityManager(Map map) {
//TODO support discardOnClose, persistencecontexttype?, interceptor,
return new EntityManagerImpl(
sessionFactory, PersistenceContextType.EXTENDED, transactionType,
this, PersistenceContextType.EXTENDED, transactionType,
discardOnClose, sessionInterceptorClass, map
);
}
public QueryBuilder getQueryBuilder() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
return criteriaQueryBuilder;
}
public Metamodel getMetamodel() {
@ -72,6 +73,7 @@ public class EntityManagerFactoryImpl implements HibernateEntityManagerFactory {
}
public Cache getCache() {
// TODO : cache the cache reference?
return new JPACache( sessionFactory );
}

View File

@ -9,7 +9,6 @@ import javax.transaction.Synchronization;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Interceptor;
import org.hibernate.annotations.common.util.ReflectHelper;
import org.hibernate.cfg.Environment;
@ -21,21 +20,22 @@ import org.slf4j.LoggerFactory;
* @author Gavin King
*/
public class EntityManagerImpl extends AbstractEntityManagerImpl {
private static final Logger log = LoggerFactory.getLogger( EntityManagerImpl.class );
protected Session session;
protected SessionFactory sessionFactory;
protected boolean open;
protected boolean discardOnClose;
private Class sessionInterceptorClass;
public EntityManagerImpl(
SessionFactory sessionFactory, PersistenceContextType pcType,
EntityManagerFactoryImpl entityManagerFactory,
PersistenceContextType pcType,
PersistenceUnitTransactionType transactionType,
boolean discardOnClose, Class sessionInterceptorClass, Map properties
boolean discardOnClose,
Class sessionInterceptorClass,
Map properties
) {
super( pcType, transactionType, properties );
this.sessionFactory = sessionFactory;
super( entityManagerFactory, pcType, transactionType, properties );
this.open = true;
this.discardOnClose = discardOnClose;
Object localSessionInterceptor = null;
@ -62,7 +62,6 @@ public class EntityManagerImpl extends AbstractEntityManagerImpl {
}
public Session getSession() {
if ( !open ) throw new IllegalStateException( "EntityManager is closed" );
return getRawSession();
}
@ -84,7 +83,7 @@ public class EntityManagerImpl extends AbstractEntityManagerImpl {
throw new PersistenceException("Session interceptor does not implement Interceptor: " + sessionInterceptorClass, e);
}
}
session = sessionFactory.openSession( interceptor );
session = getEntityManagerFactory().getSessionFactory().openSession( interceptor );
if ( persistenceContextType == PersistenceContextType.TRANSACTION ) {
( (SessionImplementor) session ).setAutoClear( true );
}

View File

@ -0,0 +1,45 @@
/*
* 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;
/**
* All nodes in a criteria query tree will generally need access to the {@link QueryBuilderImpl} from which they
* come. This base class provides convenient, consistent support for that.
*
* @author Steve Ebersole
*/
public class AbstractNode {
private final QueryBuilderImpl queryBuilder;
public AbstractNode(QueryBuilderImpl queryBuilder) {
this.queryBuilder = queryBuilder;
}
/**
* Provides protected access to the underlying {@link QueryBuilderImpl}.
*
* @return The underlying {@link QueryBuilderImpl} instance.
*/
protected QueryBuilderImpl queryBuilder() {
return queryBuilder;
}
}

View File

@ -0,0 +1,240 @@
/*
* 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 java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.Tuple;
import javax.persistence.criteria.Subquery;
import javax.persistence.metamodel.EntityType;
/**
* The Hibernate implementation of the JPA {@link CriteriaQuery}
* contract.
*
* @author Steve Ebersole
*/
public class CriteriaQueryImpl<T> extends AbstractNode implements CriteriaQuery<T> {
private final Class<T> returnType;
private final QueryStructure<T> queryStructure;
private List<Order> orderSpecs = Collections.emptyList();
public CriteriaQueryImpl(
QueryBuilderImpl queryBuilder,
Class<T> returnType) {
super( queryBuilder );
this.returnType = returnType;
this.queryStructure = new QueryStructure<T>( this, queryBuilder );
}
public Class<T> getResultType() {
return returnType;
}
// SELECTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public CriteriaQuery<T> distinct(boolean applyDistinction) {
queryStructure.setDistinction( applyDistinction );
return this;
}
public boolean isDistinct() {
return queryStructure.isDistinction();
}
@SuppressWarnings({ "unchecked" })
public Selection<T> getSelection() {
return ( Selection<T> ) queryStructure.getSelection();
}
public void applySelection(Selection<? extends T> selection) {
queryStructure.setSelection( selection );
}
public CriteriaQuery<T> select(Selection<? extends T> selection) {
applySelection( selection );
return this;
}
@SuppressWarnings({ "unchecked" })
public CriteriaQuery<T> multiselect(Selection<?>... selections) {
return multiselect( Arrays.asList( selections ) );
}
@SuppressWarnings({ "unchecked" })
public CriteriaQuery<T> multiselect(List<Selection<?>> selections) {
final Selection<? extends T> selection;
if ( Tuple.class.isAssignableFrom( getResultType() ) ) {
selection = ( Selection<? extends T> ) queryBuilder().tuple( selections );
}
else if ( getResultType().isArray() ) {
selection = ( Selection<? extends T> ) queryBuilder().array(
( Class<? extends Object[]> ) getResultType(),
selections
);
}
else if ( Object.class.equals( getResultType() ) ) {
switch ( selections.size() ) {
case 0: {
throw new IllegalArgumentException(
"empty selections passed to criteria query typed as Object"
);
}
case 1: {
selection = ( Selection<? extends T> ) selections.get( 0 );
break;
}
default: {
selection = ( Selection<? extends T> ) queryBuilder().array( selections );
}
}
}
else {
selection = queryBuilder().construct( getResultType(), selections );
}
applySelection( selection );
return this;
}
// ROOTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public Set<Root<?>> getRoots() {
return queryStructure.getRoots();
}
public <X> Root<X> from(EntityType<X> entityType) {
return queryStructure.from( entityType );
}
public <X> Root<X> from(Class<X> entityClass) {
return queryStructure.from( entityClass );
}
// RESTRICTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* {@inheritDoc}
*/
public Predicate getRestriction() {
return queryStructure.getRestriction();
}
/**
* {@inheritDoc}
*/
public CriteriaQuery<T> where(Expression<Boolean> expression) {
queryStructure.setRestriction( queryBuilder().wrap( expression ) );
return this;
}
/**
* {@inheritDoc}
*/
public CriteriaQuery<T> where(Predicate... predicates) {
// TODO : assuming this should be a conjuntion, but the spec does not say specifically...
queryStructure.setRestriction( queryBuilder().and( predicates ) );
return this;
}
// GROUPING ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* {@inheritDoc}
*/
public List<Expression<?>> getGroupList() {
return queryStructure.getGroupings();
}
/**
* {@inheritDoc}
*/
public CriteriaQuery<T> groupBy(Expression<?>... groupings) {
queryStructure.setGroupings( groupings );
return this;
}
/**
* {@inheritDoc}
*/
public Predicate getGroupRestriction() {
return queryStructure.getHaving();
}
/**
* {@inheritDoc}
*/
public CriteriaQuery<T> having(Expression<Boolean> expression) {
queryStructure.setHaving( queryBuilder().wrap( expression ) );
return this;
}
/**
* {@inheritDoc}
*/
public CriteriaQuery<T> having(Predicate... predicates) {
queryStructure.setHaving( queryBuilder().and( predicates ) );
return this;
}
// ORDERING ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public List<Order> getOrderList() {
return orderSpecs;
}
public CriteriaQuery<T> orderBy(Order... orders) {
if ( orders != null && orders.length > 0 ) {
orderSpecs = Arrays.asList( orders );
}
else {
orderSpecs = Collections.emptyList();
}
return this;
}
public Set<ParameterExpression<?>> getParameters() {
// TODO-STEVE : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public <U> Subquery<U> subquery(Class<U> subqueryType) {
return queryStructure.subquery( subqueryType );
}
}

View File

@ -0,0 +1,205 @@
/*
* 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 java.util.List;
import java.util.Set;
import javax.persistence.criteria.AbstractQuery;
import javax.persistence.criteria.CollectionJoin;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.ListJoin;
import javax.persistence.criteria.MapJoin;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.SetJoin;
import javax.persistence.criteria.Subquery;
import javax.persistence.metamodel.EntityType;
import org.hibernate.ejb.criteria.expression.AbstractExpression;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class CriteriaSubqueryImpl<T> extends AbstractExpression<T> implements Subquery<T> {
private final AbstractQuery<?> parent;
private final QueryStructure<T> queryStructure;
private Expression<T> selection;
private Set<Join<?, ?>> joins;
public CriteriaSubqueryImpl(
QueryBuilderImpl queryBuilder,
Class<T> javaType,
AbstractQuery<?> parent) {
super(queryBuilder, javaType);
this.parent = parent;
this.queryStructure = new QueryStructure<T>( this, queryBuilder );
}
public AbstractQuery<?> getParent() {
return parent;
}
// ROOTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public Set<Root<?>> getRoots() {
return queryStructure.getRoots();
}
public <X> Root<X> from(EntityType<X> entityType) {
return queryStructure.from( entityType );
}
public <X> Root<X> from(Class<X> entityClass) {
return queryStructure.from( entityClass );
}
// SELECTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public Subquery<T> distinct(boolean applyDistinction) {
queryStructure.setDistinction( applyDistinction );
return this;
}
public boolean isDistinct() {
return queryStructure.isDistinction();
}
public Expression<T> getSelection() {
return selection;
}
public Subquery<T> select(Expression<T> expression) {
queryStructure.setSelection( selection );
this.selection = expression;
return this;
}
// RESTRICTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* {@inheritDoc}
*/
public Predicate getRestriction() {
return queryStructure.getRestriction();
}
/**
* {@inheritDoc}
*/
public Subquery<T> where(Expression<Boolean> expression) {
queryStructure.setRestriction( queryBuilder().wrap( expression ) );
return this;
}
/**
* {@inheritDoc}
*/
public Subquery<T> where(Predicate... predicates) {
// TODO : assuming this should be a conjuntion, but the spec does not say specifically...
queryStructure.setRestriction( queryBuilder().and( predicates ) );
return this;
}
// GROUPING ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* {@inheritDoc}
*/
public List<Expression<?>> getGroupList() {
return queryStructure.getGroupings();
}
/**
* {@inheritDoc}
*/
public Subquery<T> groupBy(Expression<?>... groupings) {
queryStructure.setGroupings( groupings );
return this;
}
/**
* {@inheritDoc}
*/
public Predicate getGroupRestriction() {
return queryStructure.getHaving();
}
/**
* {@inheritDoc}
*/
public Subquery<T> having(Expression<Boolean> expression) {
queryStructure.setHaving( queryBuilder().wrap( expression ) );
return this;
}
/**
* {@inheritDoc}
*/
public Subquery<T> having(Predicate... predicates) {
queryStructure.setHaving( queryBuilder().and( predicates ) );
return this;
}
// CORRELATIONS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public Set<Join<?, ?>> getJoins() {
return joins;
}
public <Y> Root<Y> correlate(Root<Y> arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}
public <X, Y> Join<X, Y> correlate(Join<X, Y> arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}
public <X, Y> CollectionJoin<X, Y> correlate(CollectionJoin<X, Y> arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}
public <X, Y> SetJoin<X, Y> correlate(SetJoin<X, Y> arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}
public <X, Y> ListJoin<X, Y> correlate(ListJoin<X, Y> arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}
public <X, K, V> MapJoin<X, K, V> correlate(MapJoin<X, K, V> arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}
public <U> Subquery<U> subquery(Class<U> subqueryType) {
return queryStructure.subquery( subqueryType );
}
}

View File

@ -0,0 +1,338 @@
/*
* 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 java.util.Set;
import java.util.Collection;
import java.util.Map;
import java.util.LinkedHashSet;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.CollectionJoin;
import javax.persistence.criteria.SetJoin;
import javax.persistence.criteria.ListJoin;
import javax.persistence.criteria.MapJoin;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Fetch;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.CollectionAttribute;
import javax.persistence.metamodel.SetAttribute;
import javax.persistence.metamodel.ListAttribute;
import javax.persistence.metamodel.MapAttribute;
import javax.persistence.metamodel.Bindable;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.Type.PersistenceType;
import org.hibernate.ejb.criteria.expression.AbstractExpression;
import org.hibernate.ejb.criteria.expression.EntityTypeExpression;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public abstract class FromImpl<Z,X> extends PathImpl<X> implements From<Z,X> {
public static final JoinType DEFAULT_JOIN_TYPE = JoinType.INNER;
private final Expression<Class<? extends X>> type;
private Set<Join<X, ?>> joins;
private Set<Fetch<X, ?>> fetches;
/**
* Special constructor for {@link RootImpl}.
*
* @param queryBuilder
* @param entityType
*/
protected FromImpl(QueryBuilderImpl queryBuilder, EntityType<X> entityType) {
super( queryBuilder, entityType.getBindableJavaType(), null, entityType );
this.type = new EntityTypeExpression( queryBuilder, entityType.getBindableJavaType() );
}
public FromImpl(
QueryBuilderImpl queryBuilder,
Class<X> javaType,
PathImpl<Z> origin,
Bindable<X> model,
Expression<Class<? extends X>> type) {
super( queryBuilder, javaType, origin, model );
this.type = type;
}
@Override
public Expression<Class<? extends X>> type() {
return type;
}
// JOINS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* {@inheritDoc}
*/
public Set<Join<X, ?>> getJoins() {
return joins;
}
/**
* Retrieve the collection of joins, imnplementing delayed creations. Calls on this method
* assume the set is physically needed, and the result is the set being built if not already.
*
* @return The set of joins.
*/
public Set<Join<X, ?>> getJoinsInternal() {
if ( joins == null ) {
joins = new LinkedHashSet<Join<X,?>>();
}
return joins;
}
/**
* {@inheritDoc}
*/
public <Y> Join<X, Y> join(SingularAttribute<? super X, Y> singularAttribute) {
return join( singularAttribute, DEFAULT_JOIN_TYPE );
}
/**
* {@inheritDoc}
*/
public <Y> Join<X, Y> join(SingularAttribute<? super X, Y> attribute, JoinType jt) {
if ( PersistenceType.BASIC.equals( attribute.getType().getPersistenceType() ) ) {
throw new IllegalStateException( "Cannot join to basic type" );
}
throw new UnsupportedOperationException( "Not yet implemented!" );
}
/**
* {@inheritDoc}
*/
public <Y> CollectionJoin<X, Y> join(CollectionAttribute<? super X, Y> collection) {
return join( collection, DEFAULT_JOIN_TYPE );
}
/**
* {@inheritDoc}
*/
public <Y> CollectionJoin<X, Y> join(CollectionAttribute<? super X, Y> collection, JoinType jt) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
/**
* {@inheritDoc}
*/
public <Y> SetJoin<X, Y> join(SetAttribute<? super X, Y> set) {
return join( set, DEFAULT_JOIN_TYPE );
}
/**
* {@inheritDoc}
*/
public <Y> SetJoin<X, Y> join(SetAttribute<? super X, Y> set, JoinType jt) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
/**
* {@inheritDoc}
*/
public <Y> ListJoin<X, Y> join(ListAttribute<? super X, Y> list) {
return join( list, DEFAULT_JOIN_TYPE );
}
/**
* {@inheritDoc}
*/
public <Y> ListJoin<X, Y> join(ListAttribute<? super X, Y> list, JoinType jt) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
/**
* {@inheritDoc}
*/
public <K, V> MapJoin<X, K, V> join(MapAttribute<? super X, K, V> map) {
return join( map, DEFAULT_JOIN_TYPE );
}
/**
* {@inheritDoc}
*/
public <K, V> MapJoin<X, K, V> join(MapAttribute<? super X, K, V> map, JoinType jt) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
/**
* {@inheritDoc}
*/
public <X, Y> Join<X, Y> join(String attributeName) {
return join( attributeName, DEFAULT_JOIN_TYPE );
}
/**
* {@inheritDoc}
*/
public <X, Y> Join<X, Y> join(String attributeName, JoinType jt) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
/**
* {@inheritDoc}
*/
public <X, Y> CollectionJoin<X, Y> joinCollection(String attributeName) {
return joinCollection( attributeName, DEFAULT_JOIN_TYPE );
}
/**
* {@inheritDoc}
*/
public <X, Y> CollectionJoin<X, Y> joinCollection(String attributeName, JoinType jt) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
/**
* {@inheritDoc}
*/
public <X, Y> SetJoin<X, Y> joinSet(String attributeName) {
return joinSet( attributeName, DEFAULT_JOIN_TYPE );
}
/**
* {@inheritDoc}
*/
public <X, Y> SetJoin<X, Y> joinSet(String attributeName, JoinType jt) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
/**
* {@inheritDoc}
*/
public <X, Y> ListJoin<X, Y> joinList(String attributeName) {
return joinList( attributeName, DEFAULT_JOIN_TYPE );
}
/**
* {@inheritDoc}
*/
public <X, Y> ListJoin<X, Y> joinList(String attributeName, JoinType jt) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
/**
* {@inheritDoc}
*/
public <X, K, V> MapJoin<X, K, V> joinMap(String attributeName) {
return joinMap( attributeName, DEFAULT_JOIN_TYPE );
}
/**
* {@inheritDoc}
*/
public <X, K, V> MapJoin<X, K, V> joinMap(String attributeName, JoinType jt) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
// FETCHES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* {@inheritDoc}
*/
public Set<Fetch<X, ?>> getFetches() {
return fetches;
}
/**
* Retrieve the collection of fetches, imnplementing delayed creations. Calls on this method
* assume the set is physically needed, and the result is the set being built if not already.
*
* @return The set of fetches.
*/
public Set<Fetch<X, ?>> getFetchesInternal() {
if ( fetches == null ) {
fetches = new LinkedHashSet<Fetch<X,?>>();
}
return fetches;
}
public <Y> Fetch<X, Y> fetch(SingularAttribute<? super X, Y> singularAttribute) {
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(PluralAttribute<? super X, ?, Y> pluralAttribute) {
return fetch( pluralAttribute, DEFAULT_JOIN_TYPE );
}
public <Y> Fetch<X, Y> fetch(PluralAttribute<? super X, ?, Y> pluralAttribute, JoinType jt) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public <Y> Fetch<X, Y> fetch(String attributeName) {
return fetch( attributeName, DEFAULT_JOIN_TYPE );
}
public <Y> Fetch<X, Y> fetch(String attributeName, JoinType jt) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
// PATH HANDLING ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public <Y> Path<Y> get(SingularAttribute<? super X, Y> ySingularAttribute) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public <E, C extends Collection<E>> Expression<C> get(PluralAttribute<X, C, E> collection) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public <K, V, M extends Map<K, V>> Expression<M> get(MapAttribute<X, K, V> map) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public <Y> Path<Y> get(String attributeName) {
// TODO : implement
throw new UnsupportedOperationException( "Not yet implemented!" );
}
}

View File

@ -0,0 +1,57 @@
/*
* 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.Order;
import javax.persistence.criteria.Expression;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class OrderImpl implements Order {
private final Expression<?> expression;
private boolean ascending;
public OrderImpl(Expression<?> expression) {
this( expression, true );
}
public OrderImpl(Expression<?> expression, boolean ascending) {
this.expression = expression;
this.ascending = ascending;
}
public Order reverse() {
ascending = !ascending;
return this;
}
public boolean isAscending() {
return ascending;
}
public Expression<?> getExpression() {
return expression;
}
}

View File

@ -0,0 +1,121 @@
/*
* 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 java.util.Collection;
import java.util.Map;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Path;
import javax.persistence.metamodel.Bindable;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.MapAttribute;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.SingularAttribute;
import org.hibernate.ejb.criteria.expression.AbstractExpression;
/**
* A {@link Path} models an individual portion of a join expression.
*
* @author Steve Ebersole
*/
public class PathImpl<X> extends AbstractExpression<X> implements Path<X> {
private final PathImpl<?> origin;
private Bindable<X> model;
protected PathImpl(
QueryBuilderImpl queryBuilder,
Class<X> javaType,
PathImpl<?> origin,
Bindable<X> model) {
super( queryBuilder, javaType );
this.origin = origin;
this.model = model;
}
/**
* {@inheritDoc}
*/
public PathImpl<?> getParentPath() {
return origin;
}
/**
* {@inheritDoc}
*/
public Bindable<X> getModel() {
if ( model == null ) {
throw new IllegalStateException( this + " represents a basic path and not a bindable" );
}
return model;
}
/**
* {@inheritDoc}
* <p/>
* Subclasses override this appropriately, but here we simply throw
* an {@link IllegalStateException}
*/
public Expression<Class<? extends X>> type() {
throw new IllegalStateException( "type() is not applicable to a primitive path node." ); }
/**
* {@inheritDoc}
* <p/>
* Subclasses override this appropriately, but here we simply throw
* an {@link IllegalStateException}
*/
public <Y> Path<Y> get(SingularAttribute<? super X, Y> attribute) {
throw new IllegalStateException( this + " is a primitive path node." );
}
/**
* {@inheritDoc}
* <p/>
* Subclasses override this appropriately, but here we simply throw
* an {@link IllegalStateException}
*/
public <E, C extends Collection<E>> Expression<C> get(PluralAttribute<X, C, E> collection) {
throw new IllegalStateException( this + " is a primitive path node." );
}
/**
* {@inheritDoc}
* <p/>
* Subclasses override this appropriately, but here we simply throw
* an {@link IllegalStateException}
*/
public <K, V, M extends Map<K, V>> Expression<M> get(MapAttribute<X, K, V> map) {
throw new IllegalStateException( this + " is a primitive path node." );
}
/**
* {@inheritDoc}
* <p/>
* Subclasses override this appropriately, but here we simply throw
* an {@link IllegalStateException}
*/
public <Y> Path<Y> get(String attributeName) {
throw new IllegalStateException( this + " is a primitive path node." );
}
}

View File

@ -0,0 +1,948 @@
/*
* 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 java.io.Serializable;
import java.util.List;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.math.BigDecimal;
import java.math.BigInteger;
import javax.persistence.criteria.QueryBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Selection;
import javax.persistence.criteria.CompoundSelection;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Subquery;
import javax.persistence.Tuple;
import org.hibernate.ejb.EntityManagerFactoryImpl;
import org.hibernate.ejb.criteria.expression.CompoundSelectionImpl;
import org.hibernate.ejb.criteria.expression.ParameterExpressionImpl;
import org.hibernate.ejb.criteria.expression.LiteralExpression;
import org.hibernate.ejb.criteria.expression.function.AverageAggregrateFunction;
import org.hibernate.ejb.criteria.expression.function.SumAggregateFunction;
import org.hibernate.ejb.criteria.predicate.BooleanExpressionPredicate;
import org.hibernate.ejb.criteria.predicate.ExplicitTruthValueCheck;
import org.hibernate.ejb.criteria.predicate.TruthValue;
import org.hibernate.ejb.criteria.predicate.NullnessPredicate;
import org.hibernate.ejb.criteria.predicate.CompoundPredicate;
import org.hibernate.ejb.criteria.predicate.ComparisonPredicate;
import org.hibernate.ejb.criteria.predicate.InPredicate;
import org.hibernate.ejb.criteria.predicate.BetweenPredicate;
import static org.hibernate.ejb.criteria.predicate.ComparisonPredicate.ComparisonOperator;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class QueryBuilderImpl implements QueryBuilder, Serializable {
private final EntityManagerFactoryImpl entityManagerFactory;
public QueryBuilderImpl(EntityManagerFactoryImpl entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
/**
* Provides protected access to the underlying {@link EntityManagerFactoryImpl}.
*
* @return The underlying {@link EntityManagerFactoryImpl}
*/
protected EntityManagerFactoryImpl getEntityManagerFactory() {
return entityManagerFactory;
}
/**
* {@inheritDoc}
*/
public CriteriaQuery<Object> createQuery() {
return new CriteriaQueryImpl<Object>( this, Object.class );
}
/**
* {@inheritDoc}
*/
public <T> CriteriaQuery<T> createQuery(Class<T> resultClass) {
return new CriteriaQueryImpl<T>( this, resultClass );
}
/**
* {@inheritDoc}
*/
public CriteriaQuery<Tuple> createTupleQuery() {
return new CriteriaQueryImpl<Tuple>( this, Tuple.class );
}
/**
* Package-protected method to centralize checking of criteria query
* multiselects as defined by the
* {@link CriteriaQuery#multiselect(List)} method.
*
* @param selections The selection varargs to check
*
* @throws IllegalArgumentException If, as per
* {@link CriteriaQuery#multiselect(List)} documentation,
* <i>&quot;An argument to the multiselect method must not be a tuple-
* or array-valued compound selection item.&quot;</i>
*/
void checkMultiselect(List<Selection<?>> selections) {
for ( Selection<?> selection : selections ) {
if ( selection.isCompoundSelection() ) {
if ( selection.getJavaType().isArray() ) {
throw new IllegalArgumentException(
"multiselect selections cannot contain " +
"compound array-valued elements"
);
}
if ( Tuple.class.isAssignableFrom( selection.getJavaType() ) ) {
throw new IllegalArgumentException(
"multiselect selections cannot contain " +
"compound tuple-valued elements"
);
}
}
}
}
/**
* {@inheritDoc}
*/
public CompoundSelection<Tuple> tuple(Selection<?>... selections) {
return tuple( Arrays.asList( selections ) );
}
/**
* Version of {@link #tuple(Selection[])} taking a list.
*
* @param selections List of selections.
*
* @return The tuple compound selection
*/
public CompoundSelection<Tuple> tuple(List<Selection<?>> selections) {
checkMultiselect( selections );
return new CompoundSelectionImpl<Tuple>( this, Tuple.class, selections );
}
/**
* {@inheritDoc}
*/
public CompoundSelection<Object[]> array(Selection<?>... selections) {
return array( Arrays.asList( selections ) );
}
/**
* Version of {@link #array(Selection[])} taking a list of selections.
*
* @param selections List of selections.
*
* @return The array compound selection
*/
public CompoundSelection<Object[]> array(List<Selection<?>> selections) {
return array( Object[].class, selections );
}
/**
* Version of {@link #array(Selection[])} taking a list of selections,
* as well as the type of array.
*
* @param type The type of array
* @param selections List of selections.
*
* @return The array compound selection
*/
public <Y> CompoundSelection<Y> array(Class<Y> type, List<Selection<?>> selections) {
checkMultiselect( selections );
return new CompoundSelectionImpl<Y>( this, type, selections );
}
/**
* {@inheritDoc}
*/
public <Y> CompoundSelection<Y> construct(Class<Y> result, Selection<?>... selections) {
return construct( result, Arrays.asList( selections ) );
}
/**
* Version of {@link #construct(Class,Selection[])} taking the
* to-be-constructed type as well as a list of selections.
*
* @param result The result class to be constructed.
* @param selections The selections to use in the constructor call.
*
* @return The <b>view</b> compound selection.
*/
public <Y> CompoundSelection<Y> construct(Class<Y> result, List<Selection<?>> selections) {
checkMultiselect( selections );
return new CompoundSelectionImpl<Y>( this, result, selections );
}
// ordering ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* {@inheritDoc}
*/
public Order asc(Expression<?> x) {
return new OrderImpl( x, true );
}
/**
* {@inheritDoc}
*/
public Order desc(Expression<?> x) {
return new OrderImpl( x, false );
}
// predicates ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public Predicate wrap(Expression<Boolean> expression) {
if ( Predicate.class.isInstance( expression ) ) {
return ( ( Predicate ) expression );
}
else {
return new BooleanExpressionPredicate( this, expression );
}
}
/**
* {@inheritDoc}
*/
public Predicate not(Expression<Boolean> expression) {
return wrap( expression ).negate();
}
/**
* {@inheritDoc}
*/
public Predicate and(Expression<Boolean> x, Expression<Boolean> y) {
return new CompoundPredicate( this, Predicate.BooleanOperator.AND, x, y );
}
/**
* {@inheritDoc}
*/
public Predicate or(Expression<Boolean> x, Expression<Boolean> y) {
return new CompoundPredicate( this, Predicate.BooleanOperator.OR, x, y );
}
/**
* {@inheritDoc}
*/
public Predicate and(Predicate... restrictions) {
return new CompoundPredicate( this, Predicate.BooleanOperator.AND, restrictions );
}
/**
* {@inheritDoc}
*/
public Predicate or(Predicate... restrictions) {
return new CompoundPredicate( this, Predicate.BooleanOperator.OR, restrictions );
}
/**
* {@inheritDoc}
*/
public Predicate conjunction() {
return new CompoundPredicate( this, Predicate.BooleanOperator.AND );
}
/**
* {@inheritDoc}
*/
public Predicate disjunction() {
return new CompoundPredicate( this, Predicate.BooleanOperator.OR );
}
/**
* {@inheritDoc}
*/
public Predicate isTrue(Expression<Boolean> x) {
return new ExplicitTruthValueCheck( this, x, TruthValue.TRUE );
}
/**
* {@inheritDoc}
*/
public Predicate isFalse(Expression<Boolean> x) {
return new ExplicitTruthValueCheck( this, x, TruthValue.FALSE );
}
/**
* {@inheritDoc}
*/
public Predicate isNull(Expression<?> x) {
return new NullnessPredicate( this, x );
}
/**
* {@inheritDoc}
*/
public Predicate isNotNull(Expression<?> x) {
return isNull( x ).negate();
}
/**
* {@inheritDoc}
*/
public Predicate equal(Expression<?> x, Expression<?> y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.EQUAL, x, y );
}
/**
* {@inheritDoc}
*/
public Predicate notEqual(Expression<?> x, Expression<?> y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.NOT_EQUAL, x, y );
}
/**
* {@inheritDoc}
*/
public Predicate equal(Expression<?> x, Object y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.EQUAL, x, y );
}
/**
* {@inheritDoc}
*/
public Predicate notEqual(Expression<?> x, Object y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.NOT_EQUAL, x, y ).negate();
}
/**
* {@inheritDoc}
*/
public <Y extends Comparable<Y>> Predicate greaterThan(Expression<? extends Y> x, Expression<? extends Y> y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN, x, y );
}
/**
* {@inheritDoc}
*/
public <Y extends Comparable<Y>> Predicate lessThan(
Expression<? extends Y> x,
Expression<? extends Y> y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN, x, y );
}
/**
* {@inheritDoc}
*/
public <Y extends Comparable<Y>> Predicate greaterThanOrEqualTo(
Expression<? extends Y> x,
Expression<? extends Y> y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN_OR_EQUAL, x, y );
}
/**
* {@inheritDoc}
*/
public <Y extends Comparable<Y>> Predicate lessThanOrEqualTo(
Expression<? extends Y> x,
Expression<? extends Y> y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN_OR_EQUAL, x, y );
}
/**
* {@inheritDoc}
*/
public <Y extends Comparable<Y>> Predicate greaterThan(
Expression<? extends Y> x,
Y y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN, x, y );
}
/**
* {@inheritDoc}
*/
public <Y extends Comparable<Y>> Predicate lessThan(
Expression<? extends Y> x,
Y y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN, x, y );
}
/**
* {@inheritDoc}
*/
public <Y extends Comparable<Y>> Predicate greaterThanOrEqualTo(
Expression<? extends Y> x,
Y y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN_OR_EQUAL, x, y );
}
/**
* {@inheritDoc}
*/
public <Y extends Comparable<Y>> Predicate lessThanOrEqualTo(
Expression<? extends Y> x,
Y y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN_OR_EQUAL, x, y );
}
/**
* {@inheritDoc}
*/
public Predicate gt(Expression<? extends Number> x, Expression<? extends Number> y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN, x, y );
}
/**
* {@inheritDoc}
*/
public Predicate lt(Expression<? extends Number> x, Expression<? extends Number> y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN, x, y );
}
/**
* {@inheritDoc}
*/
public Predicate ge(Expression<? extends Number> x, Expression<? extends Number> y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN_OR_EQUAL, x, y );
}
/**
* {@inheritDoc}
*/
public Predicate le(Expression<? extends Number> x, Expression<? extends Number> y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN_OR_EQUAL, x, y );
}
/**
* {@inheritDoc}
*/
public Predicate gt(Expression<? extends Number> x, Number y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN, x, y );
}
/**
* {@inheritDoc}
*/
public Predicate lt(Expression<? extends Number> x, Number y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN, x, y );
}
/**
* {@inheritDoc}
*/
public Predicate ge(Expression<? extends Number> x, Number y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN_OR_EQUAL, x, y );
}
/**
* {@inheritDoc}
*/
public Predicate le(Expression<? extends Number> x, Number y) {
//noinspection SuspiciousNameCombination
return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN_OR_EQUAL, x, y );
}
/**
* {@inheritDoc}
*/
public <Y extends Comparable<Y>> Predicate between(
Expression<? extends Y> expression,
Y lowerBound,
Y upperBound) {
return new BetweenPredicate<Y>( this, expression, lowerBound, upperBound );
}
/**
* {@inheritDoc}
*/
public <Y extends Comparable<Y>> Predicate between(
Expression<? extends Y> expression,
Expression<? extends Y> lowerBound,
Expression<? extends Y> upperBound) {
return new BetweenPredicate<Y>( this, expression, lowerBound, upperBound );
}
/**
* {@inheritDoc}
*/
public <T> In<T> in(Expression<? extends T> expression) {
return new InPredicate<T>( this, expression );
}
public <T> In<T> in(Expression<? extends T> expression, Expression<? extends T>... values) {
return new InPredicate<T>( this, expression, values );
}
public <T> In<T> in(Expression<? extends T> expression, T... values) {
return new InPredicate<T>( this, expression, values );
}
public <T> In<T> in(Expression<? extends T> expression, Collection<T> values) {
return new InPredicate<T>( this, expression, values );
}
// parameters ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* {@inheritDoc}
*/
public <T> ParameterExpression<T> parameter(Class<T> paramClass) {
// TODO : AFAICT there is really no way to implement this one correctly.
// the problem is the need to report getPosition...
throw new UnsupportedOperationException( "Note yet implemented!" );
// return new ParameterExpressionImpl( this, paramClass, ??? );
}
/**
* {@inheritDoc}
*/
public <T> ParameterExpression<T> parameter(Class<T> paramClass, String name) {
return new ParameterExpressionImpl<T>( this, paramClass, name );
}
/**
* {@inheritDoc}
*/
public <T> Expression<T> literal(T value) {
return new LiteralExpression<T>( this, value );
}
// aggregate functions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* {@inheritDoc}
*/
public <N extends Number> Expression<Double> avg(Expression<N> x) {
return new AverageAggregrateFunction( this, x );
}
/**
* {@inheritDoc}
*/
public <N extends Number> Expression<N> sum(Expression<N> x) {
return new SumAggregateFunction<N>( this, x );
}
/**
* {@inheritDoc}
*/
public <N extends Number> Expression<N> max(Expression<N> x) {
// TODO : requires a MaxAggregrateFunction
throw new UnsupportedOperationException( "Note yet implemented!" );
}
/**
* {@inheritDoc}
*/
public <N extends Number> Expression<N> min(Expression<N> x) {
// TODO : requires a MaxAggregrateFunction
throw new UnsupportedOperationException( "Note yet implemented!" );
}
/**
* {@inheritDoc}
*/
public <X extends Comparable<X>> Expression<X> greatest(Expression<X> x) {
// TODO : not exactly sure what this should be returning, same as #max ?
throw new UnsupportedOperationException( "Note yet implemented!" );
}
/**
* {@inheritDoc}
*/
public <X extends Comparable<X>> Expression<X> least(Expression<X> x) {
// TODO : not exactly sure what this should be returning, same as #min ?
throw new UnsupportedOperationException( "Note yet implemented!" );
}
/**
* {@inheritDoc}
*/
public Expression<Long> count(Expression<?> x) {
// TODO : CountAggregateFunction
throw new UnsupportedOperationException( "Note yet implemented!" );
}
/**
* {@inheritDoc}
*/
public Expression<Long> countDistinct(Expression<?> x) {
// TODO : CountDistinctAggregateFunction or CountAggregateFunction w/ distinct parameterization
throw new UnsupportedOperationException( "Note yet implemented!" );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public Predicate exists(Subquery<?> subquery) {
return null;
}
public <Y> Expression<Y> all(Subquery<Y> ySubquery) {
return null;
}
public <Y> Expression<Y> some(Subquery<Y> ySubquery) {
return null;
}
public <Y> Expression<Y> any(Subquery<Y> ySubquery) {
return null;
}
public <N extends Number> Expression<N> neg(Expression<N> nExpression) {
return null;
}
public <N extends Number> Expression<N> abs(Expression<N> nExpression) {
return null;
}
public <N extends Number> Expression<N> sum(Expression<? extends N> expression, Expression<? extends N> expression1) {
return null;
}
public <N extends Number> Expression<N> prod(Expression<? extends N> expression, Expression<? extends N> expression1) {
return null;
}
public <N extends Number> Expression<N> diff(Expression<? extends N> expression, Expression<? extends N> expression1) {
return null;
}
public <N extends Number> Expression<N> sum(Expression<? extends N> expression, N n) {
return null;
}
public <N extends Number> Expression<N> prod(Expression<? extends N> expression, N n) {
return null;
}
public <N extends Number> Expression<N> diff(Expression<? extends N> expression, N n) {
return null;
}
public <N extends Number> Expression<N> sum(N n, Expression<? extends N> expression) {
return null;
}
public <N extends Number> Expression<N> prod(N n, Expression<? extends N> expression) {
return null;
}
public <N extends Number> Expression<N> diff(N n, Expression<? extends N> expression) {
return null;
}
public Expression<Number> quot(Expression<? extends Number> expression, Expression<? extends Number> expression1) {
return null;
}
public Expression<Number> quot(Expression<? extends Number> expression, Number number) {
return null;
}
public Expression<Number> quot(Number number, Expression<? extends Number> expression) {
return null;
}
public Expression<Integer> mod(Expression<Integer> integerExpression, Expression<Integer> integerExpression1) {
return null;
}
public Expression<Integer> mod(Expression<Integer> integerExpression, Integer integer) {
return null;
}
public Expression<Integer> mod(Integer integer, Expression<Integer> integerExpression) {
return null;
}
public Expression<Double> sqrt(Expression<? extends Number> expression) {
return null;
}
public Expression<Long> toLong(Expression<? extends Number> expression) {
return null;
}
public Expression<Integer> toInteger(Expression<? extends Number> expression) {
return null;
}
public Expression<Float> toFloat(Expression<? extends Number> expression) {
return null;
}
public Expression<Double> toDouble(Expression<? extends Number> expression) {
return null;
}
public Expression<BigDecimal> toBigDecimal(Expression<? extends Number> expression) {
return null;
}
public Expression<BigInteger> toBigInteger(Expression<? extends Number> expression) {
return null;
}
public Expression<String> toString(Expression<Character> characterExpression) {
return null;
}
public <C extends Collection<?>> Predicate isEmpty(Expression<C> cExpression) {
return null;
}
public <C extends Collection<?>> Predicate isNotEmpty(Expression<C> cExpression) {
return null;
}
public <C extends Collection<?>> Expression<Integer> size(C c) {
return null;
}
public <C extends Collection<?>> Expression<Integer> size(Expression<C> cExpression) {
return null;
}
public <E, C extends Collection<E>> Predicate isMember(E e, Expression<C> cExpression) {
return null;
}
public <E, C extends Collection<E>> Predicate isNotMember(E e, Expression<C> cExpression) {
return null;
}
public <E, C extends Collection<E>> Predicate isMember(Expression<E> eExpression, Expression<C> cExpression) {
return null;
}
public <E, C extends Collection<E>> Predicate isNotMember(Expression<E> eExpression, Expression<C> cExpression) {
return null;
}
public <V, M extends Map<?, V>> Expression<Collection<V>> values(M map) {
return null;
}
public <K, M extends Map<K, ?>> Expression<Set<K>> keys(M m) {
return null;
}
public Predicate like(Expression<String> stringExpression, Expression<String> stringExpression1) {
return null;
}
public Predicate like(Expression<String> stringExpression, Expression<String> stringExpression1, Expression<Character> characterExpression) {
return null;
}
public Predicate like(Expression<String> stringExpression, Expression<String> stringExpression1, char c) {
return null;
}
public Predicate like(Expression<String> stringExpression, String s) {
return null;
}
public Predicate like(Expression<String> stringExpression, String s, Expression<Character> characterExpression) {
return null;
}
public Predicate like(Expression<String> stringExpression, String s, char c) {
return null;
}
public Predicate notLike(Expression<String> stringExpression, Expression<String> stringExpression1) {
return null;
}
public Predicate notLike(Expression<String> stringExpression, Expression<String> stringExpression1, Expression<Character> characterExpression) {
return null;
}
public Predicate notLike(Expression<String> stringExpression, Expression<String> stringExpression1, char c) {
return null;
}
public Predicate notLike(Expression<String> stringExpression, String s) {
return null;
}
public Predicate notLike(Expression<String> stringExpression, String s, Expression<Character> characterExpression) {
return null;
}
public Predicate notLike(Expression<String> stringExpression, String s, char c) {
return null;
}
public Expression<String> concat(Expression<String> stringExpression, Expression<String> stringExpression1) {
return null;
}
public Expression<String> concat(Expression<String> stringExpression, String s) {
return null;
}
public Expression<String> concat(String s, Expression<String> stringExpression) {
return null;
}
public Expression<String> substring(Expression<String> stringExpression, Expression<Integer> integerExpression) {
return null;
}
public Expression<String> substring(Expression<String> stringExpression, int i) {
return null;
}
public Expression<String> substring(Expression<String> stringExpression, Expression<Integer> integerExpression, Expression<Integer> integerExpression1) {
return null;
}
public Expression<String> substring(Expression<String> stringExpression, int i, int i1) {
return null;
}
public Expression<String> trim(Expression<String> stringExpression) {
return null;
}
public Expression<String> trim(Trimspec trimspec, Expression<String> stringExpression) {
return null;
}
public Expression<String> trim(Expression<Character> characterExpression, Expression<String> stringExpression) {
return null;
}
public Expression<String> trim(Trimspec trimspec, Expression<Character> characterExpression, Expression<String> stringExpression) {
return null;
}
public Expression<String> trim(char c, Expression<String> stringExpression) {
return null;
}
public Expression<String> trim(Trimspec trimspec, char c, Expression<String> stringExpression) {
return null;
}
public Expression<String> lower(Expression<String> stringExpression) {
return null;
}
public Expression<String> upper(Expression<String> stringExpression) {
return null;
}
public Expression<Integer> length(Expression<String> stringExpression) {
return null;
}
public Expression<Integer> locate(Expression<String> stringExpression, Expression<String> stringExpression1) {
return null;
}
public Expression<Integer> locate(Expression<String> stringExpression, Expression<String> stringExpression1, Expression<Integer> integerExpression) {
return null;
}
public Expression<Integer> locate(Expression<String> stringExpression, String s) {
return null;
}
public Expression<Integer> locate(Expression<String> stringExpression, String s, int i) {
return null;
}
public Expression<java.sql.Date> currentDate() {
return null;
}
public Expression<java.sql.Timestamp> currentTimestamp() {
return null;
}
public Expression<java.sql.Time> currentTime() {
return null;
}
public <Y> Expression<Y> coalesce(Expression<? extends Y> expression, Expression<? extends Y> expression1) {
return null;
}
public <Y> Expression<Y> coalesce(Expression<? extends Y> expression, Y y) {
return null;
}
public <Y> Expression<Y> nullif(Expression<Y> yExpression, Expression<?> expression) {
return null;
}
public <Y> Expression<Y> nullif(Expression<Y> yExpression, Y y) {
return null;
}
public <T> Coalesce<T> coalesce() {
return null;
}
public <C, R> SimpleCase<C, R> selectCase(Expression<? extends C> expression) {
return null;
}
public <R> Case<R> selectCase() {
return null;
}
public <T> Expression<T> function(String s, Class<T> tClass, Expression<?>... expressions) {
return null;
}
}

View File

@ -0,0 +1,158 @@
/*
* 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 java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.Collections;
import javax.persistence.criteria.AbstractQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Selection;
import javax.persistence.criteria.Subquery;
import javax.persistence.metamodel.EntityType;
/**
* Models basic query structure. Used as a delegate in implementing both
* {@link org.hibernate.criterion.CriteriaQuery} and
* {@link javax.persistence.criteria.Subquery}.
* <p/>
* Note the <tt>ORDER BY</tt> specs are neglected here. That's because it is not valid
* for a subquery to define an <tt>ORDER BY</tt> clause. So we just handle them on the
* root query directly...
*
* @author Steve Ebersole
*/
public class QueryStructure<T> {
private final AbstractQuery<T> owner;
private final QueryBuilderImpl queryBuilder;
public QueryStructure(AbstractQuery<T> owner, QueryBuilderImpl queryBuilder) {
this.owner = owner;
this.queryBuilder = queryBuilder;
}
private boolean distinction;
private Selection<? extends T> selection;
private Set<Root<?>> roots = new HashSet<Root<?>>();
private Predicate restriction;
private List<Expression<?>> groupings = Collections.emptyList();
private Predicate having;
private List<Subquery<?>> subqueries;
// SELECTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public boolean isDistinction() {
return distinction;
}
public void setDistinction(boolean distinction) {
this.distinction = distinction;
}
public Selection<? extends T> getSelection() {
return selection;
}
public void setSelection(Selection<? extends T> selection) {
this.selection = selection;
}
// ROOTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public Set<Root<?>> getRoots() {
return roots;
}
public <X> Root<X> from(Class<X> entityClass) {
EntityType<X> entityType = queryBuilder.getEntityManagerFactory()
.getMetamodel()
.entity( entityClass );
if ( entityType == null ) {
throw new IllegalArgumentException( entityClass + " is not an entity" );
}
return from( entityType );
}
public <X> Root<X> from(EntityType<X> entityType) {
RootImpl<X> root = new RootImpl( queryBuilder, entityType );
roots.add( root );
return root;
}
// RESTRICTIONS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public Predicate getRestriction() {
return restriction;
}
public void setRestriction(Predicate restriction) {
this.restriction = restriction;
}
// GROUPINGS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public List<Expression<?>> getGroupings() {
return groupings;
}
public void setGroupings(List<Expression<?>> groupings) {
this.groupings = groupings;
}
public void setGroupings(Expression<?>... groupings) {
if ( groupings != null && groupings.length > 0 ) {
this.groupings = Arrays.asList( groupings );
}
else {
this.groupings = Collections.emptyList();
}
}
public Predicate getHaving() {
return having;
}
public void setHaving(Predicate having) {
this.having = having;
}
// SUBQUERIES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public List<Subquery<?>> getSubqueries() {
return subqueries;
}
public <U> Subquery<U> subquery(Class<U> subqueryType) {
CriteriaSubqueryImpl<U> subquery = new CriteriaSubqueryImpl<U>( queryBuilder, subqueryType, owner );
subqueries.add( subquery );
return subquery;
}
}

View File

@ -0,0 +1,44 @@
/*
* 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.Root;
import javax.persistence.metamodel.EntityType;
/**
* Defines a <tt>query root</tt>.
*
* @author Steve Ebersole
*/
public class RootImpl<X> extends FromImpl<X,X> implements Root<X> {
public RootImpl(
QueryBuilderImpl queryBuilder,
EntityType<X> model) {
super( queryBuilder, model );
}
@Override
public EntityType<X> getModel(){
return ( EntityType<X> ) super.getModel();
}
}

View File

@ -0,0 +1,89 @@
/*
* 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.expression;
import java.util.Collection;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class AbstractExpression<T> extends SelectionImpl<T> implements Expression<T> {
protected AbstractExpression(QueryBuilderImpl queryBuilder, Class<T> javaType) {
super( queryBuilder, javaType );
}
/**
* {@inheritDoc}
*/
public <X> Expression<X> as(Class<X> type) {
// TODO-STEVE : implement - needs a cast expression
throw new UnsupportedOperationException( "Not yet implemented!" );
}
/**
* {@inheritDoc}
*/
public Predicate isNull() {
return queryBuilder().isNull( this );
}
/**
* {@inheritDoc}
*/
public Predicate isNotNull() {
return queryBuilder().isNotNull( this );
}
/**
* {@inheritDoc}
*/
public Predicate in(Object... values) {
return queryBuilder().in( this, values );
}
/**
* {@inheritDoc}
*/
public Predicate in(Expression<?>... values) {
return queryBuilder().in( this, values );
}
/**
* {@inheritDoc}
*/
public Predicate in(Collection<?> values) {
return queryBuilder().in( this, values );
}
/**
* {@inheritDoc}
*/
public Predicate in(Expression<Collection<?>> values) {
return queryBuilder().in( this, values );
}
}

View File

@ -0,0 +1,65 @@
/*
* 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.expression;
import javax.persistence.TupleElement;
import org.hibernate.ejb.criteria.AbstractNode;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public abstract class AbstractTupleElement<X> extends AbstractNode implements TupleElement<X> {
private final Class<X> javaType;
private String alias;
protected AbstractTupleElement(QueryBuilderImpl queryBuilder, Class<X> javaType) {
super( queryBuilder );
this.javaType = javaType;
}
/**
* {@inheritDoc}
*/
public Class<X> getJavaType() {
return javaType;
}
/**
* {@inheritDoc}
*/
public String getAlias() {
return alias;
}
/**
* Protected access to define the alias.
*
* @param alias The alias to use.
*/
protected void setAlias(String alias) {
this.alias = alias;
}
}

View File

@ -0,0 +1,56 @@
/*
* 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.expression;
import java.util.List;
import javax.persistence.criteria.CompoundSelection;
import javax.persistence.criteria.Selection;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* The Hibernate implementation of the JPA {@link CompoundSelection}
* contract.
*
* @author Steve Ebersole
*/
public class CompoundSelectionImpl<X> extends SelectionImpl<X> implements CompoundSelection<X> {
private List<Selection<?>> selectionItems;
public CompoundSelectionImpl(
QueryBuilderImpl queryBuilder,
Class<X> javaType,
List<Selection<?>> selectionItems) {
super( queryBuilder, javaType );
this.selectionItems = selectionItems;
}
@Override
public boolean isCompoundSelection() {
return true;
}
@Override
public List<Selection<?>> getCompoundSelectionItems() {
return selectionItems;
}
}

View File

@ -0,0 +1,35 @@
/*
* 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.expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class EntityTypeExpression<T> extends AbstractExpression<T> {
public EntityTypeExpression(QueryBuilderImpl queryBuilder, Class<T> javaType) {
super( queryBuilder, javaType );
}
}

View File

@ -0,0 +1,66 @@
/*
* 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.expression;
import java.util.List;
import java.util.Arrays;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class FunctionExpressionImpl<X> extends AbstractExpression<X> implements Expression<X> {
private final String functionName;
private final List<Expression<?>> argumentExpressions;
public FunctionExpressionImpl(
QueryBuilderImpl queryBuilder,
Class<X> javaType,
String functionName,
List<Expression<?>> argumentExpressions) {
super( queryBuilder, javaType );
this.functionName = functionName;
this.argumentExpressions = argumentExpressions;
}
public FunctionExpressionImpl(
QueryBuilderImpl queryBuilder,
Class<X> javaType,
String functionName,
Expression<?>... argumentExpressions) {
super( queryBuilder, javaType );
this.functionName = functionName;
this.argumentExpressions = Arrays.asList( argumentExpressions );
}
public String getFunctionName() {
return functionName;
}
public List<Expression<?>> getArgumentExpressions() {
return argumentExpressions;
}
}

View File

@ -0,0 +1,45 @@
/*
* 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.expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class LiteralExpression<T> extends AbstractExpression<T> {
private final T literal;
public LiteralExpression(
QueryBuilderImpl queryBuilder,
T literal) {
//noinspection unchecked
super( queryBuilder, ( Class<T> ) literal.getClass() );
this.literal = literal;
}
public T getLiteral() {
return literal;
}
}

View File

@ -0,0 +1,63 @@
/*
* 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.expression;
import javax.persistence.criteria.ParameterExpression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* Defines a parameter specification, or the information about a parameter (where it occurs, what is
* its type, etc).
*
* @author Steve Ebersole
*/
public class ParameterExpressionImpl<T> extends AbstractExpression<T> implements ParameterExpression<T> {
private final String name;
private final Integer position;
public ParameterExpressionImpl(
QueryBuilderImpl queryBuilder,
Class<T> javaType,
String name) {
super( queryBuilder, javaType );
this.name = name;
this.position = null;
}
public ParameterExpressionImpl(
QueryBuilderImpl queryBuilder,
Class<T> javaType,
Integer position) {
super( queryBuilder, javaType );
this.name = null;
this.position = position;
}
public String getName() {
return name;
}
public Integer getPosition() {
return position;
}
}

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.expression;
import java.util.List;
import javax.persistence.criteria.Selection;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* The Hibernate implementation of the JPA {@link Selection}
* contract.
*
* @author Steve Ebersole
*/
public class SelectionImpl<X> extends AbstractTupleElement<X> implements Selection<X> {
public SelectionImpl(QueryBuilderImpl queryBuilder, Class<X> javaType) {
super( queryBuilder, javaType );
}
public Selection<X> alias(String alias) {
setAlias( alias );
return this;
}
public boolean isCompoundSelection() {
return false;
}
public List<Selection<?>> getCompoundSelectionItems() {
throw new IllegalStateException( "Not a compund selection" );
}
}

View File

@ -0,0 +1,66 @@
/*
* 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.predicate;
import java.util.List;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Selection;
import org.hibernate.ejb.criteria.expression.AbstractExpression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* Basic template support for {@link Predicate} implementors providing
* expression handling, negation and conjunction/disjunction handling.
*
* @author Steve Ebersole
*/
public abstract class AbstractPredicateImpl extends AbstractExpression<Boolean> implements Predicate {
private boolean negated;
protected AbstractPredicateImpl(QueryBuilderImpl queryBuilder) {
super( queryBuilder, Boolean.class );
}
public boolean isNegated() {
return negated;
}
public Predicate negate() {
negated = !negated;
return this;
}
// Selection ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public final boolean isCompoundSelection() {
// Should always be false for predicates
return super.isCompoundSelection();
}
public final List<Selection<?>> getCompoundSelectionItems() {
// Should never have sub selection items for predicates
return super.getCompoundSelectionItems();
}
}

View File

@ -0,0 +1,50 @@
/*
* 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.predicate;
import java.util.List;
import java.util.Collections;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class AbstractSimplePredicate extends AbstractPredicateImpl {
private static final List<Expression<Boolean>> NO_EXPRESSIONS = Collections.emptyList();
public AbstractSimplePredicate(QueryBuilderImpl queryBuilder) {
super( queryBuilder );
}
public BooleanOperator getOperator() {
return BooleanOperator.AND;
}
public List<Expression<Boolean>> getExpressions() {
return NO_EXPRESSIONS;
}
}

View File

@ -0,0 +1,73 @@
/*
* 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.predicate;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* Models a <tt>BETWEEN</tt> {@link javax.persistence.criteria.Predicate}.
*
* @author Steve Ebersole
*/
public class BetweenPredicate<Y> extends AbstractSimplePredicate {
private final Expression<? extends Y> expression;
private final Expression<? extends Y> lowerBound;
private final Expression<? extends Y> upperBound;
public BetweenPredicate(
QueryBuilderImpl queryBuilder,
Expression<? extends Y> expression,
Y lowerBound,
Y upperBound) {
this(
queryBuilder,
expression,
queryBuilder.literal( lowerBound ),
queryBuilder.literal( upperBound )
);
}
public BetweenPredicate(
QueryBuilderImpl queryBuilder,
Expression<? extends Y> expression,
Expression<? extends Y> lowerBound,
Expression<? extends Y> upperBound) {
super( queryBuilder );
this.expression = expression;
this.lowerBound = lowerBound;
this.upperBound = upperBound;
}
public Expression<? extends Y> getExpression() {
return expression;
}
public Expression<? extends Y> getLowerBound() {
return lowerBound;
}
public Expression<? extends Y> getUpperBound() {
return upperBound;
}
}

View File

@ -0,0 +1,50 @@
/*
* 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.predicate;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* Defines a {@link Predicate} used to wrap an {@link Expression Expression&lt;Boolean&gt;}.
*
* @author Steve Ebersole
*/
public class BooleanExpressionPredicate extends AbstractSimplePredicate {
private final Expression<Boolean> expression;
public BooleanExpressionPredicate(QueryBuilderImpl queryBuilder, Expression<Boolean> expression) {
super( queryBuilder );
this.expression = expression;
}
/**
* Get the boolean expression defining the predicate.
*
* @return The underlying boolean expression.
*/
public Expression<Boolean> getExpression() {
return expression;
}
}

View File

@ -0,0 +1,112 @@
/*
* 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.predicate;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class ComparisonPredicate extends AbstractSimplePredicate {
private final ComparisonOperator comparisonOperator;
private final Expression<?> leftHandSide;
private final Expression<?> rightHandSide;
public ComparisonPredicate(
QueryBuilderImpl queryBuilder,
ComparisonOperator comparisonOperator,
Expression<?> leftHandSide,
Expression<?> rightHandSide) {
super( queryBuilder );
this.comparisonOperator = comparisonOperator;
this.leftHandSide = leftHandSide;
this.rightHandSide = rightHandSide;
}
public ComparisonPredicate(
QueryBuilderImpl queryBuilder,
ComparisonOperator comparisonOperator,
Expression<?> leftHandSide,
Object rightHandSide) {
super( queryBuilder );
this.comparisonOperator = comparisonOperator;
this.leftHandSide = leftHandSide;
// TODO : generate a parameter expression once those are ready...
this.rightHandSide = null;
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public ComparisonOperator getComparisonOperator() {
return comparisonOperator;
}
public Expression<?> getLeftHandSide() {
return leftHandSide;
}
public Expression<?> getRightHandSide() {
return rightHandSide;
}
/**
* Defines the comparison operators. We could also get away with
* only 3 and use negation...
*/
public static enum ComparisonOperator {
EQUAL {
public ComparisonOperator negated() {
return NOT_EQUAL;
}
},
NOT_EQUAL {
public ComparisonOperator negated() {
return EQUAL;
}
},
LESS_THAN {
public ComparisonOperator negated() {
return GREATER_THAN_OR_EQUAL;
}
},
LESS_THAN_OR_EQUAL {
public ComparisonOperator negated() {
return GREATER_THAN;
}
},
GREATER_THAN {
public ComparisonOperator negated() {
return LESS_THAN_OR_EQUAL;
}
},
GREATER_THAN_OR_EQUAL {
public ComparisonOperator negated() {
return LESS_THAN;
}
};
public abstract ComparisonOperator negated();
}
}

View File

@ -0,0 +1,102 @@
/*
* 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.predicate;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* A compound {@link Predicate predicate} is a grouping of other {@link Predicate predicates} in order to apply
* either a conjunction (logical AND) or a disjunction (logical OR).
*
* @author Steve Ebersole
*/
public class CompoundPredicate extends AbstractPredicateImpl {
private final BooleanOperator operator;
private final List<Expression<Boolean>> expressions = new ArrayList<Expression<Boolean>>();
/**
* Constructs an empty conjunction or disjunction.
*
* @param queryBuilder The query builder from whcih this originates.
* @param operator Indicates whether this predicate will funtion
* as a conjunction or disjuntion.
*/
public CompoundPredicate(QueryBuilderImpl queryBuilder, BooleanOperator operator) {
super( queryBuilder );
this.operator = operator;
}
/**
* Constructs a conjunction or disjunction over the given expressions.
*
* @param queryBuilder The query builder from which this originates.
* @param operator Indicates whether this predicate will funtion
* as a conjunction or disjuntion.
* @param expressions The expressions to be grouped.
*/
public CompoundPredicate(
QueryBuilderImpl queryBuilder,
BooleanOperator operator,
Expression<Boolean>... expressions) {
this( queryBuilder, operator );
applyExpressions( expressions );
}
/**
* Constructs a conjunction or disjunction over the given expressions.
*
* @param queryBuilder The query builder from whcih this originates.
* @param operator Indicates whether this predicate will funtion
* as a conjunction or disjuntion.
* @param expressions The expressions to be grouped.
*/
public CompoundPredicate(
QueryBuilderImpl queryBuilder,
BooleanOperator operator,
List<Expression<Boolean>> expressions) {
this( queryBuilder, operator );
applyExpressions( expressions );
}
private void applyExpressions(Expression<Boolean>... expressions) {
applyExpressions( Arrays.asList( expressions ) );
}
private void applyExpressions(List<Expression<Boolean>> expressions) {
this.expressions.clear();
this.expressions.addAll( expressions );
}
public BooleanOperator getOperator() {
return operator;
}
public List<Expression<Boolean>> getExpressions() {
return expressions;
}
}

View File

@ -0,0 +1,57 @@
/*
* 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.predicate;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* ANSI-SQL defines <tt>TRUE</tt>, <tt>FALSE</tt> and <tt>UNKNOWN</tt> as <i>truth values</i>. These
* <i>truth values</i> are used to explicitly check the result of a boolean expression (the syntax is like
* <tt>a > b IS TRUE</tt>. <tt>IS TRUE</tt> is the assumed default.
* <p/>
* JPA defines support for only <tt>IS TRUE</tt> and <tt>IS FALSE</tt>, not <tt>IS UNKNOWN</tt> (<tt>a > NULL</tt>
* is an example where the result would be UNKNOWN.
*
* @author Steve Ebersole
*/
public class ExplicitTruthValueCheck extends AbstractSimplePredicate {
// TODO : given that JPA supports only TRUE and FALSE, can this be handled just with negation?
private final Expression<Boolean> booleanExpression;
private final TruthValue truthValue;
public ExplicitTruthValueCheck(QueryBuilderImpl queryBuilder, Expression<Boolean> booleanExpression, TruthValue truthValue) {
super( queryBuilder );
this.booleanExpression = booleanExpression;
this.truthValue = truthValue;
}
public Expression<Boolean> getBooleanExpression() {
return booleanExpression;
}
public TruthValue getTruthValue() {
return truthValue;
}
}

View File

@ -0,0 +1,137 @@
/*
* 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.predicate;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class InPredicate<T> extends AbstractSimplePredicate implements QueryBuilderImpl.In<T> {
private final Expression<? extends T> expression;
private final List<Expression<? extends T>> values;
/**
* Constructs an <tt>IN</tt> predicate against a given expression with an empty list of values.
*
* @param queryBuilder The query builder from which this originates.
* @param expression The expression.
*/
public InPredicate(
QueryBuilderImpl queryBuilder,
Expression<? extends T> expression) {
this( queryBuilder, expression, new ArrayList<Expression<? extends T>>() );
}
/**
* Constructs an <tt>IN</tt> predicate against a given expression with the given list of expression values.
*
* @param queryBuilder The query builder from which this originates.
* @param expression The expression.
* @param values The value list.
*/
public InPredicate(
QueryBuilderImpl queryBuilder,
Expression<? extends T> expression,
Expression<? extends T>... values) {
this( queryBuilder, expression, Arrays.asList( values ) );
}
/**
* Constructs an <tt>IN</tt> predicate against a given expression with the given list of expression values.
*
* @param queryBuilder The query builder from which this originates.
* @param expression The expression.
* @param values The value list.
*/
public InPredicate(
QueryBuilderImpl queryBuilder,
Expression<? extends T> expression,
List<Expression<? extends T>> values) {
super( queryBuilder );
this.expression = expression;
this.values = values;
}
/**
* Constructs an <tt>IN</tt> predicate against a given expression with the given given literal value list.
*
* @param queryBuilder The query builder from which this originates.
* @param expression The expression.
* @param values The value list.
*/
public InPredicate(
QueryBuilderImpl queryBuilder,
Expression<? extends T> expression,
T... values) {
super( queryBuilder );
// TODO : implements - needs parameter expressions
throw new UnsupportedOperationException( "Not yet implemented!" );
}
/**
* Constructs an <tt>IN</tt> predicate against a given expression with the given literal value list.
*
* @param queryBuilder The query builder from which this originates.
* @param expression The expression.
* @param values The value list.
*/
public InPredicate(
QueryBuilderImpl queryBuilder,
Expression<? extends T> expression,
Collection<T> values) {
super( queryBuilder );
// TODO : implements - needs parameter expressions
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public Expression<T> getExpression() {
//noinspection unchecked
return ( Expression<T> ) expression;
}
public Expression<? extends T> getExpressionInternal() {
return expression;
}
public List<Expression<? extends T>> getValues() {
return values;
}
public InPredicate<T> value(T value) {
// TODO : implements - needs parameter expressions
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public InPredicate<T> value(Expression<? extends T> value) {
values.add( value );
return this;
}
}

View File

@ -0,0 +1,56 @@
/*
* 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.predicate;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* Defines a {@link javax.persistence.criteria.Predicate} for checking the
* nullness state of an expression, aka an <tt>IS (NOT?) NULL</tt> predicate.
* <p/>
* The <tt>NOT NULL</tt> form can be built by calling the constructor and then
* calling {@link #negate}.
*
* @author Steve Ebersole
*/
public class NullnessPredicate extends AbstractSimplePredicate{
private final Expression<?> nullnessCheckExpression;
/**
* Constructs the affirmitive form of nullness checking (<i>IS NULL</i>). To
* construct the negative form (<i>IS NOT NULL</i>) call {@link #negate} on the
* constructed instance.
*
* @param queryBuilder The query builder from whcih this originates.
* @param expression The expression to check.
*/
public NullnessPredicate(QueryBuilderImpl queryBuilder, Expression<?> expression) {
super( queryBuilder );
this.nullnessCheckExpression = expression;
}
public Expression<?> getNullnessCheckExpression() {
return nullnessCheckExpression;
}
}

View File

@ -0,0 +1,38 @@
/*
* 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.predicate;
/**
* Models what ANSI-SQL terms a <i>truth value</i>. Specifically, ANSI-SQL defines <tt>TRUE</tt>, <tt>FALSE</tt> and
* <tt>UNKNOWN</tt> as <i>truth values</i>. These <i>truth values</i> are used to explicitly check the result of a
* boolean expression (the syntax is like <tt>a > b IS TRUE</tt>. <tt>IS TRUE</tt> is the assumed default.
* <p/>
* JPA defines support for only <tt>IS TRUE</tt> and <tt>IS FALSE</tt>, not <tt>IS UNKNOWN</tt> (<tt>a > NULL</tt>
* is an example where the result would be UNKNOWN). All 3 are provided here for completness.
*
* @author Steve Ebersole
*/
public enum TruthValue {
TRUE,
FALSE,
UNKNOWN
}

View File

@ -0,0 +1,48 @@
/*
* 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.basic;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery;
import org.hibernate.ejb.test.TestCase;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class BasicCriteriaUsageTest extends TestCase {
public Class[] getAnnotatedClasses() {
return new Class[] { Wall.class };
}
public void testSimpliestCriterias() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery criteria = em.getQueryBuilder().createQuery();
criteria.from( Wall.class );
em.getTransaction().commit();
em.close();
}
}

View File

@ -0,0 +1,99 @@
/*
* 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.basic;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.ManyToOne;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
@Entity
@Table( name = "crit_basic_wall" )
public class Wall {
private Long id;
private long width;
private long height;
private String color;
private Wall left;
private Wall right;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public long getWidth() {
return width;
}
public void setWidth(long width) {
this.width = width;
}
public long getHeight() {
return height;
}
public void setHeight(long height) {
this.height = height;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@ManyToOne
@JoinColumn(name = "left_id")
public Wall getLeft() {
return left;
}
public void setLeft(Wall left) {
this.left = left;
}
@ManyToOne
@JoinColumn(name = "right_id")
public Wall getRight() {
return right;
}
public void setRight(Wall right) {
this.right = right;
}
}