您應該使用數據傳輸對象,僅使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
好啊。你的錯誤的一些信息使用EAGER取 – Sina