ur DB connection is closed by the time you try to select Address list(i am assuming writing phone is just a typo) first time. Either select/iterate the list before closing the pm in method userDao.getUser(TEST_USER_ID); or use (defaultFetchGroup = "true") for List<Address> addresses declartion like this
@Persistent (defaultFetchGroup = "true") private List<Address> addresses On Tue, Jun 15, 2010 at 7:15 PM, Tony <[email protected]> wrote: > I've followed the GAE docs on setting up one-to-many relationship but > I'm still having trouble in retrieving the collection data back. I > have no problem getting the other non-collection properties back. Here > are my classes: > > @PersistenceCapable > public class User{ > > @PrimaryKey > @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) > private Key key; > > @Persistent > private String uniqueId; > > @Persistent > private String email; > > @Persistent > private List<Address> addresses = new ArrayList<Address>() ; > ... > } > > @PersistenceCapable > public class Phone{ > > @PrimaryKey > @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) > private Key key; > > @Persistent > private String number; > ... > } > > public class UserDaoImpl implements UserDao { > > > public void insertUser(User user) { > if(user.getKey() == null) { > com.google.appengine.api.datastore.Key key = > KeyFactory.createKey(User.class.getSimpleName(), user.getEmail()); > user.setKey(key); > } > PersistenceManager pm = > PersistenceManagerWrapper.getPersistenceManager(); > notNull(user); > try { > pm.makePersistent(user); > } finally { > pm.close(); > } > } > > @SuppressWarnings("unchecked") > public User getUser(String uniqueId) { > PersistenceManager pm = > PersistenceManagerWrapper.getPersistenceManager(); > Query query = pm.newQuery(User.class); > query.setFilter("uniqueId == uniqueIdParam"); > query.declareParameters("String uniqueIdParam"); > User user = null; > try { > List<User> users = (List<User>)(query.execute(uniqueId)); > //TODO abstract this > if(users.size() > 0) > user = users.get(0); > } finally { > pm.close(); > } > return user; > } > } > > public class UserDaoImplTest { > @Test > public void getUserTest() { > User user = createTestUser(); > assertNotNull("The user object should not be null", user); > userDao.insertUser(user); > > User returnedUser = userDao.getUser(TEST_USER_ID); > assertNotNull("The returnedUser object should not be null", > returnedUser); > Assert.assertPropertyEqualsExcludeProperties("User Object", > user, returnedUser, ""); > > } > } > > When I run the test, all the properties for User is populated but the > list of Phone if I get is empty. > > -- > 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]<google-appengine-java%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/google-appengine-java?hl=en. > > -- 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.
