对于Unowned Many to Many关系,不保存对象

时间:2011-11-22 07:53:26

标签: google-app-engine many-to-many entity-relationship jdo

我在JDO中通过在Persistence Capable对象中添加List来建立无关的多对多关系。 为了解释我的问题,我们可以调用这两个实体。

EntityA和EntityB

现在,当我将一个新的EntityB对象附加到EntityA的Object时,我将该Key附加到EntityA对象并在其上调用makePersistent,从而保存该对象。 我通过在控制台上打印来验证它。

因为,这是一个多对多的关系,我也必须在关系的另一端做同样的事情。 因此,我获取EntityA使用的所有EntityB对象 从“+ clazz.getName()+”中选择:keys.contains(key)并将其传递给EntityA的Object中存在的Keys列表。

我遇到的问题是,返回的对象是空心,因此即使我将EntityA键附加到获取的对象,它们也不会保存到数据存储区中。

我是JDO和GAE的新手,自昨天以来一直面临着这个问题。 有人可以对此有所了解吗?如果需要,我也可以提供示例代码。

1 个答案:

答案 0 :(得分:0)

这是代码

@PersistenceCapable
public class Objective {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private boolean active;
@Persistent
private int corporate;
@Persistent
private String nameOfObjective;
@Persistent
private String shortDescription;
@Persistent
private int status;

@Persistent
private List<Key> scoreCardKeys; //List of Keys of Scorecards.


@PersistenceCapable
public class Scorecard {

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;

@Persistent
private boolean active;
@Persistent
private int corporate; // synonymous to being public 
@Persistent
private Date creationDate;
@Persistent
private String nameOfScorecard;
@Persistent
private String shortDescription;

@Persistent
private Key createdUserKey;

@Persistent
private List<Key> objectiveKeys; // List of Keys of Objectives

目标和记分卡实体处于无关的多对多关系

这是将更新记分卡的处理器方法。

public ScoreCardRepresentation updateScoreCard(ScoreCardRepresentation scoreCardRepresentation) {
    Scorecard scoreCard = scoreCardTransformer
            .transformRtoEForSave(scoreCardRepresentation);

    scoreCard.setCreationDate(new Date());

    Scorecard updatedScoreCard = scoreCardDAO.saveScoreCard(scoreCard); /*     Update the scorecard, this already has the list of Key of Objectives in it, Hence blindly save it. */

/* Update the Key of the scorecard in the Objectives too */       
updateRelatedObjectivesToScoreCard(scoreCardRepresentation,updatedScoreCard);




private void updateRelatedObjectivesToScoreCard(
        ScoreCardRepresentation scoreCardRepresentation,
        Scorecard updatedScoreCard) {
List<String> addedObjectivesIds =  scoreCardRepresentation.getAddedObjectiveKeys();
List<String> deletedObjectivesIds =  scoreCardRepresentation.getRemovedObjectiveKeys();

    // Add ScoreCard to the newly added Objectives
    if(addedObjectivesIds != null && addedObjectivesIds.size()>0){

Scorecard sc = scoreCardDAO.findScoreCardById(Scorecard.class, updatedScoreCard.getKey());
        List<Key> objKeys = sc.getObjectiveKeys();

        List<Objective> objectives = objectiveDAO.findObjectivesByKeys(Objective.class,objKeys);

//这使用查询选择“+ clazz.getName()+”其中:keys.contains(key)

        for(Objective obj : objectives){
            List<Key> scoreCardKeys = obj.getScoreCardKeys();
            if(scoreCardKeys != null){
                scoreCardKeys.add(sc.getKey());
            } else { 
                scoreCardKeys = new ArrayList<Key>();
                scoreCardKeys.add(sc.getKey());
            }
            obj.setScoreCardKeys(scoreCardKeys);
            Objective updatedObjective = objectiveDAO.saveObjective(obj);
            System.out.println(new ObjectiveProcessor().viewObjective(KeyFactory.keyToString(obj.getKey())));
        }
    }

    //Remove Scorecard entries from Objective. 
    if(deletedObjectivesIds != null && deletedObjectivesIds.size()>0){
        List<Objective> objectives = objectiveDAO.findObjectivesByIds(Objective.class,deletedObjectivesIds);
        for(Objective obj : objectives){
            List<Key> scoreCardKeys = obj.getScoreCardKeys();
            if(scoreCardKeys != null){
                scoreCardKeys.remove(updatedScoreCard.getKey());
            } 
            obj.setScoreCardKeys(scoreCardKeys);
        }
    }
}

我所能意识到的是,当我使用**findObjectivesByKeys**取回目标时我会找回空心物体,所以我必须在它们上面调用 makeTransient 来启用它们持久性,否则他们只是忽略 makePersistent 方法调用。