Announcement Announcement Module
Collapse
No announcement yet.
Lazy Initalization Exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lazy Initalization Exception

    Dear all,
    I am new to Hibernate and getting lazy initalization exception when I tried to access the another class object.

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

    I have 2 simple classes book & booktype.

    The hibernate throws an lazy initalization exception, if I access the booktype
    List<Book> books = bookManager.getAllBooks()[0].getType();

    The same code works if I turn off the lazy initialization settings.

    Could anyone help me, where exactly going wrong in the lazy initalization setting. Your help will be much appreciated.


    book.hbm.xml file
    ------------------
    <class name="Book" table="book" batch-size="10">

    <id name="lngBookId" type="long" column="id" unsaved-value="0">
    <generator class="identity"/>
    </id>

    <property name="strBookName">
    <column name="bookname" />
    </property>

    <many-to-one name="type" class="abs.rose.BookType" column="type"/>
    </class>


    booktype.hbm.xml
    -----------------
    <class name="BookType" table="booktype" batch-size="10">

    <id name="id" type="long" column="id" unsaved-value="0">
    <generator class="identity"/>
    </id>

    <property name="type">
    <column name="type" />
    </property>

    <property name="desc">
    <column name="description"/>
    </property>

    <set name="books" inverse="true">
    <key column="id"/>
    <one-to-many class="Book" />
    </set>
    </class>


    I have a getAllBooks() method bookManager class
    -------------------------------------------------------
    public List<Book> getAllBooks() {
    final Criteria criteria = sessionFactory.getCurrentSession().createCriteria( Book.class);
    final List<Book> bookList = new ArrayList<Book>(0);
    for (Object book : criteria.list()) {
    bookList.add((Book) book);
    }

    return Collections.unmodifiableList(bookList);
    }

  • #2
    I'm sure you can find a lot(!) of answers, documentation etc. at hibernate.org. This is probably the most common problem newbees experience with Hibernate, and is due to the fact that the session needs to be open to access dependency graphs.
    If you are experiencing this in a Spring managed web app, try OpenSessionInViewInterceptor.

    Good luck!

    Comment

    Working...
    X