Q:

JDO unowned many to many relationship

I am creating a Cloud Endpoint in Google App Engine with JDO. I have two entities. The User entity contains a list of groups. The group entity contains a list of members which are user entities.
User Entity:
@PersistenceCapable(identityType = IdentityType.APPLICATION) public class USER {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        Key userid;

        @Persistent
        String name; 

        @Persistent
        private Set<GROUP> groups; //getters setters

Group Entity
@PersistenceCapable(identityType = IdentityType.APPLICATION) public class GROUP {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        Key groupid;

        @Persistent
        String name;  

        @Persistent
        private Set<USER> members; //getters setters

My API Method
@ApiMethod(name = "INSERT_CIRCLE")
    public wmcircle insertcircle(CIRCLE circle) {
        PersistenceManager mgr = getPersistenceManager();
        try {
            if (contains(circle)) {
                throw new EntityExistsException("Object already exists");
            }

            mgr.makePersistent(circle);
        } finally {
            mgr.close();
        }
        return circle;
    }

When I create a group GAE creates the user entities which the circle owns and sets the relationship but thats not the behavior I want. I want that I can set the relationship to existing user entities.

A:

The simplest way to manage an unowned many-to-many relationship is to save a collection of Keys on both sides of the relationship (see the GAE JDO Documentation). So in your case, the entities will look like this:
User
...
@Persistent
private Set<Key> groups; 

//getters setters

public void addGroup(Group group) {
    groups.add(group.getKey());
    group.getMembers().add(this.getKey());
}

public void removeGroup(Group group) {
    groups.remove(group.getKey());
    group.getMembers().remove(this.getKey());
}

Group
...
@Persistent
private Set<Key> members; 

//getters setters

public void addMember(User member) {
    members.add(member.getKey());
    member.getGroups().add(this.getKey());
}

public void removeMember(User member) {
    members.remove(member.getKey());
    member.getGroups().remove(this.getKey());
}

