2012-08-25 66 views
1

我跟着https://developers.google.com/web-toolkit/articles/mvp-architecture。 在他们的模型中,他们使用一个类'Contact'和一个名为'ContactDetails'的轻类型。 因为我不需要轻量版本,所以我删除了ContactDetails并将其替换为Contact。避免使用DTO类?

现在我遇到的异常像

Type 'org.eclipse.persistence.indirection.IndirectList' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.: instance = {IndirectList: not instantiated} 

的原因是,因为我发现在这里(http://stackoverflow.com/a/6778986/1141785),该联系类是一类使用持久性API技术,不应该发送到电线。

那么ContactDetails类是我应该通过电线发送的DTO类吗? 有没有办法避免使用这个额外的类?

我想避免使用'light'版本的原因是,我想使用FieldUpdater编辑DataGrid中的Contact类。

当我使用DTO课程时,我有什么优势吗?

我该如何避免在Contact和ContactDetails类中有这么多重复的代码?

+1

有关于这一主题的专文:https://developers.google.com/web-toolkit/articles/using_gwt_with_hibernate(但要注意,基列不再是一个活跃的项目) –

+0

我没有看过这篇文章,因为它是关于休眠。但是,当你遇到相同的问题时,使用JPA时也值得阅读!谢谢! – user1141785

回答

1

当使用JPA,你可以有一个包含列表类,如:

public class Contact implements IsSerializable { 
    // ... 
    private List<Address> addresses; 
    // ... 
} 

但在创建联系人时,地址设置为IndirectList的一个实例。这允许从数据库延迟加载集合,但不能在客户端上运行。在将其发送给客户端之前,您需要将其替换为ArrayList之类的东西。您可以将其设置为空列表,或将内容复制到新列表中。

如果列表中的元素本身包含集合,那么您还需要替换这些集合。您需要小心循环引用,并从数据库中提取太多项目。

0

只需用fetchtype eager注释您的List。

public class Contact implements IsSerializable { 

    @ElementCollection(fetch = FetchType.EAGER) 
    private List<Address> addresses; 

}