From 287f67d0b4f14040561f8198b6c7fa44d56a1153 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Tue, 11 Jan 2011 18:14:05 -0800 Subject: [PATCH] HHH-5817 : Passing char[] or byte[] to equal function of CriteriaBuilder throws ClassCastException (Vyacheslav Dimitrov)) --- .../org/hibernate/ejb/AbstractQueryImpl.java | 2 +- .../ejb/criteria/basic/PredicateTest.java | 34 ++++++++++++++++++- .../org/hibernate/ejb/metamodel/Order.java | 29 +++++++++++++++- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/entitymanager/src/main/java/org/hibernate/ejb/AbstractQueryImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/AbstractQueryImpl.java index 751a0cfe91..3c9c383230 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/AbstractQueryImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/AbstractQueryImpl.java @@ -344,7 +344,7 @@ public abstract class AbstractQueryImpl implements TypedQuery { } } } - 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 ) ) { diff --git a/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/PredicateTest.java b/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/PredicateTest.java index f7314048a0..53124df1e9 100644 --- a/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/PredicateTest.java +++ b/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/PredicateTest.java @@ -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 orderCriteria = builder.createQuery( Order.class ); + Root orderRoot = orderCriteria.from( Order.class ); + + orderCriteria.select( orderRoot ); + Predicate p = builder.equal( orderRoot.get( "domen" ), new char[]{'r','u'} ); + orderCriteria.where( p ); + + List orders = em.createQuery( orderCriteria ).getResultList(); + assertTrue( orders.size() == 1 ); + } + + /** + * Check predicate for field which has simple char array type (byte[]). + */ + public void testByteArray() { + CriteriaQuery orderCriteria = builder.createQuery( Order.class ); + Root orderRoot = orderCriteria.from( Order.class ); + + orderCriteria.select( orderRoot ); + Predicate p = builder.equal( orderRoot.get( "number" ), new byte[]{'1','2'} ); + orderCriteria.where( p ); + + List orders = em.createQuery( orderCriteria ).getResultList(); + assertTrue( orders.size() == 0 ); + } + + } diff --git a/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Order.java b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Order.java index 6c1e362666..3b2aec879d 100644 --- a/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Order.java +++ b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Order.java @@ -49,6 +49,9 @@ public class Order implements java.io.Serializable { private LineItem sampleLineItem; private Collection lineItems = new java.util.ArrayList(); + 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 c) { this.lineItems = c; } -} \ No newline at end of file + + public char[] getDomen() { + return domen; + } + + public void setDomen(char[] d) { + domen = d; + } + + public byte[] getNumber() { + return number; + } + + public void setNumber(byte[] n) { + number = n; + } + + +}