2011-12-20 41 views
0

我有一個lazyInitialization問題,嘗試使用StreamedContent類型從mysql數據庫加載圖像(BLOB)到<p:graphicImage>(Primefaces)。我正在使用JSF + Spring + Hibernate,當我嘗試加載圖像時,我得到了org.hibernate.LazyInitializationException。這裏是代碼。感謝名單。org.hibernate.LazyInitializationException嘗試訪問實體中的BLOB字段

這裏是支持bean:

public class AccueilBean implements Serializable { 

    private CategorieService categorieService; 
    private List<Categorie> categories; 
    private StreamedContent dbImg;   




public AccueilBean(){ 


} 


@PostConstruct 
public void init() { 

    this.categories = new ArrayList<Categorie>(); 

    categories=categorieService.listerCategorie(); 

} 

public List<Categorie> getCategories(){ 

    return this.categories; 
} 



public CategorieService getCategorieService() { 
    return categorieService; 
} 


public void setCategorieService(CategorieService categorieService) { 
    this.categorieService = categorieService; 
} 


public StreamedContent getDbImg() { 

    InputStream dbStream = null; 
    dbImg = null; 

    FacesContext context = FacesContext.getCurrentInstance(); 
    Categorie cat = context.getApplication().evaluateExpressionGet(context, "#{cat}", Categorie.class); 

    Long id = cat.getId(); 
    Categorie ctg_aux = categorieService.getCategorie(id); 
    System.out.println(ctg_aux.getImage()); 
    try { 
     dbStream = ctg_aux.getImage().getBinaryStream(); 
     dbImg = new DefaultStreamedContent(dbStream,"image/jpeg"); 
     } 
    catch (SQLException e) {System.out.println("erreur");} 


    return dbImg; 
} 

} 

這裏是.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"  xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui"> 

<h:head></h:head> 
<body> 
<h:form> 
<p:dataGrid columns="3" var="cat" value="#{accueilBean.categories}"> 

<p:column> 

<p:graphicImage value="#{accueilBean.dbImg}" > 
</p:graphicImage> 
</p:column> 

</p:dataGrid> 
</h:form> 

</body> 
</html> 

這裏是Categorie實體的映射;

<class name="tn.projet.model.Categorie" table="CATEGORIE"> 
<id name="id" type="java.lang.Long"> 
<column name="ID" /> 
<generator class="increment" /> 
</id> 
<property name="nom" type="java.lang.String"> 
<column name="NOM" /> 
</property> 
<property name="desc" type="java.lang.String"> 
<column name="DESCRIPTION" /> 
</property> 
<property name="image" type="java.sql.Blob"> 
<column name="IMAGE" /> 
</property> 
<set name="produits" table="PRODUIT" inverse="false" lazy="true"> 
<key> 
<column name="ID" /> 
</key> 
<one-to-many class="tn.projet.model.Produit" /> 
</set> 
</class> 
+0

你能告訴如何的'CategorieService#getCategorie(ID)'方法看起來身體喜歡? – 2011-12-20 21:51:07

+0

我已經做到了,有什麼建議嗎? – 2011-12-21 10:13:42

回答

0
Categorie ctg_aux = categorieService.getCategorie(id); 

我覺得跟上面一行,只有ctg_aux是負載而不是急切地初始化。意義例如如果您在Categorie中收集了一些內容,只需加載ctg_aux即可加載該集合。稍後當實際訪問該集合時,它將被延遲加載。但是會話必須是開放的(實體必須處於持久狀態),同時訪問該集合。但是,如果實體處於分離狀態,您將看到這個問題。

我想只要在categorieService.getCategorie之內調用ctg_aux.getImage()方法後,你已經檢索到實體應該讓hibernate加載該字段。

更新:

在下面:

<property name="image" type="java.sql.Blob"> 
    <column name="IMAGE" /> 
</property> 

嘗試設置lazy="false"

<property name="image" type="java.sql.Blob" lazy="false"> 
    <column name="IMAGE" /> 
</property> 
+0

thx答覆,問題是我有一個'圖像'保存在數據庫中,並通過休眠映射爲BLOB屬性,但是當我加載'datagrid'中的Categorie,然後試圖檢索圖像我的東西會話丟失!任何解決方案? – 2011-12-20 21:56:05

+0

您無權訪問'CategorieService#getCategorie(id)'方法? – 2011-12-20 21:58:06

+0

@ user1108701:或者您可以嘗試在實體中的該字段上使用'@Basic(fetch = FetchType.EAGER)'之類的東西。 – 2011-12-20 22:01:31

相關問題