2013-03-16 3 views
5

मुझे एहसास होना है कि: जब मैं श्रेणी को हटा देता हूं, तो इसके सभी संबंधित लेख हटा दिए जा सकते हैं और जब मैं लेख हटाता हूं, तो यह भी हो सकता है इसकी संबंधित श्रेणी में हटा दिया गया।फ्रेमवर्क 2.1 और ईबीन प्ले करें: @ मैनीटोन और @ ओनटोनी, सीआरयूडी और कैस्केड समस्याएं

मैं मानचित्रण संबंध @ManyToOne और @OneToMany(cascade = CascadeType.ALL, mappedBy = " category") बस नीचे दिए गए कोड के रूप में सेट, लेकिन जब मैं इकाई परीक्षण कर रहा था, कई समस्याएं होती है, यह काम नहीं करता। यह मेरा कोड है:

@Entity 
public class Article extends Model { 
    @Id 
    public Long id;  

    @Constraints.Required  
    public String title; 

    @Constraints.Required 
    @Lob 
    @Basic(fetch = FetchType.LAZY) 
    public String content; 

    public boolean published = false; 

    @Formats.DateTime(pattern="yyyy-MM-dd") 
    public Date publishDate; 

    @ManyToMany(cascade = CascadeType.REMOVE) 
    @Column(nullable = true) 
    public List<Tag> tags = new ArrayList<Tag>(); 

    @ManyToOne 
    @Column(nullable = true) 
    public Category category; 

    public static Finder<Long, Article> finder = new Finder<Long, Article>(Long.class, Article.class); 

    public Article(String title, String content, Tag[] tags) { 
     this.title = title; 
     this.content = content; 
     this.publishDate = new Date(); 
     this.category = null; 
     if(tags != null) 
      for(Tag t : tags) 
       this.tags.add(t); 
    } 

    public static List<Article> all() { 
     return finder.orderBy("publishDate desc").findList(); 
    } 

    public static Article create(String title, String content, Tag[] tags) { 
     Article article = new Article(title, content, tags); 
     article.save(); 
     article.saveManyToManyAssociations("tags"); 
     return article; 
    } 

    public static void delete(Long id) { 
     finder.ref(id).delete(); 
    } 

    public static void setCategory(Article article, Category c) { 
     if(article.category != null) 
      Category.removeArticle(c, article); 
     article.category = c; 
     Category.addArticle(c, article); 
     article.update(); 
    } 
} 

और इस श्रेणी वर्ग है:

@Entity 
public class Category extends Model { 
    @Id 
    public Long id; 

    @Required 
    public String name; 

    @OneToMany(mappedBy = "category", cascade = CascadeType.ALL) 
    Set<Article> articles; 

    public static Finder<Long, Category> finder = new Finder<Long, Category>(Long.class, Category.class); 

    public Category(String name) { 
     this.name = name; 
    } 

    public static List<Category> all() { 
     return finder.all(); 
    } 

    public static Category create(String name) { 
     Category category = new Category(name); 
     category.articles = new HashSet<Article>(); 
     category.save(); 
     return category; 
    } 

    public static String rename(Long id, String newName) { 
     Category category = finder.ref(id); 
     category.name = newName; 
     category.update(); 
     return newName; 
    } 

    public static void delete(Long id) { 
     Category c = finder.ref(id); 
     c.delete(); 
    } 

    public static void addArticle(Category c, Article article) { 
     if(! c.articles.contains(article)) { 
      article.category = c; 
      c.articles.add(article); 
      article.update(); 
     } 
    } 

    public static void removeArticle(Category c, Article article) { 
     if(c.articles.contains(article)) { 
      Article.delete(article.id); 
      c.articles.remove(article); 
     } 
    } 

    public static Map<String,String> options() { 
     LinkedHashMap<String,String> options = new LinkedHashMap<String,String>(); 
     for(Category c: Category.finder.orderBy("name asc").findList()) { 
      options.put(c.id.toString(), c.name); 
     } 
     return options; 
    } 
} 

यह मेरी परीक्षा है:

@Test 
public void categoryTest() { 
    Category c1 = Category.create("Play"); 
    Category c2 = Category.create("SSH"); 
    Category c3 = Category.create("Python"); 
    Category c4 = Category.create("web"); 

    String newName = Category.rename(c3.id, "Django"); 

    Category.delete(c2.id); 

    List<Category> list = Category.all(); 
    assertEquals(3, list.size()); 

    Map<String, String> map = Category.options(); 


    Article a1 = Article.create("Hello", "Hello world, My Blog!", null); 
    Article a2 = Article.create("My Blog", "It's build by play framework", null); 
    Article a3 = Article.create("Play", "Install Play", null); 

    Category.addArticle(c1, a1); 
    Category.addArticle(c1, a2); 
    Category.addArticle(c3, a3); 

    Category.delete(c3.id); 
    Category.removeArticle(c1, a1); 

    assertEquals(2, list.size()); 
    assertEquals("Play", list.get(0).name); 

    //assertEquals("Django", map.get("3")); 
    assertEquals(1, Article.all().size()); 

    assertEquals(1, c1.articles.size()); 
    assertEquals(false, c1.articles.contains(a1)); 
} 

assetion नीचे पारित नहीं कर सकते हैं।

assertEquals(2, list.size()); 

इसका मतलब है कि श्रेणी 'अब हटाया जा सकता है, लेकिन

Category.delete(c2.id); 

काम करता है!

assertEquals(1, Article.all().size()); 

उपरोक्त परिसंपत्ति पास नहीं हो सकती है। इसका मतलब है कि श्रेणी इसके संबंधित लेखों को हटा नहीं सकती है। इस समस्या को हल करने के लिए मुझे क्या करना चाहिए?

उत्तर

1

उपरोक्त कोड में केवल एक ही दावा है जो पास नहीं होता है। यह है:

assertEquals(2, list.size()); 

ऐसा क्यों हो रहा है? निम्नलिखित कोड पर

आइए नज़र:

List<Category> list = Category.all(); 
assertEquals(3, list.size()); 
Category.delete(c3.id); 
assertEquals(2, list.size()); 

पहली पंक्ति में हम सभी श्रेणियों के लेने के लिए और उनमें से तीन हैं।
तीसरी पंक्ति में हम श्रेणियों में से एक को हटा देते हैं। यह श्रेणी ठीक से हटा दी गई है लेकिन पहली पंक्ति में गणना की गई श्रेणियों की सूची में अभी भी तीन तत्व हैं। ऐसा इसलिए है क्योंकि यह सूची डेटाबेस की वर्तमान स्थिति का प्रतिनिधित्व नहीं करती है। यह अभी भी उपरोक्त कोड की पहली पंक्ति में गणना परिणाम आयोजित करता है।
बनाने के दूसरे दावे काम ठीक से हम लाइन 3 और 4 के बीच निम्न पंक्ति जोड़ने के लिए है:

कोड से ऊपर श्रेणियों की वर्तमान सूची की गणना और चर `सूची में आवंटित करेगा '।

संबंधित मुद्दे