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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:20:28+00:00 2026-06-17T09:20:28+00:00

I have the equivalent of the following code and hibernate configuration (basically, StreamRef belongs

  • 0

I have the equivalent of the following code and hibernate configuration (basically, StreamRef belongs to tape, and has to be unique on that tape):

<class name="StreamRef" table="StreamRefToTape">
 <composite-id> <key-property name="UUID"/> 
   <key-many-to-one class="Tape" name="tape">
     <column name="Tape_TapeId" not-null="true"/>
   </key-many-to-one>
 </composite-id>
 ...</class>

 <class name="Tape" table="Tape">
 <id column="TapeId" name="tapeId"/></class>

I have millions of these StreamRef’s, and I want to save them all within the same transaction, but I also want to save on RAM during this transaction.

So I attempted the following code, my assumption being that if I turn off CacheMode, then it won’t track objects internally, so it will save a lot of RAM (this seems to help, to some degree). But when testing this hypothesis, like this:

 session = sessionFactory.openSession();
 session.setCacheMode(CacheMode.IGNORE);   // disable the first level cache
 session.beginTransaction();
 Tape t = new Tape();
 StreamRef s1 = new StreamRef("same uuid");
 StreamRef s2 = new StreamRef("same uuid"); // force a primary key collision
 session.saveOrUpdate(t);
 for(StreamRef s : t.getStreams()) {
     session.save(s);
 }
 session.commit();

I would have expected this to not raise because I turned off CacheMode (but it raises a NonUniqueObjectException https://gist.github.com/4542569 ). Could somebody please confirm that 1) the hibernate internal cache is not disable-able? and 2) this exception has nothing to do with CacheMode? Is there any way to accomplish what I want here (to not use up tons of hibernate RAM within a transaction?)

somewhat related: https://stackoverflow.com/a/3543740/32453

(As a side question…does it matter the order that setCacheMode is called vin relation to beginTransaction? I assume it doesn’t?)

Many thanks.

  • 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-06-17T09:20:29+00:00Added an answer on June 17, 2026 at 9:20 am

    The exception makes sense. You’re violating the rules you told Hibernate you were going to play by. If you really want to do what you’ve coded you’ll need to use the StatelessSession API or createSQLQuery API. As it stands, Session.setCacheMode is for interacting with the second level cache, not the session cache.

    Regarding memory usage, you’ll want to incrementally flush batches of records to disk so Hibernate can purge its ActionQueue.

    Here is an example from the section on batch updates in the user’s guide:

    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    
    for ( int i=0; i<100000; i++ ) {
        Customer customer = new Customer(.....);
        session.save(customer);
        if ( i % 20 == 0 ) { //20, same as the JDBC batch size
            //flush a batch of inserts and release memory:
            session.flush();
            session.clear();
        }
    }
    
    tx.commit();
    session.close();
    

    You can also read about stateless sessions in the same chapter.

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

Sidebar

Related Questions

I have the following code that is part of my email class that I
I currently have a class where I need accomplish the equivalent of the following
I have python code that contains the following code. d = {} d[(0,0)] =
I have the following code: /* * Pointer to a function that reads a
I have the following code that produces a date string in en-us format. I
What is c# code equivalent of following XAML, where I have a RichTextBox and
I have the following code: double d1=1000000000000000000.0; //which is equivalent to 1.0e^18 double d2=3434.0;
I have failed to find a Windows Runtime equivalent to the following WPF code
I have the following code within a razor mvc3 view that is duplicated in
I'm porting some C++ code to C#. Does C# have an equivalent to std::nth_element()

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.