HHH-8404 test case

This commit is contained in:
Brett Meyer 2014-01-20 15:07:48 -05:00
parent 4ac4ed23db
commit b9e2e6935c
3 changed files with 114 additions and 14 deletions

View File

@ -23,19 +23,21 @@
*/
package org.hibernate.jpa.test.criteria;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.EntityType;
import org.junit.Test;
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
import org.hibernate.jpa.criteria.predicate.ComparisonPredicate;
import org.hibernate.jpa.internal.metamodel.MetamodelImpl;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.jpa.test.metamodel.Address;
import org.hibernate.jpa.test.metamodel.Alias;
import org.hibernate.jpa.test.metamodel.Country;
@ -44,17 +46,13 @@ import org.hibernate.jpa.test.metamodel.Customer;
import org.hibernate.jpa.test.metamodel.Customer_;
import org.hibernate.jpa.test.metamodel.Info;
import org.hibernate.jpa.test.metamodel.LineItem;
import org.hibernate.jpa.internal.metamodel.MetamodelImpl;
import org.hibernate.jpa.test.metamodel.Order;
import org.hibernate.jpa.test.metamodel.Phone;
import org.hibernate.jpa.test.metamodel.Product;
import org.hibernate.jpa.test.metamodel.ShelfLife;
import org.hibernate.jpa.test.metamodel.Spouse;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* @author Steve Ebersole
@ -127,7 +125,7 @@ public class QueryBuilderTest extends BaseEntityManagerFunctionalTestCase {
Phone phone3 = new Phone( "3", "555", "0003", address );
Phone phone4 = new Phone( "4", "555", "0004" );
Collection<Phone> phones = new ArrayList<Phone>( 3 );
List<Phone> phones = new ArrayList<Phone>( 3 );
phones.add( phone1 );
phones.add( phone2 );
phones.add( phone3 );

View File

@ -0,0 +1,99 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.jpa.test.criteria.basic;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ListJoin;
import javax.persistence.criteria.Root;
import org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest;
import org.hibernate.jpa.test.metamodel.Address;
import org.hibernate.jpa.test.metamodel.Address_;
import org.hibernate.jpa.test.metamodel.Phone;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
/**
* Tests usage of {@link ListJoin#index()}
*
* @author Brett Meyer
*/
public class ListIndexTest extends AbstractMetamodelSpecificTest {
@Test
@TestForIssue(jiraKey = "HHH-8404")
public void testListIndex() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Address address1 = new Address();
address1.setId( "a1" );
Phone phone1 = new Phone();
phone1.setId( "p1" );
phone1.setAddress( address1 );
Phone phone2 = new Phone();
phone2.setId( "p2" );
phone2.setAddress( address1 );
address1.getPhones().add( phone1 );
address1.getPhones().add( phone2 );
Address address2 = new Address();
address2.setId( "a2" );
Phone phone3 = new Phone();
phone3.setId( "p3" );
phone3.setAddress( address2 );
address2.getPhones().add( phone3 );
em.persist( phone1 );
em.persist( phone2 );
em.persist( phone3 );
em.persist( address1 );
em.persist( address2 );
em.getTransaction().commit();
em.clear();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Address> criteria = cb.createQuery( Address.class );
Root<Address> addressRoot = criteria.from( Address.class );
ListJoin<Address, Phone> phones = addressRoot.join( Address_.phones );
criteria.where( cb.gt( phones.index(), 0 ) );
List<Address> results = em.createQuery( criteria ).getResultList();
assertNotNull( results );
// Ensure that the "index(phones) > 0" condition was included on the inner join, meaning only address1
// (> 1 phone) was returned.
assertEquals( 1, results.size() );
assertEquals( address1.getId(), results.get( 0 ).getId() );
}
}

View File

@ -22,12 +22,14 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.jpa.test.metamodel;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
/**
@ -43,7 +45,7 @@ public class Address implements java.io.Serializable {
private String city;
private String state;
private String zip;
private Collection<Phone> phones = new java.util.ArrayList<Phone>();
private List<Phone> phones = new java.util.ArrayList<Phone>();
public Address() {
}
@ -57,7 +59,7 @@ public class Address implements java.io.Serializable {
}
public Address(String id, String street, String city, String state, String zip,
Collection<Phone> phones) {
List<Phone> phones) {
this.id = id;
this.street = street;
this.city = city;
@ -113,11 +115,12 @@ public class Address implements java.io.Serializable {
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "address")
public Collection<Phone> getPhones() {
@OrderColumn
public List<Phone> getPhones() {
return phones;
}
public void setPhones(Collection<Phone> phones) {
public void setPhones(List<Phone> phones) {
this.phones = phones;
}
}