From 857a5c07249922c5e69040368e46f38b25696abf Mon Sep 17 00:00:00 2001 From: Cedomir Igaly Date: Sun, 28 May 2023 13:46:50 +0200 Subject: [PATCH] HHH-16606 - Test for selecting only property of array type using criteria query --- .../criteria/SelectOnlyArrayPropertyTest.java | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/SelectOnlyArrayPropertyTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/SelectOnlyArrayPropertyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/SelectOnlyArrayPropertyTest.java new file mode 100644 index 0000000000..49b17d1222 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/SelectOnlyArrayPropertyTest.java @@ -0,0 +1,114 @@ +package org.hibernate.orm.test.jpa.criteria; + +import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.orm.junit.JiraKey; +import org.junit.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.TypedQuery; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +@JiraKey("HHH-16606") +public class SelectOnlyArrayPropertyTest extends BaseEntityManagerFunctionalTestCase { + + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + EntityWithIdAndIntegerArray.class + }; + } + + @Test + @JiraKey("HHH-16606") + public void criteriaSelectOnlyIntArray() { + doInJPA( this::entityManagerFactory, entityManager -> { + final byte[] result = "Hello, World!".getBytes(); + + EntityWithIdAndIntegerArray myEntity = new EntityWithIdAndIntegerArray( 1, result ); + entityManager.persist( myEntity ); + + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery( byte[].class ); + + Root root = cq.from( EntityWithIdAndIntegerArray.class ); + + cq.select( root.get( "bytes" ) ) + .where( cb.equal( root.get( "id" ), 1 ) ); + + TypedQuery q = entityManager.createQuery( cq ); + + byte[] bytes = q.getSingleResult(); + + assertArrayEquals( result, bytes ); + } ); + } + + @Test + public void criteriaSelectWrappedIntArray() { + doInJPA( this::entityManagerFactory, entityManager -> { + final byte[] result = "Hi there!".getBytes(); + + EntityWithIdAndIntegerArray myEntity = new EntityWithIdAndIntegerArray( 2, result ); + entityManager.persist( myEntity ); + + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery( Object[].class ); + + Root root = cq.from( EntityWithIdAndIntegerArray.class ); + + cq.select( root.get( "bytes" ) ) + .where( cb.equal( root.get( "id" ), 2 ) ); + + TypedQuery q = entityManager.createQuery( cq ); + + final Object[] objects = q.getSingleResult(); + assertEquals( 1, objects.length ); + byte[] bytes = (byte[]) objects[0]; + + assertArrayEquals( result, bytes ); + } ); + } + + @Entity + public static class EntityWithIdAndIntegerArray { + + @Id + private Integer id; + + private byte[] bytes; + + public EntityWithIdAndIntegerArray(Integer id, byte[] bytes) { + this.id = id; + this.bytes = bytes; + } + + public EntityWithIdAndIntegerArray() { + + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public byte[] getBytes() { + return bytes; + } + + public void setBytes(byte[] bytes) { + this.bytes = bytes; + } + } +}