I have created Owned One-to-Many Relationships between Department and Employee using JPA annotation. Entities are:

Department(Parent)
    String Id
    String Name
    List Employee(@OneToMany) 

Employee(Child)
    Key key
    String firstName
    String lastName
    String Address
    Department dept(@ManyToOne)

When Employee's firstName is entered I want to display that Employee detail(lastName and address) and also from which Department he Belongs. i did not find any solution to get the parent info from child Entity. Is there any way to get the information about parent entity from its child entity ?

I think one possible solution is to get the parent id from child and in another query get the Parent Detail. but i want all the information about employee in single query. is this possible?

here is my code.

Department Entity:
    @Entity
    public class Department {

          @Id
          private String id;

          private String description;

          @OneToMany(mappedBy = "department", fetch=FetchType.EAGER, cascade = CascadeType.ALL)
          private List<Employee> employee = new ArrayList<Employee>();

          public String getId() {
              return id;
          }

          public void setId(String id) {
              this.id = id;
          }

          public String getDescription() {
            return description;
          }

          public void setDescription(String description) {
            this.description = description;
          }

          public List<Employee> getEmployee () {
                return employee ;
          }

          public void setEmployee (List<Employee > employee) {
                this.employee = employee ;
          }
    }

Employee Entity:
    @Entity
    public class Employee {
             @Id
             @GeneratedValue(strategy = GenerationType.IDENTITY)
             private Key key;

             private String firstName;
             private String lastName;
             private String Address;

             private String parent_Id;

             @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
             private Department department;

             public Key getKey() {
            return key;
             }

             public String getFirstName() {
                 return firstName;
             }

             public void setFirstName(String firstName) {
                 this.firstName = firstName;
             }

             public String getLastName() {
                 return lastName;
             }

             public void setLastName(String lastName) {
                 this.lastName = lastName;
             }

             public String getAddress() {
                return Address;
             }

             public void setAddress(String Address) {
                this.Address = Address;
             }
    }

Android Code to insert Entity in Background

Departmentendpoint.Builder builder1 = new                                          Departmentendpoint.Builder(AndroidHttp.newCompatibleTransport(), new JacksonFactory(), null);
Departmentendpoint dptendpoint = CloudEndpointUtils.updateBuilder(builder1).build();

    Department dpt = new Department();
    dpt.setDescription("Water Service"); 
    List<Employee> m = new ArrayList<Employee>();       
    Employee mgr2= new Employee();
    mgr2.setFirstName("Avinash"+i);
    mgr2.setLastName("Patel"+i);
    mgr2.setAddress("Vadodara");
    m.add(mgr2);    
    dpt.setEmployee(m);

    try {
        dptendpoint.insertDepartment(dpt).execute();
    } catch (IOException e) {
        e.printStackTrace();
    }
有帮助吗?

解决方案

You can get a parent id from a child entity's key, but you have to get the parent entity from the datastore.

You can store a department name in every employee entity, which will save you one get operation at the expense of extra data and more complex code (you have to account for situations like a change in a department name).

A better solution is to keep department entities in memcache. This way you save a trip to the datastore and keep your data model and code simple.

其他提示

The basic idea with Entity relationship with GAE Datastore is:

A child entity must have a key with parent set to a given parent Enitty like:

Entity parent = createParentEntity();
Enttity child = new Entity(KeyFactory.createKey(parent.getKey, kind, key));

A parent entity must have the child Entity key as its property:

Entity parent = createParentEntity();
Entity child = createChildEnity();

parent.setProperty("fieldName", child.getKey());

This way you can retrieve the parent from the child Entity or you can fetch the child from the parent entity. Hope this helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top