HHH-4785 - BinaryArithmeticOperation reverses incoming arguments

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18538 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2010-01-13 12:47:55 +00:00
parent 4d86564d32
commit 8ebda8540a
2 changed files with 111 additions and 28 deletions

View File

@ -115,40 +115,19 @@ public class BinaryArithmeticOperation<N extends Number>
* @param criteriaBuilder 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.
* @param rhs The right-hand operand
*/
public BinaryArithmeticOperation(
CriteriaBuilderImpl criteriaBuilder,
Class<N> resultType,
Operation operator,
Expression<? extends N> rhs,
Expression<? extends N> lhs) {
Expression<? extends N> lhs,
Expression<? extends N> rhs) {
super( criteriaBuilder, resultType );
this.operator = operator;
this.rhs = rhs;
this.lhs = lhs;
}
/**
* Creates an arithmethic operation based on an expression and a literal.
*
* @param criteriaBuilder The builder for query components.
* @param javaType 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(
CriteriaBuilderImpl criteriaBuilder,
Class<N> javaType,
Operation operator,
Expression<? extends N> rhs,
N lhs) {
super( criteriaBuilder, javaType );
this.operator = operator;
this.rhs = rhs;
this.lhs = new LiteralExpression<N>( criteriaBuilder, lhs );
}
/**
@ -157,19 +136,40 @@ public class BinaryArithmeticOperation<N extends Number>
* @param criteriaBuilder The builder for query components.
* @param javaType 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
* @param rhs The right-hand operand (the literal)
*/
public BinaryArithmeticOperation(
CriteriaBuilderImpl criteriaBuilder,
Class<N> javaType,
Operation operator,
N rhs,
Expression<? extends N> lhs) {
Expression<? extends N> lhs,
N rhs) {
super( criteriaBuilder, javaType );
this.operator = operator;
this.rhs = new LiteralExpression<N>( criteriaBuilder, rhs );
this.lhs = lhs;
this.rhs = new LiteralExpression<N>( criteriaBuilder, rhs );
}
/**
* Creates an arithmetic operation based on an expression and a literal.
*
* @param criteriaBuilder The builder for query components.
* @param javaType The operation result type
* @param operator The operator (type of operation).
* @param lhs The left-hand operand (the literal)
* @param rhs The right-hand operand
*/
public BinaryArithmeticOperation(
CriteriaBuilderImpl criteriaBuilder,
Class<N> javaType,
Operation operator,
N lhs,
Expression<? extends N> rhs) {
super( criteriaBuilder, javaType );
this.operator = operator;
this.lhs = new LiteralExpression<N>( criteriaBuilder, lhs );
this.rhs = rhs;
}
public Operation getOperator() {
return operator;

View File

@ -0,0 +1,83 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. 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 Inc.
*
* 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 java.math.BigDecimal;
import java.math.BigInteger;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import org.hibernate.ejb.metamodel.AbstractMetamodelSpecificTest;
import org.hibernate.ejb.metamodel.Product;
/**
* Tests that various expressions operate as expected
*
* @author Steve Ebersole
*/
public class ExpressionsTest extends AbstractMetamodelSpecificTest {
private CriteriaBuilder builder;
@Override
public void setUp() {
super.setUp();
builder = factory.getCriteriaBuilder();
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Product product = new Product();
product.setId( "product1" );
product.setPrice( 1.23d );
product.setQuantity( 2 );
product.setPartNumber( Integer.MAX_VALUE + 1 );
product.setRating( 1.999f );
product.setSomeBigInteger( BigInteger.valueOf( 987654321 ) );
product.setSomeBigDecimal( BigDecimal.valueOf( 987654.321 ) );
em.persist( product );
em.getTransaction().commit();
em.close();
}
@Override
public void tearDown() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.createQuery( "delete Product" ).executeUpdate();
em.getTransaction().commit();
em.close();
super.tearDown();
}
public void testDiff() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Integer> criteria = builder.createQuery( Integer.class );
criteria.from( Product.class );
criteria.select( builder.diff( builder.literal( 5 ), builder.literal( 2 ) ) );
Integer result = em.createQuery( criteria ).getSingleResult();
assertEquals( Integer.valueOf( 3 ), result );
em.getTransaction().commit();
em.close();
}
}