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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:34:56+00:00 2026-05-16T04:34:56+00:00

I have a problem when trying to update foreign keys. Using the merge function

  • 0

I have a problem when trying to update foreign keys.
Using the merge function I always get a out of memory error, or I get a timeout

Fakultaet and Standort are foreign keys in the Raum table (many to one, and lazy fetching)
Raum does eager fetching for the Fakultaet and Standort

@Stateless
@Local (IRaumHomeLocal.class)
@Remote (IRaumHomeRemote.class)
public class RaumHome implements IRaumHome {
 @PersistenceContext private EntityManager em;

 void merge(Raum r, Integer fid, Integer sid) {
  Fakultaet f = em.find(Fakultaet.class, fid);
  Standort s = em.find(Standort.class, sid);
  r.setFakultaet(f);
  r.setStandort(s);
  f.getRaums().add(r);
  s.getRaums().add(r);
  em.merge(r);
 }
 ....
}

I then tried using getReference() instead of find() since I only want to do an update,
so I have the method:

void merge(Raum r, Integer fid, Integer sid) {
 Standort s = em.getReference(Standort.class, sid);
 Fakultaet f = em.getReference(Fakultaet.class, fid);
 s.getRaums().add(r); // now it lags here
 f.getRaums().add(r);
 em.merge(r);
}

still not working
afterwards I removed the s.getRaums().add(r) and f.getRaums().add(r) lines
but this causes a LazyInitializationException in the next query I need to do.

all I want is something like this:

UPDATE Raum SET FakultaetId = ?, StandortId = ? WHERE RaumID = ?

what am I doing wrong ? is there a better method of doing this ?

  • 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-16T04:34:56+00:00Added an answer on May 16, 2026 at 4:34 am

    The problem was that I already had all the objects in a detached state and doing a find caused the entire object tree to be loaded once more.

    Using em.getReference didn’t solve the problem because accessing any of the attributes of the object caused it to be loaded, so it had the same problem as find.

    the solution is to only use the detached instances
    the new merge function looks something like this:

    void merge(Raum r, Fakultaet f, Standort s) {
     r.setFakultaet(f);
     r.setStandort(s);
     f.getRaums().add(r);
     s.getRaums().add(r);
     em.merge(r);
    }
    

    where all the parameters are detached instances.

    This still has a problem (at least for me and my hibernate version 3.2):
    after merge the detached object (raum) doesn’t have it’s version field updated, so doing a new update will cause an exception, and I had to fix this by incrementing the version field in my method

    void merge(Raum r, Fakultaet f, Standort s) {
     try {
      r.setFakultaet(f);
      r.setStandort(s);
      f.getRaums().add(r);
      s.getRaums().add(r);
      em.merge(r);
      r.setVersion(r.getVersion() + 1);
     }
     catch(RuntimeException re) {
      // logging
      throw re;
     }
    }
    

    This is more of a hack but… it works and I couldn’t find a better solution considering that this code is for a website, where working with detached instances is the best solution (creating the object tree takes a lot of time and I don’t want to recreate everything with every request)

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

Sidebar

Ask A Question

Stats

  • Questions 529k
  • Answers 529k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's a matter of efficiency... that's why your onCreate method… May 16, 2026 at 11:15 pm
  • Editorial Team
    Editorial Team added an answer If you want to ensure a particular order, sort it… May 16, 2026 at 11:15 pm
  • Editorial Team
    Editorial Team added an answer Your this inside of the success event will be the… May 16, 2026 at 11:15 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm new to Entity Framework and am trying to figure things out. I have
Gday All, I have a baffling problem whilst trying to insert some chinese characters
We are using trac. In our setup we have a problem that is solved
I have figured out a lot about nhibernate, but this seems to be my
I'm trying to reset a database in Django, using: python manage.py reset app but
I have a problem with changing my spring/hibernate application from MySql to SQL Server.
I'm trying to write something to simplify experimentation with SVG path, but i have
I'm a beginner in Java programming language, and I have a problem that I
I'm trying to sell our group on using Mercurial as a source repository rather
OK, so I have these background <div> s which pan left and right using

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.