2

我正在将 gwt 与 jdo datanucleus 一起使用。我有要求让孩子和父母在一起。但是当访问父母时我没有得到孩子。我的代码如下

我的家长班是

@PersistenceCapable(identityType = IdentityType.APPLICATION, table = "user")
public class User implements Serializable {

    private static final long serialVersionUID = 2660867968471555842L;

    @PrimaryKey
    @Persistent
    private String email;

    @Persistent(defaultFetchGroup = "true",mappedBy="user")
    private UserProfile profile;

    public User() {}

    public User(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public UserProfile getProfile() {
        return profile;
    }

    public void setProfile(UserProfile profile) {
        this.profile = profile;
    }
}

我的孩子班是

@PersistenceCapable(identityType = IdentityType.APPLICATION,table = "user_profile")
public class UserProfile implements Serializable {

    private static final long serialVersionUID = -6818036410894395030L;

    @PrimaryKey
    @Persistent(defaultFetchGroup="true")
    private User user;

    @Persistent
    private String name;

    public UserProfile() {}

    public UserProfile(User user) {
        this.user = user;
        user.setProfile(this);
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

我正在通过以下查询获取数据

PersistenceManager pm = PMF.get().getPersistenceManager();
                 User user=null;

                try{
                    String userId ="abc@abc.com";
                    Query userQuery = pm.newQuery(User.class);
                    userQuery.setFilter("email == '" + userId + "'");
                    userQuery.setUnique(true);
                    user = (User) userQuery.execute();
                } catch (Exception e) {
                    throw new IllegalAccessError("Failed to get the User..");
                }finally{
                    pm.close();
                }

但我在对象用户中得到 userprofile null。哪里有问题 ?如何将孩子与父母一起加载?

4

1 回答 1

2

我不确定你是否找到了答案,但对于那些偶然发现这个问题的人,我只是想分享一下我是如何让它工作的。

@PersistenceCapable(detachable = "true") 
@FetchGroup(name = "fooGroup", members = { @Persistent(name = "list") }) 
public class ParentClass {

    @Persistent(mappedBy = "parent")
    @Element(dependent = "true") //can not exist without parent
    private List<ChildClass> list;

}

@PersistenceCapable(detachable = "true") 
public class ChildClass {
    @Persistent
    private ParentClass parent;
}

然后进行提取:

PersistenceManager pm = PMF.get("eventual-reads-shortdeadlines").getPersistenceManager();
pm.setDetachAllOnCommit(true); 
pm.getFetchPlan().addGroup("fooGroup");
Transaction tx = pm.currentTransaction();

    try {
        tx.begin();

        Query query = pm.newQuery(ParentClass.class);
        list = (List<ParentClass>) query.execute();

        tx.commit();
    } catch (Exception ex) {
        ...
    } finally {
        if (pm != null) {
            if(pm.currentTransaction().isActive()){
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    }

您的 ParentClass 现在应该拥有每个 ChildClass 的所有内容。希望有帮助!

于 2012-06-21T18:11:01.337 回答