GWT RequestFactory:如何处理具有复合主键的实体

Geo*_*old 4 gwt requestfactory

RequestFactory可以处理复合主键吗?

文件提到,实体必须实现getId(); 如果实体没有单个"id"字段,而是有多个外键字段一起构成复合主键,那么应该如何实现呢?

Bob*_*obV 7

在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();
}
Run Code Online (Sandbox Code Playgroud)

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

@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(); }
}
Run Code Online (Sandbox Code Playgroud)

从问题链接的DevGuide对于2.1.1中的RequestFactory更改有点过时了