Domanda

<!-- Clasa Proiect-->
<class catalog="tema3" name="com.tema3.tables.Proiect" table="proiect" lazy="false">
    <id column="proiect_id" name="id">
        <generator class="native"/>
    </id>
    <property name="nume"  type="text" />

    <set name="itemList" table="proiect_item" fetch="select">
        <key column="proiect_id"/>
        <many-to-many column="item_id" unique="true" class="com.tema3.tables.Item"/>
    </set>
</class>    </class>

Questa è la mia classe di mappatura.
Il codice in cui chiamo un'istanza è:

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
 Query q = session.createQuery("from Proiect");
 List<Proiect> lists = HibernateUtil.listAndCast(q);
 session.getTransaction().commit();
 obj = lists;

E questo è ListAdcastMethod:

public static <T> List<T> listAndCast(Query q) {
    @SuppressWarnings("unchecked")
    List list = q.list();
    return list;
}

E continuo a ottenere un:

May 2, 2011 4:38:03 PM org.hibernate.LazyInitializationException <init>
SEVERE: failed to lazily initialize a collection of role: com.tema3.tables.Proiect.itemList, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.tema3.tables.Proiect.itemList, no session or session was closed
 at   org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)

Ma il fatto è che i campi della classe ProIect sono popolati tranne gli articoli impostati in cui ottengo questa eccezione, ma voglio che gli oggetti set siano popolati. Come posso farlo?

È stato utile?

Soluzione 2

Ho trovato la risposta. Deve aggiungere alla dichiarazione impostata in XML la proprietà lazy="false"

Altri suggerimenti

Inizializzare il set di elementi all'interno del listAndCast(Query q) metodo prima di restituire o chiamare un file transaction.commit(). Questo può essere fatto chiamando uno qualsiasi degli accessori su un elemento dal set di articoli. Ciò costringerà Hibernate a inizializzare l'insieme di elementi anziché restituire un elenco di proxy.

MODIFICARE

 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
 session.beginTransaction();
 Query q = session.createQuery("from Proiect");
 List<Project> lists = HibernateUtil.listAndCast(q);
 if(lists != null && lists.size() > 0) {
    Set<Item> s = lists.get(0).getItemList();
    Iterator iter = s.iterator();
    while(iter.hasNext()) {
       Item item = iter.next();
       item.getSomething();
       break;
    }
 }
 session.getTransaction().commit();
 obj = lists;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top