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

  • Home
  • SEARCH
  • 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 3352620
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:03:08+00:00 2026-05-18T02:03:08+00:00

I have a scenario where my application has access to a session for limited

  • 0

I have a scenario where my application has access to a session for limited time windows, during which it must fetch data from the database into memory, and then only use the in-memory data to serve requests.

The data model is a simple one-to-many association such as:

<class name="com.foo.Road" table="road">
    <id name="oid" column="oid"/>

    <map name="carCountMap" fetch="subselect">
        <key column="road_oid" foreign-key="oid"/>
        <index column="time_oid" type="long" />
        <one-to-many class="com.foo.CarCount" />
    </map>

    <map name="truckCountMap" fetch="subselect">
        <key column="road_oid" foreign-key="oid"/>
        <index column="time_oid" type="long" />
        <one-to-many class="com.foo.TruckCount" />
    </map>
</class>

Now suppose that the car and truck count data exists for several years, which is much more than can fit in memory. Furthermore, I’m only really interested in loading the car counts in the last 3 months.

My question is, what is the best way to load this data using hibernate such that:

  • road.getCarCountMap() returns the set of only the car counts in the last 3 months (may be empty)
  • I don’t end up with some crazy cartesian product that takes ages to process
  • No LazyInitializationExceptions are thrown after I close the session

Some things I’ve tried are:

1. Make the carCountMap collection eager and specify a where attribute on the mapping such as:

<map name="carCountMap" fetch="subselect" lazy="false" where="time_oid > 1000">
(similar for truckCountMap)

This fits in best with the collection semantics that I want, but unfortunately it forces me to hardcode a value, so I can’t really refer to the last 3 months. time_oid increases by 1 every day.

2. Define maps as lazy and use an hql query to manually join the 3 tables:

    from Road r
    left outer join fetch r.carCountMap ccm
    left outer join fetch r.truckCoutnMap tcm
    where (ccm.time.oid > :startDate)
      or (tcm.time.oid > :startDate)

The problem with this is that the resulting query returns several millions of rows, whereas it should be 10k roads * 4 measurements per month (weekly) * 3 months = ~120k. This query completes in about an hour, which is ridiculous as approach #1 (which loads the exact same data as far as I’m concerned) completes in 3 minutes.

3. Define maps as lazy and load the roads first with a criteria, then run additional queries to fill in the collection

    List roadList = session.createCriteria(Road.class).list();

    session.getNamedQuery("fetchCcm").setLong("startDate", startDate).list();
    session.getNamedQuery("fetchTcm").setLong("startDate", startDate).list();

    return roadList;

This fires the right queries, but the retrieved car and truck counts don’t get attached to the Road objects in roadList. So when I try to access the counts on any Road object, I get a LazyInitializationException.

4. Define maps as lazy, use criteria.list() to load all roads, iterate through all measurement dates in the last 3 months so force those values to be loaded.

I haven’t tried this yet because it sounds really clunky, and I’m not convinced it’ll get rid of the LazyInitializationException

  • Are there any workarounds to the problems I’ve run into with these methods?
  • Is there a better method altogether?
  • 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-18T02:03:09+00:00Added an answer on May 18, 2026 at 2:03 am

    After digging around some more, it looks like hibernate filters are the exact solution I needed for this.

    They basically provide a construct to have a where attribute on a collection or class, with parameters bound at runtime.

    In the mapping file, define the filter and attach it to the collections:

    <class name="com.foo.Road" table="road">
        <id name="oid" column="oid"/>
    
        <map name="carCountMap" fetch="subselect">
            <key column="road_oid" foreign-key="oid"/>
            <index column="time_oid" type="long" />
            <one-to-many class="com.foo.CarCount" />
            <filter name="byStartDate" condition="time_oid > :startDate" />
        </map>
    
        <map name="truckCountMap" fetch="subselect">
            <key column="road_oid" foreign-key="oid"/>
            <index column="time_oid" type="long" />
            <one-to-many class="com.foo.TruckCount" />
            <filter name="byStartDate" condition="time_oid > :startDate" />
        </map>
    </class>
    
    <filter-def name="byStartDate">
        <filter-param name="startDate" type="long"/>
    </filter-def>
    

    Then in the dao, enable the filter, bind the parameter and run the query:

    session.enableFilter("byStartDate").setParameter("startDate", calculatedStartDateOid);
    return session.createCriteria(Road.class).list();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario: I have an application that pulls data from a SQL database as well
I have a scenario. (Windows Forms, C#, .NET) There is a main form which
I have a scenario where users of my ASP.NET web application submit testimonials consisting
I am using SVNKit in my application. I have a scenario wherein certain files
Here's the scenario: You have an ASP.Net application supported by a Microsoft SQL Server
I have this typical scenario. I have a smartclient application built on .net 2.0
The scenario is this We have two applications A and B, both which are
I have a scenario where I have to check whether user has already opened
I have a scenario where I need to upload a file from one web
Scenario: Let's say I have four very similar applications (i.e. most of the functionality

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.