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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:18:32+00:00 2026-06-04T00:18:32+00:00

I have a container object that contains of set of objects that is persisted

  • 0

I have a container object that contains of set of objects that is persisted in Google App Engine using JDO 2.3. I want to remove an object from the set contents. With the following test code, the remove() method returns false, but the change is not persisted, as the following code demonstrates. However, the set cardinality is reduced (this behavior astonishes me). How can I correct this sample to remove the specified object from the set (in this case, object “one”)?

I haven’t been able to find anything relevant in the JDO documentation. Equality checks and hashing are based on this article.

A dump of the console log with the log level turned up is here (update: this is transactionless version).

A dump of the console log with transactions is here.

Container.java

import java.util.HashSet;
import java.util.Set;

import javax.jdo.annotations.FetchGroup;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(detachable = "true")
@FetchGroup(name = "withContents", members = { @Persistent(name = "contents") })
public class Container
{
    @PrimaryKey
    private String id;

    @Persistent(dependentElement = "true")
    private Set<Containee> contents;

    public Set<Containee> getContents()
    {
        return contents;
    }

    public Container(String id)
    {
        super();
        this.id = id;
        contents = new HashSet<Containee>();
    }
}

Containee.java

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(detachable = "true")
public class Containee
{
    @SuppressWarnings("unused")
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName = "datanucleus", 
       key = "gae.encoded-pk", value = "true")
    private String id;

    @Persistent
    private String friendlyName;

    public String getFriendlyName()
    {
        return friendlyName;
    }

    public Containee(String friendlyName)
    {
        this.friendlyName = friendlyName;
    }

    @Override
    public boolean equals(Object other)
    {
        if (other instanceof Containee)
        {
            Containee that = (Containee) other;
            return this.getFriendlyName().equals(that.getFriendlyName());
        }
        return false;
    }

    @Override
    public int hashCode()
    {
        return friendlyName.hashCode();
    }
}

Test snippet (run server-side as part of a RemoteService)

...

        System.out.println("Fetching...");
        Container after = pm.getObjectById(Container.class, "test");

        // prints 2
        System.out.println("Pre-remove set cardinality "
                + after.getContents().size());

        // prints "true"
        System.out.println("Post-store containment: "
                + after.getContents().contains(one));

        for (Containee e : after.getContents())
        {
            System.out.println(e.getFriendlyName());
        }

        System.out.println("Mark");
        boolean result = after.getContents().remove(one);
        System.out.println("End Mark");

        System.out
                .println("'after' object class: " + after.getContents().getClass());

        // prints "false" (!?!?)
        System.out.println("Post-store removal: " + result);

        // prints 1 (...?)
        System.out.println("Post-remove set cardinality: "
                + after.getContents().size());

...

Edit:

Test snippet with transactions

    Container before = new Container("test");

    Containee one = new Containee("one");
    Containee two = new Containee("two");
    Containee three = new Containee("three");

    before.getContents().add(one);
    before.getContents().add(two);
    before.getContents().add(three);

    // prints "true"
    System.out.println("Pre-store containment: "
            + before.getContents().contains(two));

    // prints "true"
    System.out.println("Pre-store removal: "
            + before.getContents().remove(two));

    PersistenceManager pm = pmf.getPersistenceManager();

    try
    {
        pm.makePersistent(before);
    }
    finally
    {
        pm.close();
    }

    pm = pmf.getPersistenceManager();
    pm.getFetchPlan().addGroup("withContents");

    Transaction tx = pm.currentTransaction();

    try
    {
        System.out.println("Fetching...");
        Container after = pm.getObjectById(Container.class, "test");

        // prints 2
        System.out.println("Pre-remove set cardinality "
                + after.getContents().size());

        // prints "true"
        System.out.println("Post-store containment: "
                + after.getContents().contains(one));

        for (Containee e : after.getContents())
        {
            System.out.println(e.getFriendlyName());
        }

        tx.begin();

        System.out.println("Mark");
        boolean hrm = after.getContents().remove(one);
        System.out.println("End Mark");

        tx.commit();

        System.out
                .println("'after' object class: " + after.getContents().getClass());

        // prints "false" (!?!?)
        System.out.println("Post-store removal: " + hrm);

        // prints 1 (...?)
        System.out.println("Post-remove set cardinality: "
                + after.getContents().size());

    }
    finally
    {
        System.out.println("Finalizing transaction...");
        if (tx.isActive())
        {
            System.out.println("Rolling back...");
            tx.rollback();
        }
    }

    pm.close();

    pm = pmf.getPersistenceManager();
    pm.getFetchPlan().addGroup("withContents");

    try
    {
        System.out.println("Fetching again...");
        Container after = pm.getObjectById(Container.class, "test");

        // prints 2
        System.out.println("Final set cardinality "
                + after.getContents().size());
    }
    finally
    {
        pm.close();
    }
  • 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-04T00:18:33+00:00Added an answer on June 4, 2026 at 12:18 am

    After a weekend of head scratching and frustration, I’ve found a work-around: leverage reference equality instead of value equality when calling Set.remove(). Here’s the code (interesting bit starts at the comment “get a reference to the object in the persisted Set”):

        Container before = new Container("test");
    
        Containee one = new Containee("one");
        Containee two = new Containee("two");
        Containee three = new Containee("three");
    
        before.getContents().add(one);
        before.getContents().add(two);
        before.getContents().add(three);
    
        pm = pmf.getPersistenceManager();
    
        try
        {
            pm.makePersistent(before);
        }
        finally
        {
            pm.close();
        }
    
        pm = pmf.getPersistenceManager();
    
        try
        {
            Container after = pm.getObjectById(Container.class, "test");
    
            // prints 3
            System.out.println("Pre-remove set cardinality "
                    + after.getContents().size());
    
            // prints "true"
            System.out.println("Post-store containment: "
                    + after.getContents().contains(one));
    
            //get a reference to the object in the persisted Set
            //that is value-equivalent to Containee #1
            Containee ref = null;
            for (Containee c : after.getContents())
            {
                if (c.equals(one)) ref = c;
            }
    
            if (ref != null)
            {
                after.getContents().remove(ref);
            }
    
            // prints 2
            System.out.println("Post-remove set cardinality: "
                    + after.getContents().size());
    
        }
        finally
        {
            pm.close();
        }
    
        pm = pmf.getPersistenceManager();
    
        try
        {
            Container after = pm.getObjectById(Container.class, "test");
    
            // prints 2 (as expected)
            System.out.println("Final set cardinality "
                    + after.getContents().size());
        }
        finally
        {
            pm.close();
        }
    

    This code doesn’t show it, but wrapping the operation with a pessimistic transaction is probably a good plan, to avoid concurrency issues.

    The success of this technique leads me to suspect that the DataNucleus framework uses object references instead of equality checks to handle deletions, but I haven’t found anything in the documentation that confirms or disproves that hypothesis.

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

Sidebar

Related Questions

I have a class that contains objects of another class: public class Container {
I have a Python set that contains objects with __hash__ and __eq__ methods in
I want to store an object that contains a List of primitives using EF.
I have a JPA-persisted object model that contains a many-to-one relationship: an Account has
I have a object that contains data from a DB. The object has a
I have an object that contains about half a dozen properties. I expect to
I have an object that contains a array. On initialization of this object, the
I have a Grails command object that contains an emailAddresses field, e.g. public class
I have a C# string object that contains the code of a generic method,
For example lets say I have a JSON object that contains states and cities.

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.