【问题标题】:How to query parent entity from child entity in Google App Engine(Java) Datastore?如何从 Google App Engine(Java) Datastore 中的子实体查询父实体?
【发布时间】:2014-05-06 12:09:49
【问题描述】:

我使用 JPA 注释在部门和员工之间创建了拥有的一对多关系。 实体是:

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

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

当输入员工的名字时,我想显示该员工的详细信息(姓氏和地址)以及他所属的部门。我没有找到任何解决方案来从子实体获取父信息。有没有办法从其子实体中获取有关父实体的信息?

我认为一种可能的解决方案是从孩子那里获取父 ID,然后在另一个查询中获取父详细信息。但我想在单个查询中获得有关员工的所有信息。这可能吗?

这是我的代码。

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 代码

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();
    }

【问题讨论】:

    标签: java google-app-engine jpa google-cloud-datastore one-to-many


    【解决方案1】:

    您可以从子实体的键中获取父 id,但您必须从数据存储中获取父实体。

    您可以在每个员工实体中存储一个部门名称,这样可以省去一次获取操作,但代价是额外的数据和更复杂的代码(您必须考虑部门名称更改等情况)。

    更好的解决方案是将部门实体保留在内存缓存中。通过这种方式,您可以节省前往数据存储的时间,并使您的数据模型和代码保持简单。

    【讨论】:

    • +1 几乎涵盖了所有内容。我认为复制选项是最准确的答案:Is there any way to get the information about parent entity from its child entity ?
    • 非常感谢 Andrei 和 @Michael 的回复。
    【解决方案2】:

    Entity 与 GAE Datastore 的关系的基本思想是:

    实体必须有一个键,其父设置为给定的父实体,例如:

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

    实体必须将子实体键作为其属性:

    Entity parent = createParentEntity();
    Entity child = createChildEnity();
    
    parent.setProperty("fieldName", child.getKey());
    

    这样您可以从子实体中检索父实体,也可以从父实体中获取子实体。希望这会有所帮助。

    【讨论】:

    • 父级与其子级是一对多的关系,您不能在父级实体的fieldName 属性上只存储一个子键。此外,不需要将父密钥存储在子实体中,因为子密钥已经使用父密钥进行了编码。检索父项所需要做的就是根据子键运行祖先查询。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    相关资源
    最近更新 更多