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

  • Home
  • SEARCH
  • 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 8926171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:54:12+00:00 2026-06-15T07:54:12+00:00

I’ve been having problems with jpa @version on appengine (testing in eclipse, sdk latest

  • 0

I’ve been having problems with jpa @version on appengine (testing in eclipse, sdk latest version, v2 of DataNucleus – AKA 3.1.1), so I’ve written a test servlet to try to reproduce the problem in a simple form. The result behaves even more freakishly that the original code, and it looks to me as if it’s a bug, but I’d like a few eyeballs on it to check out that I haven’t done something dumb.

The setup is very simple: a singleton main @Entity class which contains a OneToMany mapping of a List of sub @Entitys (although in the test described I only create one). Here:

@Entity
public class MainEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    protected static Key singletonKey =
         KeyFactory.createKey(MainEntity.class.getSimpleName(), 1);

    // Primary Key
    @Id
    protected Key id = singletonKey;

    // Use optimistic locking
    @Version
    protected long version;

    // Ref to sub entity, LAZY to be explicit
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, orphanRemoval=true)
    protected List<SubEntity> subs = new ArrayList<SubEntity>();
}


@Entity
public class SubEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    // Primary Key
    @Id
    protected Key id;

    // Use optimistic locking
    @Version
    protected long version;

    // Get variable part of key
    public String getKey() { return id.getName(); }

    // Set variable part of key
    public void setKey(String key) {
            id = KeyFactory.createKey(MainEntity.singletonKey,
                                      SubEntity.class.getSimpleName(), key);
    }

    // Pretend contents
    protected int contents;

    // modifier for contents
    public void incContent(int step) { contents += step; }
}

So, what I do is create the instance of MainEntity and an instance of SubEntity and add() the SubEntity to MainEntity.subs and commit it all. Then, each time using a fresh EntityManager, I fetch two detached instances of the MainEntity (with the contents of subs loaded), modify the contents field of both of them, then separately merge() them back.

What I expect to see is the first merge go through OK and the second to throw an exception.

What actually happens is that both merges go through OK (so the backing store has the second version of the contents field), but the version field is still 1 on the SubEntity but 3 on the MainEntity. (!) I don’t understand why the main is getting its version number updated, why it isn’t throwing an exception itself since the MainEntity is getting updated with an incorrect version number, why the sub entity isn’t causing a locking exception, and more generally what is going on.

So, have I misunderstood something or does there seem to be a bug?

Many thanks for any comments.

Jonathan.

Here is the business bit of the main code (very slightly cleaned up):

private static final EntityManagerFactory emf =
    Persistence.createEntityManagerFactory("general_use");

private void doG(PrintWriter out) {
    // First make sure db is empty
    resetDb(out);

    // Create default contents
    initDb(out, 1);

    // Get the main entity, detached
    MainEntity m1 = fetch(out);

    // Get another version
    MainEntity m2 = fetch(out);

    // Modify both versions of the sub entity in the detached copies
    m1.subs.get(0).incContent(2);
    m2.subs.get(0).incContent(3);

    // Merge them back into db
    merge(out, m1);

    // See what's in the db
    fetch(out);

    // Merging the second one should cause an OptimisticLockException
    try {
        merge(out, m2);
        out.format("We should have gotten an exception here.");
    } catch (Throwable th) {
        out.format("We got an exception here: %s, %s", th, th.getCause());
    }

    // Look at db
    fetch(out);

}


// This gets the MainEntity from the db by its key
private MainEntity fetch(PrintWriter out) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    MainEntity m = em.find(MainEntity.class, MainEntity.singletonKey);
    m.toString();    // Make sure that nothing is lazily fetched.
    // MainEntity has a toString() defined on it that uses all the fields
    // in itself and the contents of subs, so this loads everything
    em.getTransaction().commit();
    em.close();
    out.format("Fetch completed, detached state: %s", m);
    return m;
}

// This merges a detached MainEntity and its related sub entity into the db
private void merge(PrintWriter out, MainEntity mp) {
    MainEntity m;
    out.format("Merge commenced, detached state: %s", mp);
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    m = em.merge(mp);
    out.format("Merge pre commit, attached state: %s", m);
    // as above, m.toString() in the format in fact loads everything
    em.getTransaction().commit();
    em.close();
    out.format("Merge completed, detached state: %s", m);
}


// This creates the singleton MainEntity and a number of SubEntitys with keys "Init db #n" which it points to
private void initDb(PrintWriter out, int noSubCopies) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    MainEntity m = new MainEntity();
    em.persist(m);
    em.getTransaction().commit();
    em.close();

    em = emf.createEntityManager();
    em.getTransaction().begin();
    m = em.find(MainEntity.class, m.id);
    for (int i = 0; i < noSubCopies; i++) {
        SubEntity s = new SubEntity();
        s.setKey("Init db #" + i);
        em.persist(s);
        m.subs.add(s);
    }
    m.toString();
    em.getTransaction().commit();
    em.close();
    out.format("InitDb completed, detached state: %s", m);
}
  • 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-15T07:54:13+00:00Added an answer on June 15, 2026 at 7:54 am

    I’d say that’s likely a bug in the loading of the “subs” field. Check that it has the version set on the main and on the subs after you do the find of the “main” (before the commit/close). You can use JDOHelper.getVersion(obj) also since that will show the version of the object, as opposed to the field value. I don’t see any code in the GAE plugin to definitely have the version set under all circumstances.

    If the JDOHelper.getVersion(sub) or JDOHelper.getVersion(main) return null when within the txn (before detach), report it as a bug at http://code.google.com/p/datanucleus-appengine/issues/list

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have been unable to fix a problem with Java Unicode and encoding. The
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.