问题:

后台将获取的List对象转化为json格式数据传到前台时,本来List中是存在数据的,但是通过gson转化后为空。如:

Type type= new TypeToken<List<EmpBaseInf>>() {
  }.getType();
  StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
    .append(filter.getPagingBean().getTotalItems()).append(
      ",result:");
  Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
    .setDateFormat(Constants.DATE_FORMAT_YMD).create();
  buff.append(gson.toJson(list, type));

问题原因:

因为empBaseInf为主表,其从表中引用了EmpBaseInf对象,这样如果在获取list之前处理过从表的数据,那么此时的list中的对象均为引用对象,并非实际对象本身。

解决办法:

在从表的hibernate配置文件中添加:lazy="false"。

<many-to-one
   name="empBaseInf"
   class="com.xtsoft.entity.hr.doc.EmpBaseInf"

   lazy="false"
            not-null="false"
   fetch="select"
  >