ManyToMany与Play Framework 1.2.5 JPA的关系

时间:2013-04-29 19:29:56

标签: jpa orm playframework many-to-many playframework-1.x

我有2个模型Article.javaTags.javaArticle可以有多个Tags,而Tags可以属于许多Article。使用JPA和Play Framework 1.2.5来建立这种关系真的很麻烦。下面是我的代码(没有setter-getter),实际上它甚至可以抛出异常,但我无法得到Tags

getTagname()Article
Article article = Article.findById((long)id);
List<Tags> tags = article.getTags();

for (Tags tags2 : tags) {
    System.out.println(tags2.getTagname());
}

这是我的模型, Article.java

@Entity
public class Article extends Model{

    @Required
    public String title;

    @Required
    public String link;

    @Required
    @Lob
    public String description;

    public Date date;

    @ManyToMany(cascade=CascadeType.ALL)
    public List<Tags> tags = new ArrayList<Tags>();
}

Tags.java

@Entity
public class Tags extends Model {

    @Required
    public String tagname;

    @ManyToMany(mappedBy="tags") 
    public List<Article> tagsInArticle = new ArrayList<Article>(); 
}

1 个答案:

答案 0 :(得分:2)

我正在执行上面提供的代码(并使用Play 1.2.5),似乎您提供的代码中存在问题。以下是我的步骤:

首先,我创建了2个模型 Article.java

package models;

import play.data.validation.Required;
import play.db.jpa.Model;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "article")
public class Article extends Model {
    @Required
    public String title;

    @ManyToMany(cascade = CascadeType.ALL)
    public List<Tag> tags = new ArrayList<Tag>();
}

Tag.java

package models;

import play.data.validation.Required;
import play.db.jpa.Model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "tag")
public class Tag extends Model {
    @Required
    @Column(name = "tag_name")
    public String tagName;

    @ManyToMany(mappedBy = "tags")
    public List<Article> articles = new ArrayList<Article>();
}

然后,在数据库上,我手动添加了几条记录用于测试目的:

  

文章(id; title)&gt; 1;“java example”和6;“article1”

     

标记(id; tag_name)&gt; 4;“java”和5;“playframework”

     

article_tag (articles_id; tags_id)&gt; 6; 4和6; 5和1; 4

然后,我使用控制器操作进行测试:

public static void test() {
    Article article = Article.findById(6L); // find "article1"
    Tag tag_java = Tag.findById(4L); // find java tag
    render(article, tag_java);
}
下面的

查看

#{extends 'main.html' /}

<h3>Article Title : ${article?.title}</h3>

Tags:<br>
<ol>
#{list article?.tags, as:'tag'}
    <li>${tag.tagName}</li>
#{/list}
</ol>

All article tagged <b>java</b> :
<ul>
#{list tag_java?.articles, as:'java_article'}
    <li>${java_article.title}</li>
#{/list}
</ul>

最后,结果是预期的结果:

result


<强>更新

@ManyToOne关系是双向的。提供单一数据文章,我们可以在该文章上包含所有标记,并且所有这些标记都可以包含与每个标记相对应的所有文章数据。控制器代码类似,但没有直接传递Tag对象,视图如下所示:

#{extends 'main.html' /}

<h3>Article Title : ${article?.title}</h3>

Tags:<br>
<ol>
#{list article?.tags, as:'tag'}
    <li>${tag.tagName}</li>
#{/list}
</ol>

#{list article?.tags, as:'tag'}
Related article [tagged with ${tag.tagName}]:<br>
<ol>
    #{list tag?.articles, as:'article'}
        <li>${article.title}</li>
    #{/list}
</ol>
#{/list}

bi-directional rel