2011-11-22 62 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方法調用。