簡體   English   中英

嘗試訪問實體中的BLOB字段時出現org.hibernate.LazyInitializationException

[英]org.hibernate.LazyInitializationException while trying to access a BLOB field in entity

我有一個lazyInitialization問題,嘗試使用StreamedContent Type從mysql數據庫將圖像(BLOB)加載到<p:graphicImage> (Primefaces)中。 我正在使用JSF + Spring + Hibernate,並且在嘗試加載圖像時收到org.hibernate.LazyInitializationException 這是代碼。 謝謝。

這是背景豆:

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>
 Categorie ctg_aux = categorieService.getCategorie(id);

我認為在上面的代碼行中,只有ctg_aux被加載,但不會急於初始化。 含義例如,如果您在Categorie有一些集合,則僅加載ctg_aux不會加載該集合。 稍后在實際訪問該集合時將延遲加載。 但是在訪問該集合時,會話必須是開放的(實體必須處於持久狀態)。 但是此時,如果實體處於分離狀態,您將看到該問題。

我猜只是在檢索到實體后立即在categorieService.getCategorie調用ctg_aux.getImage()方法也應該使休眠也加載該字段。

更新:

在下面的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM