|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * Copyright (c) 2010, 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.basic; |
| 25 | + |
| 26 | +import static org.junit.Assert.assertEquals; |
| 27 | +import static org.junit.Assert.assertNotNull; |
| 28 | + |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import javax.persistence.EntityManager; |
| 32 | +import javax.persistence.criteria.CriteriaBuilder; |
| 33 | +import javax.persistence.criteria.CriteriaQuery; |
| 34 | +import javax.persistence.criteria.ListJoin; |
| 35 | +import javax.persistence.criteria.Root; |
| 36 | + |
| 37 | +import org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest; |
| 38 | +import org.hibernate.jpa.test.metamodel.Address; |
| 39 | +import org.hibernate.jpa.test.metamodel.Address_; |
| 40 | +import org.hibernate.jpa.test.metamodel.Phone; |
| 41 | +import org.hibernate.testing.TestForIssue; |
| 42 | +import org.junit.Test; |
| 43 | + |
| 44 | +/** |
| 45 | + * Tests usage of {@link ListJoin#index()} |
| 46 | + * |
| 47 | + * @author Brett Meyer |
| 48 | + */ |
| 49 | +public class ListIndexTest extends AbstractMetamodelSpecificTest { |
| 50 | + |
| 51 | + @Test |
| 52 | + @TestForIssue(jiraKey = "HHH-8404") |
| 53 | + public void testListIndex() { |
| 54 | + EntityManager em = getOrCreateEntityManager(); |
| 55 | + |
| 56 | + em.getTransaction().begin(); |
| 57 | + |
| 58 | + Address address1 = new Address(); |
| 59 | + address1.setId( "a1" ); |
| 60 | + Phone phone1 = new Phone(); |
| 61 | + phone1.setId( "p1" ); |
| 62 | + phone1.setAddress( address1 ); |
| 63 | + Phone phone2 = new Phone(); |
| 64 | + phone2.setId( "p2" ); |
| 65 | + |
| 66 | + phone2.setAddress( address1 ); |
| 67 | + address1.getPhones().add( phone1 ); |
| 68 | + address1.getPhones().add( phone2 ); |
| 69 | + |
| 70 | + Address address2 = new Address(); |
| 71 | + address2.setId( "a2" ); |
| 72 | + Phone phone3 = new Phone(); |
| 73 | + phone3.setId( "p3" ); |
| 74 | + |
| 75 | + phone3.setAddress( address2 ); |
| 76 | + address2.getPhones().add( phone3 ); |
| 77 | + |
| 78 | + em.persist( phone1 ); |
| 79 | + em.persist( phone2 ); |
| 80 | + em.persist( phone3 ); |
| 81 | + em.persist( address1 ); |
| 82 | + em.persist( address2 ); |
| 83 | + em.getTransaction().commit(); |
| 84 | + em.clear(); |
| 85 | + |
| 86 | + CriteriaBuilder cb = em.getCriteriaBuilder(); |
| 87 | + CriteriaQuery<Address> criteria = cb.createQuery( Address.class ); |
| 88 | + Root<Address> addressRoot = criteria.from( Address.class ); |
| 89 | + ListJoin<Address, Phone> phones = addressRoot.join( Address_.phones ); |
| 90 | + criteria.where( cb.gt( phones.index(), 0 ) ); |
| 91 | + List<Address> results = em.createQuery( criteria ).getResultList(); |
| 92 | + |
| 93 | + assertNotNull( results ); |
| 94 | + // Ensure that the "index(phones) > 0" condition was included on the inner join, meaning only address1 |
| 95 | + // (> 1 phone) was returned. |
| 96 | + assertEquals( 1, results.size() ); |
| 97 | + assertEquals( address1.getId(), results.get( 0 ).getId() ); |
| 98 | + } |
| 99 | +} |
0 commit comments