2012-01-22 50 views
0

这几天我强制与我需要使用GWT作为表示层的应用程序。我学到了一些能够帮助我处理项目的事情,但是现在我有一个问题,我找不到一个好的解决方案。发送对象在春天gwt

的问题是,当我尝试发送我从MySQL中检索与Hibernate弹簧Ø表示层(GWT)的对象,我得到这个异常:

com.google.gwt.user.client.rpc.SerializationException: Type 'org.hibernate.collection.PersistentBag' 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 = [[email protected], [email protected], [email protected]] 

我要提到我想送一名为考试的类,这个类有一个问题类的列表。

我会很感激任何有助于摆脱这个问题的帮助。

回答

1

您应该使用数据传输对象,仅使DTO的可转移到GWT客户端。

您应该创建ExamDto和QuestionDto,并且在从MySQL接收到考试对象后,您必须将其转换为ExamDto。

在客户端,您将只使用DTO。如果您想将考试保存到MySQL,您应该将ExamDto转换为考试。

要将POJO转换为DTO并返回,应使用Dozer

要使用推土机,您需要使用推土机映射来映射DTO和POJO。我使用Custom Mappings Via Dozer XML Files

描述GWT-Hibernate的关系最好的教程:Using GWT with Hibernate

而且,我心中已经创建转换器类DozerGenerator,在我的应用程序中使用它。例如,我有2个RPC--其中一个是查找用户,其次是保存用户。

public UserDto findUserById(long id) throws IllegalArgumentException { 
    //userService.findUserById(long id) returns User object and than 
    //you need to convert it to UserDto to transfer to client. 
     return DozerGenerator.appUserEntityToDto(userService.findUserByID(id)); 
    } 
    //here, you converts UserDto to User 
    public Long saveUser(UserDto userDto) throws IllegalArgumentException {     
     return userService.saveUser(DozerGenerator.appUserDtoToEntity(mapper, userDto)); 
} 

在这里,它的DozerGenerator类:

public class DozerGenerator { 

    /* User <-> UserDto */ 
     public static User appUserDtoToEntity(DozerBeanMapper mapper, UserDto dto) { 
      return mapper.map(dto, User.class); 
     } 

     public static UserDto appUserEntityToDto(DozerBeanMapper mapper, User user) { 
      return mapper.map(user, UserDto.class); 
     } 

     public static List<UserDto> appUserListEntityToDto(DozerBeanMapper mapper, List<User> users) { 
      List<UserDto> models = new ArrayList<UserDto>(); 
      for (User u : users) { 
       models.add(DozerGenerator.appUserEntityToDto(mapper, u)); 
      } 
      return models; 
     } 

     public static List<User> appUserListDtoToEntity(DozerBeanMapper mapper, List<UserDto> dtos) { 
      List<User> users = new ArrayList<User>(); 
      for (UserDto u : dtos) { 
       users.add(DozerGenerator.appUserDtoToEntity(mapper, u)); 
      } 
      return users; 
     } 
} 

另外,我使用GWT +春+ JPA + Hibernate的我的应用程序不需要任何特殊的库,spring4gwt和吉利德(hibernate4gwt),它工作正常。

您还可以在此处找到有关Issue 3296

0

你有join fetch与考试编名单,我认为这是一个懒惰的集合未初始化(填充),但

+0

好啊。你的错误的一些信息使用EAGER取 – Sina