-1

我担心 GAE(Google App Engine)中的 JDO。(GWT 2.4 和 GAE 1.6.3 SDK 和 JDO 2.3)

我有一个“用户”类,它应该在列表中保存“用户”的集合,但它不起作用。

当我保存我的用户类时,它会在数据库中创建“用户”-对象,并在列表用户中创建用户对象。但是当我从数据库加载用户对象时,列表用户是空的......我必须自己加载列表吗?我想当我加载用户对象时,JDO 应该直接加载列表。

我需要你的帮助!感谢之前的!

我在抽象类 PersistentUser 和 PersistentUsers 中创建密钥会是一个问题吗?链表会是问题吗?

我的代码:

@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() {

    ...

}


}

创建用户代码: ... if(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();
        }


}
4

1 回答 1

-1

我找到了问题/解决方案

这是我愚蠢的大脑认为我可以在调试时获取它。

我的代码是正确的,但调试时信息不在对象中!

在 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();
}

}
于 2012-04-10T08:46:42.337 回答