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
請告知如何解決這個錯誤。
,有沒有辦法檢查實體是否分離?我想要做的是重新加載服務方法中的實體,如果它只是分離的,因爲這個方法用於實體可能不被分離的其他地方。 – 2012-07-08 08:04:43
@Msaleh:你可以查詢Hibernate session directy:'session.contains(myEntity)'... – jeha 2012-07-08 10:40:29