2012-04-09 72 views
0

我在GAE(Google App Engine)中擔心JDO。 (GWT 2.4和GAE 1.6.3 SDK和JDO 2.3)GAE GWT JDO持久性列表<Element>無法正確保存/加載

我有一個類「用戶」,它應該將一個「用戶」集合保存在列表中,但它不起作用。

當我保存我的Users-Class時,它會在數據庫中創建「用戶」 - 對象,並且它還會在列表用戶中創建用戶對象。但是當我從數據庫加載用戶對象時,列表用戶是空的... 我是否必須自己加載列表?我猜想,當我加載用戶對象時,JDO schould直接加載列表。

我需要你的幫助!以前感謝!

難道這是一個問題,我在抽象類PersistentUser和PersistentUsers中創建密鑰? LinkedList可以成爲問題嗎?

我的代碼:

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true") 
@Version(strategy=VersionStrategy.VERSION_NUMBER) 
public class Users extends PersistentUsers implements Serializable{ 

/** 
* 
*/ 
private static final long serialVersionUID = -21666269538993247L; 

/** 
* Mapped from Operator X 
*/ 
@Persistent 
private String operatorId; 

@Persistent(mappedBy="userlist") 
@Element(dependent = "true") 
private List<User> users; 

/** 
* 
* List of Ids of Users 
* 
*/ 
@Persistent(serialized = "true") 
@Element(dependent = "true") 
private List<String> userIds; 


/** 
* @return the users 
*/ 
public List<User> getUsers() { 
    return users; 
} 
/** 
* @param users the users to set 
*/ 
public void setUsers(List<User> users) { 
    this.users = users; 
} 
... 
} 

用戶類:

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true") 
@Version(strategy=VersionStrategy.VERSION_NUMBER) 
public class User extends PersistentUser implements Serializable{ 

/** 
* 
*/ 
private static final long serialVersionUID = 6899284258473985914L; 

@Persistent 
private String emailAddress; 

@Persistent 
private UserRole role; 

/** 
* 
* Mapped from Userlist X from Operator Y 
*/ 
@Persistent 
private Users userlist; 

public User(String email, UserRole role){ 
    this.emailAddress = email; 
    this.role = role; 
} 

public String getEmailAddress() { 
    return emailAddress; 
} 
public void setEmailAddress(String emailAddress) { 
    this.emailAddress = emailAddress; 
} 
public UserRole getRole() { 
    return role; 
} 
public void setRole(UserRole role) { 
    this.role = role; 
} 

/** 
* @return the userlist 
*/ 
public Users getUserlist() { 
    return userlist; 
} 

/** 
* @param userlist the userlist to set 
*/ 
public void setUserlist(Users userlist) { 
    this.userlist = userlist; 
} 


} 

PersistentUser和PersistentUsers類是相同的內容(但由於JDO-AppEngine上繼承問題兩個不同的類別:

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true") 
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE) 
@Version(strategy=VersionStrategy.VERSION_NUMBER) 
public abstract class PersistentUsers implements IPersitentObject { 

/** 
* Id 
* 
* Autogenerated String id of the Database 
* 
*/ 
@PrimaryKey 
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
protected Key encodedKey; 


@Persistent 
protected String username; 

@Override 
public String getId() { 
    if(encodedKey == null) return null; 
    return KeyFactory.keyToString(encodedKey); 
} 
/*public void setId(String id) { 

    this.encodedKey = id; 
}*/ 

/** 
* Helper function - get Version from DB 
*/ 
@Override 
public Long getVersion(){ 
    ... 
} 

/** 
* Helper function - will save this instance in DB 
*/ 
public void persist(){ 

    ... 
} 

/** 
* Helper function - will remove this instance from DB 
*/ 
public void delete(){ 
    ... 
} 

@Override 
public final boolean checkUsername() { 

    ... 

} 


} 

創建用戶代碼: ... 如果(RequestFactoryServlet.getThreadLocalRequest()getUserPrincipal()= NULL!){

 //Create New User 
     String email =  RequestFactoryServlet.getThreadLocalRequest().getUserPrincipal().getName(); 
     User u = UserFactory.getUser(email, UserRole.ADMINISTRATOR); 
     //u.persist(); 

     //Create New Userlist 
     Users users = UserFactory.getUsers(); 

     //Get Uids (normally empty) 
     LinkedList<String> uids = (LinkedList<String>) users.getUserIds(); 
     if(uids==null){ 
      uids = new LinkedList<String>(); 
     } 
     uids.add(u.getId()); 

     //Get DB-Userlist of current User-List 
     LinkedList<User> userlist = (LinkedList<User>) users.getUsers(); 
     if(userlist==null){ 
      userlist = new LinkedList<User>(); 
     } 
     userlist.add(u); 
     users.setUserIds(uids); 
     users.setUsers(userlist); 

     u.setUserlist(users); 
     //Persit Userlist and Persist User 
     users.persist(); 

     this.userlistId = users.getId(); 

} 
... 

持久性代碼:

public static void persist(IPersitentObject o){ 

     PersistenceManager pm = Pmf.get().getPersistenceManager(); 

     try{ 
      pm.makePersistent(o); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     }finally { 

      pm.close(); 
     } 


} 
+1

所以你說你檢索它,但不要打擾說什麼,事務邊界,插件的版本,或任何對日誌的引用。我會從那些開始...... – DataNucleus 2012-04-09 18:12:16

+0

GWT 2.4和GAE 1.6.3 SDK和JDO 2.3。 我做了一個事務,當我保存它時,我在第二階段用一個新的查詢從數據庫加載它。 – Sam 2012-04-10 06:31:29

+0

因此,將持久代碼添加到您的帖子中以便人們可以看到,這樣他們可以看到您正在檢查字段內容的位置 – DataNucleus 2012-04-10 06:41:15

回答

0

我發現這個問題/解決方案

這是我愚蠢的大腦思維我可以在調試時取出它。

我的代碼是正確的,但調試時信息不在對象中!

在一個TestCase中測試它顯示,它工作。

public class UsersTest { 

private PersistenceManager pm; 
private final LocalServiceTestHelper helper = 
      new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()); 
private String userlistId; 
private String userId; 

@Before 
public void setUp() throws Exception { 
    helper.setUp(); 

    pm = ch.zhaw.ams.server.core.persistance.Pmf.get().getPersistenceManager(); 


} 


@After 
public void tearDown() throws Exception { 
} 

@Test 
public void testNewUsers() { 


    //New UserList 



     //Create New Userlist 
     Users users = UserFactory.getUsers(); 

     //Create New User 
     String email = "ss"; 
     User u = UserFactory.getUser(email, UserRole.ADMINISTRATOR); 

     users.getUsers().add(u); 

     users.persist(); 
     this.userlistId = users.getId(); 
     this.userId = users.getUsers().get(0).getId(); 



    //Test Users 
    pm = ch.zhaw.ams.server.core.persistance.Pmf.get().getPersistenceManager(); 
    Users ul= pm.getObjectById(Users.class, this.userlistId); 
    assertNotNull(ul); 
    assertNotNull(ul.getUsers().get(0)); 
    assertTrue(ul.getUsers().get(0).getId().equals(this.userId)); 
    pm.close(); 
} 

}