2014-06-11 78 views
-1

這是我的Controller方法,我試圖通過提供zip,cityname和省名稱來讀取我的數據庫。使用gson序列化對象並獲取LazyInitializationException

@RequestMapping(value = "/retrieve", method = RequestMethod.GET) 

public @ResponseBody String retrieveObjectThroughAjax(ModelMap model){ 

//Calling Service Method to read data according to zip,cityName and province provide 

PropertyItems propertyItems=getPropertyTypeandAddressService.readAddressFromZip("H2H-  
           2N3","Montreal","Quebec"); 

     Gson gson = new Gson(); 
     String json = null; 
     try{ 

      json = gson.toJson(propertyItems); // serializing object 

     }catch(Exception e){ 
      logger.error(Constants.METHOD_INSIDE_MESSAGE +"getAuthors",e); 

     } 
     logger.debug(json); 
     return json; 
    } 

    } 

服務方法

@Service 
public class GetPropertyTypeandAddressServiceImpl implements GetPropertyTypeandAddressService{ 

    @Autowired 
    private GetPropertyTypeandAddressDAO getPropertyTypeandAddressDAO; 

    @Transactional 
    public PropertyItems readAddressFromZip(String zipCode,String cityName,String provinceName){ 
     PropertyItems propertyItems=getPropertyTypeandAddressDAO.getAddressFromZip(zipCode, cityName, provinceName); 
     Hibernate.initialize(propertyItems); 
     return propertyItems; 

    } 
} 

DAO方法

@Repository 
public class GetPropertyTypeandAddressDAOimp implements GetPropertyTypeandAddressDAO{ 
    @Autowired 
    private SessionFactory sessionFactory; 

    @Override 
    public PropertyItems getAddressFromZip(String zipCode,String cityName,String provinceName) { 
     PropertyItems propertyitems = new PropertyItems(); 
      Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PropertyItems.class,"propertyItemsClass"); 
      if(zipCode != null){ 
      criteria.createAlias("propertyItemsClass.address","address"); 
      criteria.add(Restrictions.eq("address.zip",zipCode)); 
      List<PropertyItems> propertyitem = criteria.list(); 
      if(propertyitem.size()>0){ 
       propertyitems = propertyitem.get(0);  
      } 
      } 
      else if(cityName != null){ 
       criteria.createAlias("propertyItemsClass.address","address"); 
       criteria.add(Restrictions.eq("address.city","city")); 
       criteria.add(Restrictions.eq("city.cityname",cityName)); 
       List<PropertyItems> propertyitem = criteria.list(); 
        if(propertyitem.size()>0){ 
         propertyitems = propertyitem.get(0);  
        } 
       } 
      else if(provinceName != null){ 
       criteria.createAlias("propertyItemsClass.address","address"); 
       criteria.add(Restrictions.eq("address.city","city")); 
       criteria.add(Restrictions.eq("city.provinces","provinces")); 
       criteria.add(Restrictions.eq("provinces.provinceName",provinceName)); 
       List<PropertyItems> propertyitem = criteria.list(); 
        if(propertyitem.size()>0){ 
         propertyitems = propertyitem.get(0);  
        } 

       } 
     return propertyitems; 
    } 
} 

控制檯錯誤

09:53:56,988 ERROR HelloController:567 - Inside Method: getAuthors org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.agilemaple.common.entity.Property.propertyType, no session or session was closed 
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383) 
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375) 

按照要求我的地產項目這個樣子

實體: PROPERT項目

@Entity 
@Table(name="web_property_item") 
public class PropertyItems { 
    @Id 
    @GeneratedValue 
    private Integer id; 

    private String name; 

    @ManyToOne 
    @JoinColumn(name="property_type_id") 
    private PropertyType propertyType; 


    @OneToOne(mappedBy="propertyItems",cascade=CascadeType.ALL) 
    private Address address; 



    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public PropertyType getPropertyType() { 
     return propertyType; 
    } 

    public void setPropertyType(PropertyType propertyType) { 
     this.propertyType = propertyType; 
    } 

    public Address getAddress() { 
     return address; 
    } 

    public void setAddress(Address address) { 
     this.address = address; 
    } 

實體:物業類型

@Entity 
@Table(name="web_property_type") 
public class PropertyType { 

    @Id 
    @GeneratedValue 
    private Integer id; 

    private String name; 

    @ManyToOne 
    @JoinColumn(name="property_id") 
    private Property property; 

    @OneToMany(fetch = FetchType.LAZY,mappedBy="propertyType", cascade=CascadeType.ALL) 
    private Set<PropertyItems> propertyItems; 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public Property getProperty() { 
     return property; 
    } 

    public void setProperty(Property property) { 
     this.property = property; 
    } 

    public Set<PropertyItems> getPropertyItems() { 
     return propertyItems; 
    } 

    public void setPropertyItems(Set<PropertyItems> propertyItems) { 
     this.propertyItems = propertyItems; 
    } 

} 
+1

'PropertyItems'看起來像什麼?注意'Hibernate.initialize()'不會級聯,所以如果你在對象圖上進一步加載了屬性,它們將不會被加載,當然會在訪問時導致'LazyInitializationException' – geoand

+0

我在我的問題中添加了PropertyItems 。 – user2985842

+0

發貼請輸入PropertyType類的代碼 – Al1en313

回答

0

處於休眠的問題。你的字段屬性集有懶惰的獲取方法,這意味着當你調用這個集合的方法get的時候它會嘗試獲取。當你調用json方法時,gson調用所有對象的方法,但在這一刻,hibernate會話關閉,hibernate無法在控制器中打開它。我面臨同樣的問題,但直接在JSP上。在三個星期內,我通過一個hibernate解決了它的問題(在你的情況下),並且我編寫了代碼在視圖攔截器中打開會話。我現在在地下,所以我不能顯示財產,但在一個小時內我將編輯此答案並添加財產。

加了: 我記得了!物業是:hibernate.enable_lazy_load_no_trans = true

如果它不會幫助,我會添加opensessioninviewinterceptor的代碼。

@Override 
public void addInterceptors(InterceptorRegistry registry) { 
    OpenSessionInViewInterceptor sessionInViewInterceptor = new OpenSessionInViewInterceptor(); 
    sessionInViewInterceptor.setSessionFactory(sessionFactory()); 
} 
+0

謝謝,我在等待 – user2985842

+0

我記得,加了它。 – dikkini

+0

不,它不起作用 – user2985842

相關問題