HHH-3646 - implement a test case for query collection-of-component

This commit is contained in:
David Mansfield 2011-03-28 17:56:24 -04:00 committed by Steve Ebersole
parent 64b73d6301
commit b8230bd07d
4 changed files with 201 additions and 1 deletions

View File

@ -28,6 +28,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import java.util.HashMap;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
@ -1709,5 +1710,69 @@ public class CriteriaQueryTest extends BaseCoreFunctionalTestCase {
session.close();
}
@Test
public void testCriteriaCollectionOfComponent() {
Session session = openSession();
Transaction t = session.beginTransaction();
Student gavin = new Student();
gavin.setName("Gavin King");
gavin.setStudentNumber(232);
Map addresses = new HashMap();
StudentAddress addr = new StudentAddress();
addr.setLine1("101 Main St.");
addr.setCity("Anytown");
addr.setState("NY");
addr.setZip("10016");
addresses.put("HOME", addr);
addr = new StudentAddress();
addr.setLine1("202 Spring St.");
addr.setCity("Springfield");
addr.setState("MA");
addr.setZip("99999");
addresses.put("SCHOOL", addr);
gavin.setAddresses(addresses);
session.persist(gavin);
Student xam = new Student();
xam.setName("Max Rydahl Andersen");
xam.setStudentNumber(101);
addresses = new HashMap();
addr = new StudentAddress();
addr.setLine1("123 3rd Ave");
addr.setCity("New York");
addr.setState("NY");
addr.setZip("10004");
addresses.put("HOME", addr);
xam.setAddresses(addresses);
session.persist(xam);
session.flush();
session.clear();
// search on a component property
List results = session.createCriteria(Student.class)
.createCriteria("addresses")
.add(Restrictions.eq("state", "MA"))
.list();
assertEquals(1, results.size());
gavin = (Student)results.get(0);
assertEquals(2, gavin.getAddresses().keySet().size());
session.delete(gavin);
session.delete(xam);
t.commit();
session.close();
}
}

View File

@ -46,7 +46,18 @@
<key column="studentId"/>
<one-to-many class="Enrolment"/>
</set>
<many-to-one name="preferredCourse" column="preferredCourseCode"/>
<many-to-one name="preferredCourse" column="preferredCourseCode"/>
<map name="addresses" table="studentAddresses">
<key column="studentId" />
<map-key type="string" column="addressType" />
<composite-element class="org.hibernate.test.criteria.StudentAddress" >
<property name="line1" />
<property name="line2" />
<property name="city" />
<property name="state" />
<property name="zip" />
</composite-element>
</map>
</class>
<class name="Enrolment">

View File

@ -2,6 +2,7 @@
package org.hibernate.test.criteria;
import java.util.HashSet;
import java.util.Set;
import java.util.Map;
/**
* @author Gavin King
@ -12,6 +13,7 @@ public class Student {
private CityState cityState;
private Course preferredCourse;
private Set enrolments = new HashSet();
private Map addresses;
public String getName() {
return name;
@ -52,4 +54,12 @@ public class Student {
public void setEnrolments(Set employments) {
this.enrolments = employments;
}
public Map getAddresses() {
return addresses;
}
public void setAddresses(Map addresses) {
this.addresses = addresses;
}
}

View File

@ -0,0 +1,114 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.test.criteria;
/**
* @author David Mansfield
*/
public class StudentAddress {
private String line1;
private String line2;
private String city;
private String state;
private String zip;
public String getLine1() {
return line1;
}
public void setLine1(String line1) {
this.line1 = line1;
}
public String getLine2() {
return line2;
}
public void setLine2(String line2) {
this.line2 = line2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
// @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((city == null) ? 0 : city.hashCode());
result = prime * result + ((line1 == null) ? 0 : line1.hashCode());
result = prime * result + ((line2 == null) ? 0 : line2.hashCode());
result = prime * result + ((state == null) ? 0 : state.hashCode());
result = prime * result + ((zip == null) ? 0 : zip.hashCode());
return result;
}
// @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
StudentAddress other = (StudentAddress) obj;
if (city == null) {
if (other.city != null)
return false;
} else if (!city.equals(other.city))
return false;
if (line1 == null) {
if (other.line1 != null)
return false;
} else if (!line1.equals(other.line1))
return false;
if (line2 == null) {
if (other.line2 != null)
return false;
} else if (!line2.equals(other.line2))
return false;
if (state == null) {
if (other.state != null)
return false;
} else if (!state.equals(other.state))
return false;
if (zip == null) {
if (other.zip != null)
return false;
} else if (!zip.equals(other.zip))
return false;
return true;
}
}