【问题标题】:GWT RequestFactory: how to handle entities with composite primary keysGWT RequestFactory:如何处理具有复合主键的实体
【发布时间】:2011-06-24 12:24:16
【问题描述】:

RequestFactory 可以处理复合主键吗?

documentation 提到实体必须实现getId();如果实体没有单个“id”字段,而是有多个外键字段共同构成复合主键,该如何实现?

【问题讨论】:

    标签: gwt requestfactory


    【解决方案1】:

    在 GWT 2.1.1 中,Id 和 Version 属性可以是 RequestFactory 知道如何传输的任何类型。基本上,任何原始类型 (int)、盒装类型 (Integer) 或任何具有关联代理类型的对象。您不必自己将复合 id 简化为 String; RF 管道可以通过使用实体类型键的持久 id 或值类型键的序列化状态来自动处理复合键。

    使用之前发布的示例:

    interface Location {
      public String getDepartment();
      public String getDesk();
    }
    
    interface Employee {
      public Location getId();
      public int getVersion();
    }
    
    @ProxyFor(Location.class)
    interface LocationProxy extends ValueProxy {
      // ValueProxy means no requirement for getId() / getVersion()
      String getDepartment();
      String getDesk();
    }
    @ProxyFor(Employee.class)
    interface EmployeeProxy extends EntityProxy {
      // Use a composite type as an id key
      LocationProxy getId();
      // Version could also be a complex type
      int getVersion();
    }
    

    如果您无法将标识简化为域类型上的单个 getId() 属性,则可以使用 Locator 来提供外部定义的 id 和版本属性。例如:

    @ProxyFor(value = Employee.class, locator = EmployeeLocator.class)
    interface EmployeeProxy {.....}
    
    class EmployeeLocator extends Locator<Employee, String> {
      // There are several other methods to implement, too
      String getId(Employee domainObject) { return domainObject.getDepartment() + " " + domainObject.getDesk(); }
    }
    

    问题中链接的 DevGuide 在以下方面有点过时了 RequestFactory changes in 2.1.1

    【讨论】:

    • 应该提到复合ID必须映射为ValueProxy(上面代码中的LocationProxy),并且该代理必须暴露在代理中的某处(即可达当您从 GWT.create()d RequestFactory 开始遍历接口和方法时;在上面的示例代码中,可以从 EmployeeProxy 上的 getId 访问它。
    • 哦,顺便说一句,版本不必在代理上公开。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 2011-11-05
    • 2013-12-24
    • 2023-04-08
    • 2012-10-18
    • 2013-01-15
    • 1970-01-01
    相关资源
    最近更新 更多