Question

In Google app engine data-store i have 3 step hierarchy for entity. and these 3 entities are..

Department(Parent Entity) 
-Id
-Department Name
-list of Employee(@OneToMany) 

Employee(Child of Department)  
-key
-firstName
-lastName
-phoneNumber
-list of Address(@OneToMany) 

Address(Child of Employee)
-key
-city
-pincode

If I need to retrieve all the employees of a specific city with their respective addresses, what entity/ies I should create? With my existing datastore design, I will need to query n times for retrieving address for n employee, which is not at all desirable. Please advise how I can design my datastore entities so that I can retrieve all the employees with their address details in a single or may be two queries.

Any help is appreciated. Thanks in advance!

Était-ce utile?

La solution

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top