EJB-447 : Implement JPA 2.0 criteria apis

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17264 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2009-08-11 19:16:08 +00:00
parent 8de99d279d
commit ed6346174d
4 changed files with 128 additions and 25 deletions

View File

@ -38,7 +38,8 @@ import javax.persistence.metamodel.EntityType;
import org.hibernate.ejb.criteria.expression.ExpressionImpl;
/**
* TODO : javadoc
* The Hibernate implementation of the JPA {@link Subquery} contract. Mostlty a set of delegation to its internal
* {@link QueryStructure}.
*
* @author Steve Ebersole
*/

View File

@ -813,9 +813,11 @@ public class QueryBuilderImpl implements QueryBuilder, Serializable {
* {@inheritDoc}
*/
public <N extends Number> Expression<N> sum(Expression<? extends N> expression1, Expression<? extends N> expression2) {
Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType( (Class)Number.class, (Expression)expression1 );
type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, (Expression)expression2 );
return new BinaryArithmeticOperation<N>(
this,
(Class<N>) expression1.getJavaType(),
type,
BinaryArithmeticOperation.Operation.ADD,
expression1,
expression2
@ -826,9 +828,11 @@ public class QueryBuilderImpl implements QueryBuilder, Serializable {
* {@inheritDoc}
*/
public <N extends Number> Expression<N> prod(Expression<? extends N> expression1, Expression<? extends N> expression2) {
Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType( (Class)Number.class, (Expression)expression1 );
type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, (Expression)expression2 );
return new BinaryArithmeticOperation<N>(
this,
(Class<N>) expression1.getJavaType(),
type,
BinaryArithmeticOperation.Operation.MULTIPLY,
expression1,
expression2
@ -839,6 +843,8 @@ public class QueryBuilderImpl implements QueryBuilder, Serializable {
* {@inheritDoc}
*/
public <N extends Number> Expression<N> diff(Expression<? extends N> expression1, Expression<? extends N> expression2) {
Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType( (Class)Number.class, (Expression)expression1 );
type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, (Expression)expression2 );
return new BinaryArithmeticOperation<N>(
this,
(Class<N>) expression1.getJavaType(),
@ -852,9 +858,11 @@ public class QueryBuilderImpl implements QueryBuilder, Serializable {
* {@inheritDoc}
*/
public <N extends Number> Expression<N> sum(Expression<? extends N> expression, N n) {
Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType( (Class)Number.class, (Expression)expression );
type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, n );
return new BinaryArithmeticOperation<N>(
this,
(Class<N>) expression.getJavaType(),
type,
BinaryArithmeticOperation.Operation.ADD,
expression,
n
@ -865,9 +873,11 @@ public class QueryBuilderImpl implements QueryBuilder, Serializable {
* {@inheritDoc}
*/
public <N extends Number> Expression<N> prod(Expression<? extends N> expression, N n) {
Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType( (Class)Number.class, (Expression)expression );
type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, n );
return new BinaryArithmeticOperation<N>(
this,
(Class<N>) expression.getJavaType(),
type,
BinaryArithmeticOperation.Operation.MULTIPLY,
expression,
n
@ -878,9 +888,11 @@ public class QueryBuilderImpl implements QueryBuilder, Serializable {
* {@inheritDoc}
*/
public <N extends Number> Expression<N> diff(Expression<? extends N> expression, N n) {
Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType( (Class)Number.class, (Expression)expression );
type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, n );
return new BinaryArithmeticOperation<N>(
this,
(Class<N>) expression.getJavaType(),
type,
BinaryArithmeticOperation.Operation.SUBTRACT,
expression,
n
@ -891,9 +903,11 @@ public class QueryBuilderImpl implements QueryBuilder, Serializable {
* {@inheritDoc}
*/
public <N extends Number> Expression<N> sum(N n, Expression<? extends N> expression) {
Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType( (Class)Number.class, (Expression)expression );
type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, n );
return new BinaryArithmeticOperation<N>(
this,
(Class<N>) expression.getJavaType(),
type,
BinaryArithmeticOperation.Operation.ADD,
n,
expression
@ -904,9 +918,11 @@ public class QueryBuilderImpl implements QueryBuilder, Serializable {
* {@inheritDoc}
*/
public <N extends Number> Expression<N> prod(N n, Expression<? extends N> expression) {
Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType( (Class)Number.class, (Expression)expression );
type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, n );
return new BinaryArithmeticOperation<N>(
this,
(Class<N>) expression.getJavaType(),
type,
BinaryArithmeticOperation.Operation.MULTIPLY,
n,
expression
@ -917,9 +933,11 @@ public class QueryBuilderImpl implements QueryBuilder, Serializable {
* {@inheritDoc}
*/
public <N extends Number> Expression<N> diff(N n, Expression<? extends N> expression) {
Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType( (Class)Number.class, (Expression)expression );
type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, n );
return new BinaryArithmeticOperation<N>(
this,
(Class<N>) expression.getJavaType(),
type,
BinaryArithmeticOperation.Operation.SUBTRACT,
n,
expression
@ -930,24 +948,39 @@ public class QueryBuilderImpl implements QueryBuilder, Serializable {
* {@inheritDoc}
*/
public Expression<Number> quot(Expression<? extends Number> expression1, Expression<? extends Number> expression2) {
// TODO : still open question whether this should be a quotient (integer division) or division
throw new UnsupportedOperationException( "Not yet implemented!" );
return new BinaryArithmeticOperation<Number>(
this,
Number.class,
BinaryArithmeticOperation.Operation.DIVIDE,
expression1,
expression2
);
}
/**
* {@inheritDoc}
*/
public Expression<Number> quot(Expression<? extends Number> expression, Number number) {
// TODO : still open question whether this should be a quotient (integer division) or division
throw new UnsupportedOperationException( "Not yet implemented!" );
return new BinaryArithmeticOperation<Number>(
this,
Number.class,
BinaryArithmeticOperation.Operation.DIVIDE,
expression,
number
);
}
/**
* {@inheritDoc}
*/
public Expression<Number> quot(Number number, Expression<? extends Number> expression) {
// TODO : still open question whether this should be a quotient (integer division) or division
throw new UnsupportedOperationException( "Not yet implemented!" );
return new BinaryArithmeticOperation<Number>(
this,
Number.class,
BinaryArithmeticOperation.Operation.DIVIDE,
number,
expression
);
}
/**

View File

@ -23,7 +23,6 @@ package org.hibernate.ejb.criteria.expression;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.ParameterContainer;
import org.hibernate.ejb.criteria.ParameterRegistry;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
@ -44,18 +43,66 @@ public class BinaryArithmeticOperation<N extends Number>
private final Expression<? extends N> rhs;
private final Expression<? extends N> lhs;
/**
* Helper for determining the appropriate operation return type based on one of the operands as an expression.
*
* @param defaultType The default return type to use if we cannot determine the java type of 'expression' operand.
* @param expression The operand.
*
* @return The appropriate return type.
*/
public static Class<? extends Number> determineReturnType(
Class<? extends Number> defaultType,
Expression<? extends Number> expression) {
return expression == null || expression.getJavaType() == null
? defaultType
: expression.getJavaType();
}
/**
* Helper for determining the appropriate operation return type based on one of the operands as a literal.
*
* @param defaultType The default return type to use if we cannot determine the java type of 'numberLiteral' operand.
* @param numberLiteral The operand.
*
* @return The appropriate return type.
*/
public static Class<? extends Number> determineReturnType(
Class<? extends Number> defaultType,
Number numberLiteral) {
return numberLiteral == null ? defaultType : numberLiteral.getClass();
}
/**
* Creates an arithmethic operation based on 2 expressions.
*
* @param queryBuilder The builder for query components.
* @param resultType The operation result type
* @param operator The operator (type of operation).
* @param rhs The right-hand operand
* @param lhs The left-hand operand.
*/
public BinaryArithmeticOperation(
QueryBuilderImpl queryBuilder,
Class<N> javaType,
Class<N> resultType,
Operation operator,
Expression<? extends N> rhs,
Expression<? extends N> lhs) {
super( queryBuilder, javaType );
super( queryBuilder, resultType );
this.operator = operator;
this.rhs = rhs;
this.lhs = lhs;
}
/**
* Creates an arithmethic operation based on an expression and a literal.
*
* @param queryBuilder The builder for query components.
* @param resultType The operation result type
* @param operator The operator (type of operation).
* @param rhs The right-hand operand
* @param lhs The left-hand operand (the literal).
*/
public BinaryArithmeticOperation(
QueryBuilderImpl queryBuilder,
Class<N> javaType,
@ -68,6 +115,15 @@ public class BinaryArithmeticOperation<N extends Number>
this.lhs = new LiteralExpression<N>( queryBuilder, lhs );
}
/**
* Creates an arithmethic operation based on an expression and a literal.
*
* @param queryBuilder The builder for query components.
* @param resultType The operation result type
* @param operator The operator (type of operation).
* @param rhs The right-hand operand (the literal).
* @param lhs The left-hand operand
*/
public BinaryArithmeticOperation(
QueryBuilderImpl queryBuilder,
Class<N> javaType,
@ -79,19 +135,27 @@ public class BinaryArithmeticOperation<N extends Number>
this.rhs = new LiteralExpression<N>( queryBuilder, rhs );
this.lhs = lhs;
}
public Operation getOperator() {
return operator;
}
/**
* {@inheritDoc}
*/
public Expression<? extends N> getRightHandOperand() {
return rhs;
}
/**
* {@inheritDoc}
*/
public Expression<? extends N> getLeftHandOperand() {
return lhs;
}
/**
* {@inheritDoc}
*/
public void registerParameters(ParameterRegistry registry) {
Helper.possibleParameter( getRightHandOperand(), registry );
Helper.possibleParameter( getLeftHandOperand(), registry );

View File

@ -1,12 +1,11 @@
package org.hibernate.ejb.criteria.expression;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.ParameterContainer;
import org.hibernate.ejb.criteria.ParameterRegistry;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* Models arithmetc operations with two operands.
* Models unary arithmetic operation (unary plus and unary minus).
*
* @author Steve Ebersole
*/
@ -30,14 +29,20 @@ public class UnaryArithmeticOperation<T>
this.operand = operand;
}
public Expression<T> getOperand() {
return operand;
}
public Operation getOperation() {
return operation;
}
/**
* {@inheritDoc}
*/
public Expression<T> getOperand() {
return operand;
}
/**
* {@inheritDoc}
*/
public void registerParameters(ParameterRegistry registry) {
Helper.possibleParameter( getOperand(), registry );
}