Reputation: 1513
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();
}
Upvotes: 1
Views: 1007
Reputation: 1513
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.
Upvotes: 1
Reputation: 2747
Your Relationship is not done correctly.
Container.java
// ...
@Persistent(mappedBy = "employee", dependentElement = "true")
private Set<Containee> contents;
Containee.java
// ...
@Persistent
private Container container;
Upvotes: 1