HHH-8404 test case

Conflicts:
	hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/QueryBuilderTest.java
	hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/metamodel/Address.java
This commit is contained in:
Brett Meyer 2014-01-20 17:53:06 -05:00
parent 3d2d9bfd4f
commit 2059edd296
3 changed files with 108 additions and 6 deletions

View File

@ -125,7 +125,7 @@ public class QueryBuilderTest extends BaseEntityManagerFunctionalTestCase {
Phone phone3 = new Phone( "3", "555", "0003", address ); Phone phone3 = new Phone( "3", "555", "0003", address );
Phone phone4 = new Phone( "4", "555", "0004" ); 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( phone1 );
phones.add( phone2 ); phones.add( phone2 );
phones.add( phone3 ); phones.add( phone3 );

View File

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

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.ejb.metamodel.AbstractMetamodelSpecificTest;
import org.hibernate.ejb.metamodel.Address;
import org.hibernate.ejb.metamodel.Address_;
import org.hibernate.ejb.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() );
}
}