GWT RequestFactory: how to handle entities with composite primary keys
Can RequestFactory handle composite primary keys?
The documentation mentions that entities must implement getId()
; how should this be implemented in the 开发者_Python百科case where entities do not have a single "id" field, but rather have multiple foreign key fields that together constitute a composite primary key?
In GWT 2.1.1, the Id and Version properties may be of any type that RequestFactory knows how to transport. Basically, any primitive type (int
), boxed type (Integer
), or any object that has an associated Proxy type. You don't have to reduce a composite id to a String yourself; the RF plumbing can take care of composite keys automatically by using the persistent id of an entity-type key or the serialized state of a value-type key.
Using the previously-posted example:
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();
}
If you can't reduce the identity to a single getId()
property on the domain type, you can use a Locator
to provide an externally-defined id and version property. For example:
@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(); }
}
The DevGuide linked from the question is a bit out of date with respect to RequestFactory changes in 2.1.1
精彩评论