Advertisement
oquidave

appengine one-to-many relationship

Nov 14th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. //
  2. The two classes represent [un-owned] one-to-many relationship between a theme and verses. Each theme can have several verses. The persistence is done using jdo appengine api which i've implemented.  like
  3.  
  4. //verse details
  5.             Verse verse = new Verse();
  6.             verse.setVerseTitle("i shall not fear");
  7.             verse.setVerseRef("psalms 23:2");
  8.             verse.setVerseBody("the lord is my light and salvation, whom shall i fear");
  9.            
  10.             //theme details
  11.             Theme theme = new Theme();
  12.             theme.setTheme("psallms");
  13.             theme.setThemeDescription("this is the theme on book of psalms");
  14.            
  15.             theme.getVerseSets().add(verse);
  16.             pm.makePersistent(theme);
  17.  
  18. My question is;
  19. How do i select only verses from a specific theme...the equivalent of "select * from verse where theme_id="theme_id" if it were the usual relational db.  
  20.  
  21. //Theme.java
  22. package com.versefeed.entities;
  23.  
  24. import java.util.List;
  25.  
  26. import javax.jdo.annotations.Element;
  27. import javax.jdo.annotations.IdGeneratorStrategy;
  28. import javax.jdo.annotations.PersistenceCapable;
  29. import javax.jdo.annotations.Persistent;
  30. import javax.jdo.annotations.PrimaryKey;
  31.  
  32. import com.google.appengine.api.datastore.Key;
  33.  
  34. @PersistenceCapable
  35. public class Theme {
  36.  
  37.    
  38.     @PrimaryKey
  39.    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  40.    private Key key;
  41.    
  42.     @Persistent
  43.     private String theme;
  44.     @Persistent
  45.     private String themeDescription;
  46.     @Persistent
  47.     private String themeArthur;
  48.    
  49.     @Persistent(mappedBy = "themes")
  50.     @Element(dependent = "true")//cascade deletes to child objects
  51.    private List<Verse> verseSets;
  52.    
  53.     public String getTheme() {
  54.         return theme;
  55.     }
  56.  
  57.     public void setTheme(String theme) {
  58.         this.theme = theme;
  59.     }
  60.  
  61.     public String getThemeDescription() {
  62.         return themeDescription;
  63.     }
  64.  
  65.     public void setThemeDescription(String themeDescription) {
  66.         this.themeDescription = themeDescription;
  67.     }
  68.  
  69.     public String getThemeArthur() {
  70.         return themeArthur;
  71.     }
  72.  
  73.     public void setThemeArthur(String themeArthur) {
  74.         this.themeArthur = themeArthur;
  75.     }
  76.  
  77.     public List<Verse> getVerseSets() {
  78.         return verseSets;
  79.     }
  80.  
  81.     public void setVerseSets(List<Verse> verseSets) {
  82.         this.verseSets = verseSets;
  83.     }
  84.  
  85.     public Key getKey() {
  86.         return key;
  87.     }
  88.  
  89.     public Theme() {
  90.         // TODO Auto-generated constructor stub
  91.     }
  92.  
  93. }
  94.  
  95. //Verse.java
  96. package com.versefeed.entities;
  97.  
  98. import javax.jdo.annotations.IdGeneratorStrategy;
  99. import javax.jdo.annotations.PersistenceCapable;
  100. import javax.jdo.annotations.Persistent;
  101. import javax.jdo.annotations.PrimaryKey;
  102.  
  103. import com.google.appengine.api.datastore.Key;
  104. import com.google.appengine.api.datastore.Text;
  105.  
  106. @PersistenceCapable
  107. public class Verse {
  108.  
  109.     @PrimaryKey
  110.    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  111.    private Key key;
  112.    
  113.     @Persistent
  114.     private boolean flag = false;
  115.     @Persistent
  116.     private Text verseBody;
  117.     @Persistent
  118.     private String verseRef;
  119.     @Persistent
  120.     private String verseTitle;
  121.    
  122.     @Persistent
  123.     private Theme themes;
  124.    
  125.    
  126.     public Verse() {
  127.         // TODO Auto-generated constructor stub
  128.     }
  129.  
  130.  
  131.     public boolean isFlag() {
  132.         return flag;
  133.     }
  134.  
  135.  
  136.     public void setFlag(boolean flag) {
  137.         this.flag = flag;
  138.     }
  139.  
  140.  
  141.     public Text getVerseBody() {
  142.         return verseBody;
  143.     }
  144.  
  145.  
  146.     public void setVerseBody(String verseBody) {
  147.        
  148.         this.verseBody = new Text(verseBody);
  149.     }
  150.  
  151.  
  152.     public String getVerseRef() {
  153.         return verseRef;
  154.     }
  155.  
  156.  
  157.     public void setVerseRef(String verseRef) {
  158.         this.verseRef = verseRef;
  159.     }
  160.  
  161.  
  162.     public String getVerseTitle() {
  163.         return verseTitle;
  164.     }
  165.  
  166.  
  167.     public void setVerseTitle(String verseTitle) {
  168.         this.verseTitle = verseTitle;
  169.     }
  170.  
  171.    
  172.    
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement