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 370123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:59:15+00:00 2026-05-12T13:59:15+00:00

Java Persistence with Hibernate shows lots of examples of how to eagerly fetch associated

  • 0

Java Persistence with Hibernate shows lots of examples of how to eagerly fetch associated entities, such as:

  • Adding @org.hibernate.annotations.BatchSize to the associated class
  • Adding @org.hibernate.annotations.Fetch to the field that references the associated class
  • Using the “fetch” keyword in the HQL query, etc…

However in my case, I am dealing with a slow-running process that is responsible for building associations to the entity class of interest. That means that – at the time of execution – I can’t query one entity and ask it to eagerly fetch all the associated instances of the other entity since no such association exists.

In other words, the process looks something like this:

public class InitConfig {

    private final SessionFactory sessionFactory;
    private final NodeManager nodeManager;

    public void run() {

        final Configuration config = new Configuration("NewConfiguration", new HashSet<Node> ());
        for(String name : lotsOfNames) {

            //Lots of these queries run slowly
            final Node node = this.nodeManager.getNode(name);
            config.addNode(node);
        }
        this.sessionFactory.getCurrentSession().save(config);
    }
}

The related DAO (NodeManager) and slow-querying Entity (Node) look like this:

public class NodeManager {

    private final SessionFactory sessionFactory;

    public Node getNode(String name) {

        final Session db = this.sessionFactory.getCurrentSession();
        final Query query = db.createQuery("from NODE where NAME = :name");
        query.setString("name", name);
        return (Node)query.uniqueResult();
    }
}

@Entity(name="NODE")
public class Node {

    @GeneratedValue(strategy=GenerationType.TABLE)
    @Id @Column(name="ID")
    private Long id;

    private @Column(name="NAME", nullable=false, unique=true) String name;

    //Other properties and associations...
}

And finally, the entity being created by the slow-running process:

@Entity(name="CONFIGURATION")
public class Configuration {

    @Id @GeneratedValue @Column(name="ID")
    private Long id;
    private @Column(name="NAME", nullable=false, unique=true) String name;

    @ManyToMany
    @JoinTable(
        name="CONFIGURATION_NODE",
        joinColumns=@JoinColumn(name="CONFIGURATION_ID", nullable=false),
        inverseJoinColumns=@JoinColumn(name="NODE_ID", nullable=false))
    private Set<Node> nodes = new HashSet<Node> ();

    public void addNode(Node node) {

        this.nodes.add(node);
    }
}

My question is this: How do I modify the Hibernate configuration and/or code to eagerly fetch many instances of Node at a time?

Follow on questions would be:

  • Is Hibernate’s 2nd level cache appropriate for this, and if so – how do I configure it?
  • If not, is there some other Hibernate feature that can be used here?

There are roughly 100,000 Nodes at the moment, so I’m reluctant to take the brute-force approach of querying every single Node and caching it in the application somewhere, since that would not scale to higher numbers of Nodes and seems like it would duplicate data between the application and Hibernate’s internals (Session, 2nd level cache, etc…).

  • 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-12T13:59:15+00:00Added an answer on May 12, 2026 at 1:59 pm

    I’m not 100% clear on what you’re trying to do here… Does Configuration.addNode() (or some other method you’ve not shown) involve some business logic?

    If it does not, you’ll likely be much better off running several batch update queries to associate your nodes with configuration.

    If it does (meaning you have to load and inspect each node), you can:

    1. Split your entire lotsOfNames list into batches of, say, 1000 names (you can play with this number to see what gives optimal performance).
    2. Load the entire batch of nodes at once via query with name IN(?) condition.
    3. Add them to your configuration.
    4. Flush session (and, optionally, commit / start new transaction).

    Should be a lot faster as you’ll be doing 1 query instead of 1000 and batching your inserts together.

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

Sidebar

Related Questions

I'm using java persistence to save a list of entities that are associated to
Here is my persistence.xml : <?xml version=1.0 encoding=UTF-8?> <persistence xmlns=http://java.sun.com/xml/ns/persistence xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd version=1.0>
I'm just starting to learn Java database persistence using Hibernate ORM and have run
Why a Hibernate persistence object in Java is marked serializable
In the book Java Persistence with Hibernate under the section titled Many Valued Associations
I'm trying to implement a Hibernate persistence layer in a Java application and I'm
I'm currently developping an application in java using Hibernate as a persistence manager and
I have a Formula defined as: @Entity @Table(name = MyEntity) @org.hibernate.annotations.Table(appliesTo = MyEntity) public
I am new to Hibernate, reading this book Java persistence with Hibernate and I
What I am doing wrong? My persistence.xml <?xml version=1.0 encoding=UTF-8?> <persistence version=2.0 xmlns=http://java.sun.com/xml/ns/persistence xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

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.