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

标签 java hibernate

我有两个类:

学生工资

在数据库中,表 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/

相关文章:

java - JPA 配置到底是什么?

java - windows7 x64应该下载哪个java开发工具?

java - 野蝇被sqlite jdbc杀死

spring - Spring 中的两个实体管理器

java - 如何获取 Hibernate 查询结果作为列表或 HashMap 的关联数组

java - 在 Hibernate Criteria 中将日期转换为字符串

spring - 对依赖于@OneToMany 集合的类进行单元测试

Java:异常

java - thread.sleep() 的替代方案 - 亚马逊 selenium webdriver 添加到购物车忽略保险对话框

java - 默认线程,如 DestroyJavaVM、Reference Handler、Signal Dispatcher