0

i am using hibernate 3.5.1-Final, with spring 3.0.5.RELEASE and i am using the following configuration for 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>

suppose that i have the following entity:

@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 ;

}

and my backing bean is as follows:

@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);
    }



}

my service is as follows:

@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();
        }

    }


}

my xhtml page is as follows:

<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>

in the xhtml page the lazy loading works with no problems, and the file data is displayed correctly, but when deleting the file, i am getting the following error in the delete service method: AdPageService.deleteAdPage

Could not initialize proxy - no Session

please advise how to fix this error.

1 Answer 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).

Sign up to request clarification or add additional context in comments.

2 Comments

,is there's a way to check if the entity is detached ? what i want to do is to reload the entity in the service method if it is detached only, because this method is used in other places where the entity might not be detached.
@Msaleh: You can query the Hibernate session directy: session.contains(myEntity) ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.