0

我正在使用hibernate 3.5.1-Finalspring 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

请告知如何解决此错误。

4

1 回答 1

2

If the AdPage object was loaded in your view (a previous Hibernate session due to the OpenSessionInViewFilter), then lazy-loading does not work because the entity is "detached" now.

To solve the lazy-loading problem you could do:

  1. reattach the entity to the current Hibernate session
  2. do an eager fetch before to ensure all attributes are loaded
  3. reload the entity by it's id (pageId here)

I'd go for option 3 (reload by it's id) to get a fresh entity (which could have changed while displaying / submitting the form).

于 2012-07-05T15:08:17.467 回答