I have a one-to-many unowned relationship (Code shown at the end).  When I
try to make the association, it fails to actually store the relationship.  I
thought I found a workaround, which is commented out in the code below.  How
do I make this unowned relationship work?

This might be related to this thread:
http://groups.google.com/group/google-appengine-java/browse_thread/thread/a44cb049f40d6bab/f9047e0a25e3453f?lnk=gst&q=JDO+collection+null#f9047e0a25e3453f

If it is, this should be a very high priority bug, because this seems like
required functionality.

Thanks,
Jeffrey

    public void test() {
        PersistenceManagerFactory pmfInstance =
JDOHelper.getPersistenceManagerFactory("transactions-optional");
        PersistenceManager pm = pmfInstance.getPersistenceManager();

        Employee employee = new Employee();
        pm.makePersistent(employee);

        Computer computer = new Computer();
        computer.setOwner(employee);
        pm.makePersistent(computer);

        // Thought this was a work around, but it doesn't work either
        //pm.close();
        //pm = pmfInstance.getPersistenceManager();
        //employee = pm.getObjectById(Employee.class, employee.key);
        //computer = pm.getObjectById(Computer.class, computer.key);

        employee.addComputer(computer);
        pm.close();

        pm = pmfInstance.getPersistenceManager();

        Employee employeeCheck = pm.getObjectById(Employee.class,
employee.key);
        if (employeeCheck == null || employeeCheck.computerKeys == null ||
employeeCheck.computerKeys.isEmpty()) {
            System.out.println("Error: employee didn't save computer
relationship");
        } else {
            System.out.println("Success: employee saved computer
relationship");
        }
        pm.close();
    }

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Employee {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    public Long key; //public to simplify example

    @Persistent
    private String name;

    @Persistent
    public Set<Long> computerKeys = new HashSet<Long>();

    public void addComputer(Computer computer) {
        if (computerKeys == null) {
            computerKeys = new HashSet<Long>();
        }
        computerKeys.add(computer.key);
    }
}

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Computer {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    public Long key; //public to simplify example

    @Persistent
    private String name;
    @Persistent
    private Long ownerEmployeeKey;

    public void setOwner(Employee owner) {
        this.ownerEmployeeKey = owner.key;
    }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to