【问题标题】:Object isnt saved for Unowned Many to Many relation对象未保存为 Unowned 多对多关系
【发布时间】:2012-01-03 15:31:52
【问题描述】:

通过在两个 Persistence Capable 对象中添加列表,我在 JDO 中有一个无主的多对多关系设置。 为了解释我的问题,让我们用 .

实体A和实体B

现在,当我有一个 EntityB 的新对象要附加到 EntityA 的对象时,我将该密钥附加到 EntityA 对象并在其上调用 makePersistent,这会保存该对象。 我通过在控制台上打印来验证这一点。

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

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

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

【问题讨论】:

  • 我还注意到,我得到了空心状态的对象。我必须对它们调用 maketransient 来更新它们吗?
  • 如果您不提供持久性代码,就不能指望任何人理解这一点。并且 GAE v1 没有做适当的“无主”关系;它只是使用关键字段的黑客攻击。

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


【解决方案1】:

这里是代码

@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

Objective 和 Scorecard 实体处于无主的多对多关系中

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

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);

// 这里使用查询 select from " + clazz.getName()+ " where :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** 取回目标时,我正在取回空心对象,所以我必须对它们调用 ma​​keTransient 以启用它们的持久性,否则他们只是忽略 ma​​kePersistent 方法调用。

【讨论】:

    猜你喜欢
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多