Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6123655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:02:32+00:00 2026-05-23T16:02:32+00:00

I am running into a problem managing my Hibernate-backed collections. The problem is illustrated

  • 0

I am running into a problem managing my Hibernate-backed collections. The problem is illustrated in the code below; briefly,

  1. A single session and transaction are started.
  2. All instances of ClassA are loaded and walked to force Hibernate to load all instances of ClassC. At this point all instances of ClassC actually load via lazy fetching.
  3. In the same transaction and session, all instances of ClassB are loaded.
  4. All instances of ClassC are no longer available (the containing collections are empty) and do not lazy load if requested.

What’s the best way to load up the data sets needed and keep them in memory?

Notes:

The project is in rapid-prototyping mode so I am using a single session and single
transaction to load up two big chunks of the application’s data. This is also a desktop application so the use of a single session and transaction may not even be a problem in the long run if the code were working.

I set up ehcache and the debugger showing that Hibernate found the ehcache config file. The cache is set such that

maxElementsInMemory="5000000"
eternal="true" 

the timeouts are set to 20 mins, even though eternal is set true, just to be sure the session isn’t timing out.

The data set size is not particularly large, about 100k records.
The Hibernate 3.0 module packaged with NetBeans was use to create the class templates from a data model.


The mapping file looks roughly like this:

<hibernate-mapping>
  <class catalog="myCatalog" name="ClassA" table="classA">
    <id ... </id>
    <set inverse="true" name="classCs" sort="natural">
      <key>
        <column length="12" name="mykey" not-null="true"/>
      </key>
      <one-to-many class="ClassC"/>
    </set>
  </class>
</hibernate-mapping>

Class definitions:

@Entity
@Table(name = "classA", catalog = "myCatalog")
public class ClassA implements java.io.Serializable {
   private SortedSet<ClassC> classCs = new TreeSet<ClassC>();

   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "ClassA")
   public SortedSet<ClassC> getClassCs() {
      return this.classCs;
   }
}

@Entity
@Table(name = "classB", catalog = "myCatalog")
public class ClassB implements java.io.Serializable {
    // ... It is possible to walk collections in this class to reach some instances of classA.
}

public class ClassC implements java.io.Serializable {
    // ... contains no collections
}

Manifestation of problem:

// in Hibernate Util   
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

public static void main(String[] args) {
    Application.session = HibernateUtil.getSessionFactory().openSession();
    Application.tx = session.beginTransaction();

    // Load data set 'A' and force collections to load
    List<ClassA> listClassA = new ArrayList<ClassA>();
    Query q = Application.session.createQuery("from ClassA as a").setReadOnly(true);
    Iterator<ClassA> iterator = q.list().iterator();
    while (iterator.hasNext()) {
        ClassA ca = (ClassA)iterator.next();
        // force member collection to load -- at present this is necessary even though FetchType is EAGER
        ca.getClassCs(); 
        listClassA.add(ca);
    }           

    // The collections of classCs are in are in memory here
    for(ClassA a : listClassA){
        log.info(a.getId() + "-" + a.getClassCs().size());
    } 

    // Load data set 'B'
    List<ClassB> listClassB = new ArrayList<ClassB>();
    String sq = "from classB as b where ...";
    Query q = Application.session.createQuery(sq);
    listClassB.addAll(q.list());

    // The collections of classCs are NOT in memory and touching collections does not force reload
    // The collections exist but now all have size zero
    for(ClassA a : listClassA){
        log.info(a.getId() + "-" + a.getClassCs().size());
    }
    return;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:02:32+00:00Added an answer on May 23, 2026 at 4:02 pm

    I turns out that Hibernate can optionally flush the session cache before each query. One of the settings to control the session cache is session.setFlushMode(). In Hibernate 3.2.5, setting flush mode to:

    FlushMode.COMMIT or FlushMode.MANUAL
    

    achieves the desired result. I found this while reading “Hibernate in Action”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running into a problem with method overwriting. Look at the src code below,
Been running into a problem ever since I started using rvm to manage my
I'm running into a problem with some of my code on iPhone3GS running iOS5.0
I'm running into a problem that I can't seem to solve. The code works
I'm running into a problem while trying to compile my code: D:\workspace>javac DbConnect.class javac:
I am running into this problem when trying to recompile a single class inside
Running into a problem where on certain servers we get an error that the
Running into a problem. I have a table defined to hold the values of
Been running into this problem lately... When debugging an app in VS.Net 2005, breakpoints
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.