2012-07-05 61 views
0

我使用休眠3.5.1決賽,與春天3.0.5.RELEASE ,我使用下面的配置的OpenSessionInViewFilter延遲加載不@服務類工作

<filter> 
    <filter-name>hibernateFilter</filter-name> 
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 
    <init-param> 
     <param-name>sessionFactoryBeanName</param-name> 
     <param-value>sessionFactory</param-value> 
    </init-param> 
    </filter> 
    <filter-mapping> 
    <filter-name>hibernateFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
    <dispatcher>FORWARD</dispatcher> 
    <dispatcher>REQUEST</dispatcher> 
    <dispatcher>ERROR</dispatcher> 
    </filter-mapping> 

假設我有以下實體

@SuppressWarnings("serial") 
@Entity 
@Table(name = "adpage", catalog = "mydb") 
public class Adpage implements java.io.Serializable { 

    @Id 
    @Column(name = "pkid", nullable = false, length = 50) 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(fetch = FetchType.EAGER) 
    private long pageId; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "audio_file_id", unique = true, nullable = true) 
    private AudioFile audioFile ; 

} 

和我支持bean如下:

@Component("myBean") 
@Scope("view") 
public class MyBean { 

    @Autowired 
    private AdPageDao adPageDao; 

    @Autowired 
    private AdPageService adPageService; 

    public void preRender() { 
       adPageObj = adPageDao.getAdPageByID(adPageId); 
    } 

    public void deleteAdPage(Adpage adPage) { 
     adPageService.deleteAdPage(adPage); 
    } 



} 

服務如下:

@Service 
public class AdPageService { 

    @Autowired 
    private AudioFileDao audioFileDao; 

    public void deleteAdPage(Adpage adPage) { 


     if (adPage.getAudioFile() != null) { 
      log.debug("deleting audio file: " 
        + adPage.getAudioFile().getName() + " for adpage: " // exception here 
        + adPage.getName()); 
      audioFileDao.deleteAudioFile(adPage.getAudiofileref()); 
      GeneralUtils.deleteFilePhysically(adPage.getAudioFile() 
        .getName(); 
     } 

    } 


} 

XHTML頁面如下:

<f:event type="preRenderView" listener="#{myBean.preRender}" /> 
<ice:panelGrid columns="2"> 

       <ice:outputLabel id="fileName">File Name:</ice:outputLabel> 
       <ice:outputText value="#{myBean.adPageObj.audioFile.originalName}"></ice:outputText> 

       <ice:outputLabel id="fileLength">File Length:</ice:outputLabel> 
       <ice:outputText value="#{myBean.adPageObj.audioFile.length}"></ice:outputText> 

       <ice:outputLabel id="fileDesc">Description:</ice:outputLabel> 
       <ice:outputText value="#{myBean.adPageObj.audioFile.description}"></ice:outputText> 

       </ice:panelGrid> 

在懶惰加載的xhtml頁面沒有問題,文件數據是正常顯示,但在刪除文件時,我收到以下錯誤在刪除服務方法:AdPageService.deleteAdPage

Could not initialize proxy - no Session 

請告知如何解決這個錯誤。

回答

2

如果AdPage對象已加載到您的視圖中(由於OpenSessionInViewFilter而導致之前的Hibernate會話),則延遲加載不起作用,因爲實體現在是「分離」的。

爲了解決延遲加載問題,你可以這樣做:

  1. reattach the entity當前的Hibernate會話
  2. 做一個預先抓取之前,確保所有屬性都加載
  3. 重裝實體通過它的id (pageId這裏)

我會去選項3(通過它的ID重新加載)來獲得一個新的實體(這可能已經改變,同時顯示/提交表格)。

+0

,有沒有辦法檢查實體是否分離?我想要做的是重新加載服務方法中的實體,如果它只是分離的,因爲這個方法用於實體可能不被分離的其他地方。 – 2012-07-08 08:04:43

+1

@Msaleh:你可以查詢Hibernate session directy:'session.contains(myEntity)'... – jeha 2012-07-08 10:40:29