2011-11-22 55 views
1

我有一个在JDO中通过在持久性对象中添加列表的非多对多关系设置。 为了解释我的问题,让我们用这两个实体来调用。对象没有保存为无主多对多关系

EntityA和EntityB

现在,当我有EntityB的新对象附加到EntityA的对象,我追加一条关键的EntityA对象并调用makePersistent这个就可以了,节省了对象。 我确认通过在控制台上打印它。

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

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

我是JDO和GAE的新手,自从昨天以来一直面临这个问题。 有人可以请说明这一点吗?如果需要,我也可以提供示例代码。

+0

另外我注意到的是我回到中空状态的物体。我必须给他们打电话maketransient更新他们吗? – Hrishikesh

+0

如果你不提供持久代码,你不能指望任何人明白这一点。而GAE v1并没有做适当的“无主”关系;它只是使用Key字段进行破解。 – DataNucleus

+0

好的,这是我的代码... – Hrishikesh

回答

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(键)

 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方法调用。