|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * Copyright (c) {DATE}, Red Hat Inc. or third-party contributors as |
| 5 | + * indicated by the @author tags or express copyright attribution |
| 6 | + * statements applied by the authors. All third-party contributions are |
| 7 | + * distributed under license by Red Hat Inc. |
| 8 | + * |
| 9 | + * This copyrighted material is made available to anyone wishing to use, modify, |
| 10 | + * copy, or redistribute it subject to the terms and conditions of the GNU |
| 11 | + * Lesser General Public License, as published by the Free Software Foundation. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 16 | + * for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public License |
| 19 | + * along with this distribution; if not, write to: |
| 20 | + * Free Software Foundation, Inc. |
| 21 | + * 51 Franklin Street, Fifth Floor |
| 22 | + * Boston, MA 02110-1301 USA |
| 23 | + */ |
| 24 | +package org.hibernate.jpa.test.criteria.selectcase; |
| 25 | + |
| 26 | +import javax.persistence.EntityManager; |
| 27 | +import javax.persistence.EnumType; |
| 28 | +import javax.persistence.Enumerated; |
| 29 | +import javax.persistence.Id; |
| 30 | +import javax.persistence.criteria.CriteriaBuilder; |
| 31 | +import javax.persistence.criteria.CriteriaQuery; |
| 32 | +import javax.persistence.criteria.Predicate; |
| 33 | +import javax.persistence.criteria.Root; |
| 34 | +import java.util.List; |
| 35 | + |
| 36 | +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; |
| 37 | + |
| 38 | +import org.junit.Test; |
| 39 | + |
| 40 | +import org.hibernate.testing.TestForIssue; |
| 41 | + |
| 42 | +@TestForIssue( jiraKey = "HHH-9731" ) |
| 43 | +public class SelectCaseTest extends BaseEntityManagerFunctionalTestCase { |
| 44 | + |
| 45 | + @Override |
| 46 | + protected Class<?>[] getAnnotatedClasses() { |
| 47 | + return new Class[] {Entity.class}; |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void selectCaseWithValuesShouldWork() { |
| 52 | + EntityManager entityManager = getOrCreateEntityManager(); |
| 53 | + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); |
| 54 | + |
| 55 | + CriteriaBuilder.Case<EnumValue> selectCase = cb.selectCase(); |
| 56 | + Predicate somePredicate = cb.equal( cb.literal( 1 ), 1 ); |
| 57 | + selectCase.when( somePredicate, EnumValue.VALUE_1 ); |
| 58 | + selectCase.otherwise( EnumValue.VALUE_2 ); |
| 59 | + |
| 60 | + CriteriaQuery<Entity> query = cb.createQuery( Entity.class ); |
| 61 | + Root<Entity> from = query.from( Entity.class ); |
| 62 | + query.select( from ).where( cb.equal( from.get( "value" ), selectCase ) ); |
| 63 | + |
| 64 | + entityManager.createQuery( query ).getResultList(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void selectCaseWithCastedTypeValuesShouldWork() { |
| 69 | + EntityManager entityManager = getOrCreateEntityManager(); |
| 70 | + |
| 71 | + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); |
| 72 | + |
| 73 | + CriteriaBuilder.Case<String> selectCase = cb.selectCase(); |
| 74 | + Predicate somePredicate = cb.equal( cb.literal( 1 ), 1 ); |
| 75 | + selectCase.when( somePredicate, EnumValue.VALUE_1.name() ); |
| 76 | + selectCase.otherwise( EnumValue.VALUE_2.name() ); |
| 77 | + |
| 78 | + CriteriaQuery<Entity> query = cb.createQuery( Entity.class ); |
| 79 | + Root<Entity> from = query.from( Entity.class ); |
| 80 | + query.select( from ).where( cb.equal( from.get( "value" ), selectCase.as( String.class ) ) ); |
| 81 | + |
| 82 | + entityManager.createQuery( query ).getResultList(); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void simpleSelectCaseWithValuesShouldWork() { |
| 87 | + EntityManager entityManager = getOrCreateEntityManager(); |
| 88 | + |
| 89 | + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); |
| 90 | + |
| 91 | + CriteriaBuilder.SimpleCase<Integer, EnumValue> selectCase = cb.selectCase( cb.literal( 1 ) ); |
| 92 | + selectCase.when( 1, EnumValue.VALUE_1 ); |
| 93 | + selectCase.otherwise( EnumValue.VALUE_2 ); |
| 94 | + |
| 95 | + CriteriaQuery<Entity> query = cb.createQuery( Entity.class ); |
| 96 | + Root<Entity> from = query.from( Entity.class ); |
| 97 | + query.select( from ).where( cb.equal( from.get( "value" ), selectCase ) ); |
| 98 | + |
| 99 | + List<?> result = entityManager.createQuery( query ).getResultList(); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void simpleSelectCaseWithCastedTypeValuesShouldWork() { |
| 104 | + EntityManager entityManager = getOrCreateEntityManager(); |
| 105 | + |
| 106 | + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); |
| 107 | + |
| 108 | + CriteriaBuilder.SimpleCase<Integer, String> selectCase = cb.selectCase( cb.literal( 1 ) ); |
| 109 | + selectCase.when( 1, EnumValue.VALUE_1.name() ); |
| 110 | + selectCase.otherwise( EnumValue.VALUE_2.name() ); |
| 111 | + |
| 112 | + CriteriaQuery<Entity> query = cb.createQuery( Entity.class ); |
| 113 | + Root<Entity> from = query.from( Entity.class ); |
| 114 | + query.select( from ).where( cb.equal( from.get( "value" ), selectCase.as( String.class ) ) ); |
| 115 | + |
| 116 | + entityManager.createQuery( query ).getResultList(); |
| 117 | + } |
| 118 | + |
| 119 | + @javax.persistence.Entity |
| 120 | + public static class Entity { |
| 121 | + |
| 122 | + @Id |
| 123 | + private Long id; |
| 124 | + |
| 125 | + @Enumerated(EnumType.STRING) |
| 126 | + private EnumValue value; |
| 127 | + } |
| 128 | + |
| 129 | + public enum EnumValue { |
| 130 | + VALUE_1, |
| 131 | + VALUE_2; |
| 132 | + } |
| 133 | +} |
0 commit comments