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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:16:09+00:00 2026-06-08T20:16:09+00:00

Maybe this question has been anwsered, maybe not. I want to update an object

  • 0

Maybe this question has been anwsered, maybe not.

I want to update an object with optimistic lock, using

...
EntityManager.merge(...);
EntityManager.flush(...);
...

My code works fine. What I want is update the version number in object and keep object reference.

I googling a long time, but I did not found the answer.

My last resort is make a nativeQuery and get the version number.

Here is the a test code:

    EntityManagerFactory factory = Persistence.createEntityManagerFactory(DBManager.getUnidadPersistencia());
    EntityManager emA = factory.createEntityManager();

    Grupo g = emA.find(Grupo.class, 1L);

    emA.close();

    //In some place 
    g.getSolicitanteList().get(0).setObservaciones("ZZZZ"); //Originally XXXX

    //...


    //Then I have to save the changes
    EntityManager emB = factory.createEntityManager();
    EntityTransaction tx = emB.getTransaction();

    try {
        tx.begin();
        emB.merge(g);
        emB.flush();
        tx.commit();
        //Here comes exception
        emB.refresh(g);
    } catch (Exception e) {
        if (tx.isActive()) {
            tx.rollback();
        }
        logger.log(Level.SEVERE, null, e);
    }
    emB.close();

And the stack trace:

30/07/2012 05:15:31 PM org.rp.co5parser.TestGrupo refreshTest
GRAVE: null
java.lang.IllegalArgumentException: Can not refresh not managed object: org.rp.co5parser.alojamiento.Grupo[ idGrupo=1 ].
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.refresh(EntityManagerImpl.java:943)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.refresh(EntityManagerImpl.java:849)
at org.rp.co5parser.TestGrupo.refreshTest(TestGrupo.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.apache.tools.ant.taskdefs.optional.junit.JUnit4TestMethodAdapter.run(JUnit4TestMethodAdapter.java:109)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:520)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1060)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:911)

Is that ok?

Thanks in advance….

  • 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-08T20:16:10+00:00Added an answer on June 8, 2026 at 8:16 pm

    Before googling, read the documentation:

    void refresh(java.lang.Object entity)

    Refresh the state of the instance from the database, overwriting changes made to the entity, if any.

    EDIT:

    The error you get is not an OptimisticLockException. It’s an IllegalArgumentException with a pretty clear error message: Can not refresh not managed object. You may only refresh an entity which is managed. And the object you pass is a detached object.

    You wouldn’t need to refresh the entity at all if you didn’t ignore the result of merge(). merge() takes a detached object, copies its state to the managed (attached) object with the same ID, and returns the managed object. So you should just need to do:

    try {
        tx.begin();
        g = emB.merge(g); // assign the merged, attached object, having the new version to g
        tx.commit(); // no need to flush. commit flushes automatically
    } catch (Exception e) {
        if (tx.isActive()) {
            tx.rollback();
        }
        logger.log(Level.SEVERE, null, e);
    }
    emB.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe this question has been ask already, but could not find any answer for
Maybe this question has been asked many times before, but I never found a
Sorry if this question has all ready been asked, maybe someone can point me
Maybe this has been asked before, but I couldn't find it. My question is
Maybe this question has been answered before, but the word if occurs so often
I know this sort of question has been banded about before but I've not
Maybe this question has been asked before, but I couldn't find it. I am
I know that maybe this question has been asked before, but I can't seem
Maybe this question has been asked a lot, but I still can't understand how
I'd assume this question has been asked to death, but I'm not finding anything

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.