gpt4 book ai didi

java - Hibernate org.hibernate.LazyInitializationException : could not initialize

转载 作者:太空宇宙 更新时间:2023-11-04 06:21:24 26 4
gpt4 key购买 nike

我有两个类:

学生工资

在数据库中,表 Etudiant 具有表 Pays 的外键。

在我的代码中我有这样的东西:

List<Etudiant> listEtudiants = (List<Etudiant>) etudiantService.getAll();

for(Etudiant etudiant : listEtudiants) {
if(((JTextField)arg0.getSource()).getText().equals(etudiant.getNom())){
System.out.println(etudiant.getPays().getNom());
}
}

但是当我运行此代码时,它失败并出现异常:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

在行中:

System.out.println(etudiant.getPays().getNom());

学生映射:

<hibernate-mapping>
<class name="tp.ihm.domain.Etudiant" table="etudiant" schema="public" optimistic-lock="version">

<id name="numInsc" type="java.lang.Long">
<column name="num_insc" />
<generator class="assigned" />
</id>
<many-to-one name="pays" class="tp.ihm.domain.Pays" fetch="select">
<column name="pays" not-null="true" />
</many-to-one>

<property name="nom" type="string">
<column name="nom" length="50" not-null="true" />
</property>

<property name="prenom" type="string">
<column name="prenom" length="50" not-null="true" />
</property>

</class>
</hibernate-mapping>

付款映射:

<hibernate-mapping>
<class name="tp.ihm.domain.Pays" table="pays" schema="public" optimistic-lock="version">

<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="assigned" />
</id>

<property name="nom" type="string">
<column name="nom" length="45" not-null="true" />
</property>

<set name="etudiants" table="etudiant" inverse="true" lazy="true" fetch="select">
<key>
<column name="pays" not-null="true" />
</key>
<one-to-many class="tp.ihm.domain.Etudiant" />
</set>

</class>
</hibernate-mapping>

我尝试删除 Pays 映射中的 fetch 属性,然后将其值更改为 eager,但没有任何效果。

有人可以帮我解决这个问题吗?

编辑:

这是 getAll 方法的代码:

public List getAll() throws EntityNotFoundException {

// Get the current session
Session s = getSession();
List list = null;

// If the BLL layer started a transaction
// In this case it is the BLL layer that manages the session and transaction
if (anActiveTransactionExist(s)) {
list = s.createCriteria(Etudiant).list();
} else {
LOGGER.debug("DAO initialize its own transaction");
Transaction tx = null;
try {

// Starts a transaction locally
tx = s.beginTransaction();
list = s.createCriteria(boClass).list();
tx.commit();
} catch (RuntimeException ex) {
// Cancel the transaction if there is a problem
handleDaoOpError(tx, ex);
} finally {
closeSession(s);
}
}
if (list == null || list.size() == 0)
throw new EntityNotFoundException();
return list;
}

最佳答案

您需要将 Etudiant 的映射从 fetch=select 更改为 fetch=join

fetch-“join” = Disable the lazy loading, always load all the collections and entities.
fetch-“select” (default) = Lazy load all the collections and entities.

<many-to-one name="pays" class="tp.ihm.domain.Pays" fetch="join">
<column name="pays" not-null="true" />
</many-to-one>

关于java - Hibernate org.hibernate.LazyInitializationException : could not initialize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27377993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com