繁体   English   中英

Hibernate org.hibernate.LazyInitializationException:无法初始化

[英]Hibernate org.hibernate.LazyInitializationException: could not initialize

我有两节课:

Etudiant支付

在数据库中,表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属性,然后将其值更改为渴望,但没有任何效果。

有人可以帮我吗?

编辑:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM