【发布时间】:2015-11-24 05:36:30
【问题描述】:
我正在学习 RequestFactory。我有简单的示例工作。现在我想从下面为 RF 实施这些实体:
服务器包
@Entity
public class Pizza implements Identifiable, Versionable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Version
private Long version;
private String name;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Ingredient> ingredients;
/* Getters and Setters */
}
@Entity
public class Ingredient implements Identifiable, Versionable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Version
private Long version;
private String name;
private boolean vegan;
/* Getters and Setters */
}
这是DAO 的Pizza Entity 类:
@Override
public List<Pizza> get() {
CriteriaBuilder cb = JPA.em().getCriteriaBuilder();
CriteriaQuery<Pizza> q = cb.createQuery(Pizza.class);
Root<Pizza> c = q.from(Pizza.class);
q.select(c);
TypedQuery<Pizza> query = JPA.em().createQuery(q);
List<Pizza> results = query.getResultList();
for(Pizza p: results) {
for(Ingredient i: p.getIngredients()) {
logger.info(i.getName());
}
}
return results;
}
共享包
我为这些类编写了代理:
@ProxyFor(value = Pizza.class, locator = PizzaLocator.class)
public interface PizzaProxy extends EntityProxy {
public Long getId();
public String getName();
public void setName( String name );
public Long getVersion();
public List<IngredientProxy> getIngredients();
public void setIngredients( List<IngredientProxy> ingredients )
}
@ProxyFor(value = Ingredient.class)
public interface IngredientProxy extends EntityProxy {
public void setId(Long id);
public Long getId();
public Long getVersion();
public void setVersion(Long version);
public String getName();
public void setName(String name);
public boolean isVegan();
public void setVegan(boolean vegan);
}
射频相关接口:
public interface RequestFactoryServices extends RequestFactory {
PizzaRequest pizzaRequest();
}
@Service(value = PizzaDao.class, locator = DaoLocator.class)
public interface PizzaRequest extends RequestContext {
Request<PizzaProxy> findById( Long id );
Request<Void> save( PizzaProxy pizza );
Request<List<PizzaProxy>> get();
}
客户端包
这是我从服务器获取数据的方式:
List<PizzaProxy> pizzas = new LinkedList<PizzaProxy>();
PizzaRequest context = createFactory().pizzaRequest();
context.get().to(new Receiver<List<PizzaProxy>>() {
@Override
public void onSuccess(List<PizzaProxy> response) {
for(PizzaProxy p: response) {
RootPanel.get().add(new Label(
p.getId() + " " +
p.getName() + ", " +
p.getVersion() + ", " +
p.getIngredients()
));
}
}
}).fire();
正如您在get() 方法中的DAO 类中所见,我正在打印以记录有关成分的信息。在服务器端一切正常。
问题是当我调用p.getIngredients() 时,我得到的是null,而不是IngredientsProxies 的列表。
这是因为我没有 Ingredients Entity 的 Dao 和 Locator 类吗?
请帮忙。
回答: 实体关系
对相关实体的更改可以保存在单个请求中。例如,GWT trunk 中 DynatableRF 示例应用程序中的这段代码同时创建了一个新的 Person 和 Address:
PersonRequest 上下文 = requestFactory.personRequest(); AddressProxy 地址 = context.create(AddressProxy.class); PersonProxy person = context.create(PersonProxy.class); person.setAddress(地址); context.persist().using(person).fire(...); RequestFactory 在单个请求中自动发送整个对象图。在这种情况下,服务器上 Person.persist() 的实现也负责持久化相关的地址,这可能会或可能不会自动发生,具体取决于 ORM 框架和关系的定义方式。请注意,RequestFactory 目前不支持嵌入对象(各种 ORM 框架中的 @Embedded),因为它希望每个实体都以自己的 ID 独立存在。
查询服务器时,RequestFactory 不会自动填充对象图中的关系。为此,请在请求上使用 with() 方法并将相关属性名称指定为字符串:
请求 findReq = requestFactory.personRequest().find(personId).with("address"); 还需要使用 with() 方法来检索具有扩展 ValueProxy 类型的任何属性。 with() 方法接受多个 String 参数,因此您可以一次指定多个属性名称。要指定嵌套属性,请使用点表示法。综上所述,你可能有
请求 findReq = find(personId).with("phone","address.city","address.zip")
【问题讨论】:
标签: java gwt requestfactory