|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.test.hql; |
| 8 | + |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | +import javax.persistence.Column; |
| 12 | +import javax.persistence.ElementCollection; |
| 13 | +import javax.persistence.Embeddable; |
| 14 | +import javax.persistence.Entity; |
| 15 | +import javax.persistence.EnumType; |
| 16 | +import javax.persistence.Id; |
| 17 | +import javax.persistence.JoinColumn; |
| 18 | +import javax.persistence.MapKeyColumn; |
| 19 | +import javax.persistence.MapKeyEnumerated; |
| 20 | +import javax.persistence.OneToMany; |
| 21 | +import javax.persistence.Table; |
| 22 | + |
| 23 | +import org.hibernate.Session; |
| 24 | + |
| 25 | +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +/** |
| 29 | + * Test originally written to help verify/diagnose HHH-10125 |
| 30 | + * |
| 31 | + * @author Steve Ebersole |
| 32 | + */ |
| 33 | +public class MapFunctionExpressionsTest extends BaseNonConfigCoreFunctionalTestCase { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testMapKeyExpressionInWhere() { |
| 37 | + // NOTE : JPA requires that an alias be used in the key() expression. Hibernate allows |
| 38 | + // path or alias. |
| 39 | + |
| 40 | + Session s = openSession(); |
| 41 | + s.getTransaction().begin(); |
| 42 | + // JPA form |
| 43 | + s.createQuery( "from Contact c join c.addresses a where key(a) = 'HOME'" ).list(); |
| 44 | + // Hibernate additional form |
| 45 | + s.createQuery( "from Contact c where key(c.addresses) = 'HOME'" ).list(); |
| 46 | + s.getTransaction().commit(); |
| 47 | + s.close(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testMapKeyExpressionInSelect() { |
| 52 | + // NOTE : JPA requires that an alias be used in the key() expression. Hibernate allows |
| 53 | + // path or alias. |
| 54 | + |
| 55 | + Session s = openSession(); |
| 56 | + s.getTransaction().begin(); |
| 57 | + // JPA form |
| 58 | + s.createQuery( "select key(a) from Contact c join c.addresses a" ).list(); |
| 59 | + // Hibernate additional form |
| 60 | + s.createQuery( "select key(c.addresses) from Contact c" ).list(); |
| 61 | + s.getTransaction().commit(); |
| 62 | + s.close(); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testMapValueExpressionInSelect() { |
| 67 | + Session s = openSession(); |
| 68 | + s.getTransaction().begin(); |
| 69 | + s.createQuery( "select value(a) from Contact c join c.addresses a" ).list(); |
| 70 | + s.createQuery( "select value(c.addresses) from Contact c" ).list(); |
| 71 | + s.getTransaction().commit(); |
| 72 | + s.close(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected Class[] getAnnotatedClasses() { |
| 77 | + return new Class[] { Address.class, Contact.class }; |
| 78 | + } |
| 79 | + |
| 80 | + public static enum AddressType { |
| 81 | + HOME, |
| 82 | + WORK, |
| 83 | + BUSINESS |
| 84 | + } |
| 85 | + |
| 86 | + @Entity(name = "Address") |
| 87 | + @Table(name = "address") |
| 88 | +// @Embeddable |
| 89 | + public static class Address { |
| 90 | + @Id |
| 91 | + public Integer id; |
| 92 | + String street; |
| 93 | + String city; |
| 94 | + } |
| 95 | + |
| 96 | + @Entity(name = "Contact") |
| 97 | + @Table(name = "contact") |
| 98 | + public static class Contact { |
| 99 | + @Id |
| 100 | + public Integer id; |
| 101 | + String name; |
| 102 | + @OneToMany |
| 103 | + @JoinColumn |
| 104 | +// @ElementCollection |
| 105 | + @MapKeyEnumerated(EnumType.STRING) |
| 106 | + @MapKeyColumn(name = "addr_type") |
| 107 | + Map<AddressType, Address> addresses = new HashMap<AddressType, Address>(); |
| 108 | + } |
| 109 | +} |
0 commit comments