2012-01-22 52 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