I'm trying to use query by example combining two classes, just like the second example given in page 682 of "Java Persistence with Hibernate".
I have a class Route that has a List of Waypoint, called waypointList. I want to find all routes that has a waypoint with name "WP2":
Code:
Waypoint wp = new Waypoint();
wp.setName("WP2");
Route r = new Route();
Example routeExample = Example.create(r).excludeZeroes();
Example waypointExample = Example.create(wp).excludeZeroes();
DetachedCriteria c;
// --------------------------------------------------------------------//
c = DetachedCriteria.forClass(Route.class)
.add(routeExample)
.createCriteria("waypointList")
.add(waypointExample);
List result = routeDAO.findByCriteria(c);
if (result.size() == 0) System.out.println(" EMPTY LIST! 1");
else {
for(Object o : result){
System.out.println("Route: " + ((Route) o).getOid());
}
System.out.println("size = " + result.size());
}
System.out.println("--------------------------------------------------------------------");
c = DetachedCriteria.forClass(Route.class)
.add(routeExample);
List result2 = routeDAO.findByCriteria(c);
if (result2.size() == 0) System.out.println(" EMPTY LIST! 2");
else {
for(Object o : result2){
System.out.println("Route: " + ((Route) o).getOid());
}
System.out.println("size = " + result2.size());
}
System.out.println("--------------------------------------------------------------------");
c = DetachedCriteria.forClass(Waypoint.class);
c.add(waypointExample);
List<Waypoint> result3 = waypointDAO.findByCriteria(c);
if (result2.size() == 0) System.out.println(" EMPTY LIST! 3");
else {
for(Object o : result3){
System.out.println("WPoint: " + ((Waypoint) o).getOid());
}
System.out.println("size = " + result2.size());
}
The result is:
Code:
EMPTY LIST! 1
--------------------------------------------------------------------
Route: 70
Route: 71
Route: 72
size = 3
--------------------------------------------------------------------
WPoint: 81
WPoint: 83
WPoint: 85
size = 3
That is, it finds the waypoints with that name, it finds all routes but it doesn't find the two together.
How can I perform this search with QBE?
Thank you