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;
    }
Community
  • 1
  • 1
Renaud is Not Bill Gates
  • 1,884
  • 24
  • 84
  • 163
  • show us your `getAll` method in `etudiantService` – Sajan Chandran Dec 09 '14 at 11:39
  • @SajanChandran the exception is raised from the line where I call `etudiant.getPays().getNom()` the `getAll` method does works. – Renaud is Not Bill Gates Dec 09 '14 at 11:47
  • 2
    you must be trying to read `pays` from `etudiants` after your hibernate session is closed. – Sajan Chandran Dec 09 '14 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. – Gimby Dec 09 '14 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](http://stackoverflow.com/questions/27102495/how-to-get-hibernate-hierarchy-for-lazy-objects/27120314#27120314) – techGaurdian Dec 09 '14 at 12:00
  • @Gimby please check the modification I made to my post – Renaud is Not Bill Gates Dec 09 '14 at 12:01
  • @SajanChandran thats was I thought earlier then I added this code in my view : `Session s = HibernateUtil.getSessionFactory().openSession(); s.update(etudiants.get(0).getClasse());` and it worked. But I don't want to do this line everytime I want to call such a code. – Renaud is Not Bill Gates Dec 09 '14 at 12:06
  • @AimadMAJDOU : have u tried with fetch = "join" ? – techGaurdian Dec 09 '14 at 12:34

1 Answers1

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>
Sajan Chandran
  • 10,561
  • 3
  • 25
  • 37
  • Is it good to work with fetch="join" because it will retrieve some information that we don't need. – Renaud is Not Bill Gates Dec 09 '14 at 12:36
  • 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. – Sajan Chandran Dec 09 '14 at 13:37
  • In case its problem to change fetch="join", proxy objects can be manually converted to real object manually as described here. http://stackoverflow.com/questions/2216547/converting-hibernate-proxy-to-real-object – Sariq Shaikh Dec 09 '14 at 13:41