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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:41:08+00:00 2026-05-11T21:41:08+00:00

(How) is it possible to persist a JPA Entity at the databases of multiple

  • 0

(How) is it possible to persist a JPA Entity at the databases of multiple servers without copying everything to DTOs?

We have a distributed system. Some applications do have DBs for caching purposes. The JPA Provider throws an Exception in which it complains that it cannot persist a detached object.

But I would like to preserve the ID of the entity with just persisting it in this additional DB.

(JPA 1.2, EJB 3.0, Glassfish v2.1, Toplink Essentials)

  • 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-11T21:41:08+00:00Added an answer on May 11, 2026 at 9:41 pm

    Don’t em.persist(obj), just em.merge(obj). Merge works with both attached and detached objects.

    If you are starting with a detached object, I would merge the object with the respective EntityManagers. If you’re trying to keep the identity key the same across the objects, I would pull the key from the first object merged, and use it in the future.

    What you probably (I don’t know) don’t want to do is try and merge an object that is managed by one EM with another EM. You can test this to see if it works, I just don’t know what will happen if you try.

    So.

    YourEntity unattachedEntity = ... // your original entity object.
    
    YourEntity managedEntity = em1.merge(unattachedEntity);
    
    // managedEntity now has the primary key assigned by the DB
    unattacheEntity.setPrimaryKey(managedEntity.getPrimaryKey());
    
    em2.merge(unattachedEntity);
    em3.merge(unattachedEntity);
    

    Something like that should work ok.

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

Sidebar

Related Questions

Is it possible to have following collection mapping in JPA / hibernate @OneToMany(cascade={CascadeType.PERSIST, CascadeType.REFRESH,
I am trying to work out if it is possible get JPA to persist
I want to persist an Image in my Article Entity. Is this possible with
I have a class mapped as an Entity to persist it in a database.
I'm working on a jpa project where I persist multiple instances of a Band
is is possible in Excel to have some sort of fixed function defined for
My current project uses JPA and HSQLDB. I would like to persist multiple related
Is it possible to persist a generic field? I have this property on an
Is it possible to badge a dock icon and have the icon persist when
I'm looking for possible ways to persist the following classes. Subsonic SimpleRepository looks like

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.