So I've been struggling with a very simple thing. I have a User that
contains a list of items. I want to remove an item from this list. I
do this by doing this:
1.- Get the user from the db
2.- Remove the item from the list
3.- Call an update on the user
On code:
public void removePlaylist(String userID, String playlistName) {
XYZUserContainer userCont = new XYZUserContainer();
XYZUser user = userCont.getUserByEmail(userID);
user.removePlaylist(playlistName);
userCont.updateUser(user);
}
The problem: the change is not saved on the DB. The user's list is
correctly modified but is not persisted.
"How do you update it?" you must me asking:
public XYZUser updateUser(XYZUser user) {
PersistenceManager pm = PMF.get().getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
XYZUser updatedUser = pm.makePersistent(user);
tx.commit();
return updatedUser;
} catch (RuntimeException ex) {
ex.printStackTrace();
throw ex;
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
"How do you get the user?"
public XYZUser getUserByEmail(String email) {
PersistenceManager pm = PMF.get().getPersistenceManager();
pm.setDetachAllOnCommit(true);
try {
XYZUser user = pm.getObjectById(XYZUser.class, email);
pm.close();
return user;
} catch (javax.jdo.JDOObjectNotFoundException e) {
pm.close();
return null;
}
}
The annotations:
==================================================================================
XYZUser:
public class XYZUser {
....
@Persistent(defaultFetchGroup="true")
@Expose
private List<XYZMusicList> djLists = new ArrayList<XYZMusicList>();
==================================================================================
XYZMusicList:
@PersistenceCapable(identityType = IdentityType.APPLICATION,
detachable="true")
public class XYZMusicList {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key id;
@Persistent
@Expose
private String nameOfList;
@Persistent(defaultFetchGroup="true")
@Expose
private List<Long> musicList = new ArrayList<Long>();
@Override
public boolean equals(Object obj) {
try{
XYZMusicList listaAntigua = (XYZMusicList) obj;
if(this.nameOfList.compareToIgnoreCase(listaAntigua.nameOfList)==0)
return true;
}catch(ClassCastException e){
return false;
}
return false;
}
==================================================================================
I've read the following page (http://www.datanucleus.org/products/
accessplatform_1_1/jdo/orm/one_to_many_list.html) but there are no
examples with the annotations that google app engine uses.
Any ideas on whats wrong?
--
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.