HHH-5817 : Passing char[] or byte[] to equal function of CriteriaBuilder throws ClassCastException (Vyacheslav Dimitrov)

This commit is contained in:
Gail Badner 2011-01-11 18:18:17 -08:00
parent 1c158b0bb7
commit e024adebe7
3 changed files with 62 additions and 3 deletions

View File

@ -344,7 +344,7 @@ public abstract class AbstractQueryImpl<X> implements TypedQuery<X> {
}
}
}
else if ( value.getClass().isArray() ) {
else if ( value.getClass().isArray() && value.getClass().equals( Object[].class ) ) {
final Object[] array = (Object[]) value;
for ( Object element : array ) {
if ( ! parameter.getParameterType().isInstance( element ) ) {

View File

@ -151,7 +151,39 @@ public class PredicateTest extends AbstractMetamodelSpecificTest {
em.getTransaction().begin();
em.persist( new Order( "order-1", 1.0d ) );
em.persist( new Order( "order-2", 10.0d ) );
em.persist( new Order( "order-3", 100.0d ) );
em.persist( new Order( "order-3", new char[]{'r','u'} ) );
em.getTransaction().commit();
}
/**
* Check predicate for field which has simple char array type (char[]).
*/
public void testCharArray() {
CriteriaQuery<Order> orderCriteria = builder.createQuery( Order.class );
Root<Order> orderRoot = orderCriteria.from( Order.class );
orderCriteria.select( orderRoot );
Predicate p = builder.equal( orderRoot.get( "domen" ), new char[]{'r','u'} );
orderCriteria.where( p );
List<Order> orders = em.createQuery( orderCriteria ).getResultList();
assertTrue( orders.size() == 1 );
}
/**
* Check predicate for field which has simple char array type (byte[]).
*/
public void testByteArray() {
CriteriaQuery<Order> orderCriteria = builder.createQuery( Order.class );
Root<Order> orderRoot = orderCriteria.from( Order.class );
orderCriteria.select( orderRoot );
Predicate p = builder.equal( orderRoot.get( "number" ), new byte[]{'1','2'} );
orderCriteria.where( p );
List<Order> orders = em.createQuery( orderCriteria ).getResultList();
assertTrue( orders.size() == 0 );
}
}

View File

@ -49,6 +49,9 @@ public class Order implements java.io.Serializable {
private LineItem sampleLineItem;
private Collection<LineItem> lineItems = new java.util.ArrayList<LineItem>();
private char[] domen;
private byte[] number;
public Order() {
}
@ -62,6 +65,12 @@ public class Order implements java.io.Serializable {
this.customer = customer;
}
public Order(String id, char[] domen) {
this.id = id;
this.domen = domen;
}
public Order(String id) {
this.id = id;
}
@ -138,4 +147,22 @@ public class Order implements java.io.Serializable {
public void setLineItems(Collection<LineItem> c) {
this.lineItems = c;
}
}
public char[] getDomen() {
return domen;
}
public void setDomen(char[] d) {
domen = d;
}
public byte[] getNumber() {
return number;
}
public void setNumber(byte[] n) {
number = n;
}
}