From 5e1c6bc3affa6daa9f667bb1fac66ebdbd947710 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Sat, 26 Sep 2015 09:01:07 -0500 Subject: [PATCH] HHH-10125 - Using KEY function in JP-QL SELECT clause selects value columns in rendered SQL (cherry picked from commit 909c034429cb529bdaac7d35045aa9337e7a8765) --- .../test/hql/MapFunctionExpressionsTest.java | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/hql/MapFunctionExpressionsTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/MapFunctionExpressionsTest.java b/hibernate-core/src/test/java/org/hibernate/test/hql/MapFunctionExpressionsTest.java new file mode 100644 index 0000000000..67d616def4 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/MapFunctionExpressionsTest.java @@ -0,0 +1,109 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.hql; + +import java.util.HashMap; +import java.util.Map; +import javax.persistence.Column; +import javax.persistence.ElementCollection; +import javax.persistence.Embeddable; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.MapKeyColumn; +import javax.persistence.MapKeyEnumerated; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +import org.hibernate.Session; + +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; + +/** + * Test originally written to help verify/diagnose HHH-10125 + * + * @author Steve Ebersole + */ +public class MapFunctionExpressionsTest extends BaseNonConfigCoreFunctionalTestCase { + + @Test + public void testMapKeyExpressionInWhere() { + // NOTE : JPA requires that an alias be used in the key() expression. Hibernate allows + // path or alias. + + Session s = openSession(); + s.getTransaction().begin(); + // JPA form + s.createQuery( "from Contact c join c.addresses a where key(a) = 'HOME'" ).list(); + // Hibernate additional form + s.createQuery( "from Contact c where key(c.addresses) = 'HOME'" ).list(); + s.getTransaction().commit(); + s.close(); + } + + @Test + public void testMapKeyExpressionInSelect() { + // NOTE : JPA requires that an alias be used in the key() expression. Hibernate allows + // path or alias. + + Session s = openSession(); + s.getTransaction().begin(); + // JPA form + s.createQuery( "select key(a) from Contact c join c.addresses a" ).list(); + // Hibernate additional form + s.createQuery( "select key(c.addresses) from Contact c" ).list(); + s.getTransaction().commit(); + s.close(); + } + + @Test + public void testMapValueExpressionInSelect() { + Session s = openSession(); + s.getTransaction().begin(); + s.createQuery( "select value(a) from Contact c join c.addresses a" ).list(); + s.createQuery( "select value(c.addresses) from Contact c" ).list(); + s.getTransaction().commit(); + s.close(); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Address.class, Contact.class }; + } + + public static enum AddressType { + HOME, + WORK, + BUSINESS + } + + @Entity(name = "Address") + @Table(name = "address") +// @Embeddable + public static class Address { + @Id + public Integer id; + String street; + String city; + } + + @Entity(name = "Contact") + @Table(name = "contact") + public static class Contact { + @Id + public Integer id; + String name; + @OneToMany + @JoinColumn +// @ElementCollection + @MapKeyEnumerated(EnumType.STRING) + @MapKeyColumn(name = "addr_type") + Map addresses = new HashMap(); + } +}