Reputation: 33605
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.
Upvotes: 0
Views: 1301
Reputation: 10730
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:
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).
Upvotes: 2