If you only need to query Employees based on their city, you can add a List<String>
of cities on your Employee
class. This way you can run a query like SELECT e FROM Employee e WHERE :cityQueried IN e.cities
You will have to maintain sync between the cities and the addresses.
public class Employee {
@Id
private Key key;
@Basic
private String firstName;
@Basic
private String lastName;
@OneToMany
private List<Address> addresses;
@Basic
private List<String> cities;
/**
* Same goes for removeAddress...
*/
public void addAddress(Address address) {
addresses.add(address);
cities.add(address.city);
}
}
EDIT :
Unfortunately, if you want to query an entity, you have to do it on either its own properties and/or its ancestor (parent) key.
An alternative solution, if your employees only have a limited number of addresses, is to directly store the address attributes on the Employee
entity:
public class Employee {
@Id
private Key key;
@Basic
private String firstName;
@Basic
private String lastName;
@Basic
private String address1City;
@Basic
private String address1ZipCode;
@Basic
private String address2City;
@Basic
private String address2ZipCode;
}
No miracle here either