1

I have two classes :

Etudiant and Pays

In the database the table Etudiant has a foreign key for the table Pays.

In my code I have something like this :

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());
    }
}

but when I run this code it fails with the exception:

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

In the line :

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

Mapping for Etudiant:

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

Mapping for Pays:

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

I tried to remove the fetch attribute in the mapping for Pays, and then to change it's value to eager but nothing works.

Could someone please help me with this ?

Edit :

This is the code for the getAll method :

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;
    }
8
  • show us your getAll method in etudiantService Commented Dec 9, 2014 at 11:39
  • @SajanChandran the exception is raised from the line where I call etudiant.getPays().getNom() the getAll method does works. Commented Dec 9, 2014 at 11:47
  • 2
    you must be trying to read pays from etudiants after your hibernate session is closed. Commented Dec 9, 2014 at 11:52
  • @AimadMAJDOU just because a particular line of code produces an exception does not mean the actual cause of the problem is on that very same line. Deflecting requests for more code is exactly the opposite of what you want to do, unless you came here to not get helped. Commented Dec 9, 2014 at 11:57
  • Fetch condition can do the trick fetch="join" in many-to-one condition in Entity class please refer this link lazy error Commented Dec 9, 2014 at 12:00

1 Answer 1

1

You need to change the mapping of Etudiant from fetch=select to 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>
Sign up to request clarification or add additional context in comments.

3 Comments

Is it good to work with fetch="join" because it will retrieve some information that we don't need.
In your case since the session is closed, either you have to get all the details before, else open a new session and make the call to get pays. Also you are changing the fetch strategy only for pays relation.
In case its problem to change fetch="join", proxy objects can be manually converted to real object manually as described here. stackoverflow.com/questions/2216547/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.