问题
I have 2 Models Article.java
and Tags.java
. An Article
can have many Tags
, and a Tags
can be belonged to many Article
. I am really in trouble to make this relation using JPA and Play Framework 1.2.5. Below are my codes (without setter-getter), and actually it works even throwing exception but I can not get the Tags
(getTagname()
) of an Article
Article article = Article.findById((long)id);
List<Tags> tags = article.getTags();
for (Tags tags2 : tags) {
System.out.println(tags2.getTagname());
}
Here is my models, 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:
I am doing something like your code provided above (and use Play 1.2.5), and it seems there is are problems found with the code you provided. The following are my step:
First, I create 2 models 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>();
}
and 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>();
}
Then, on the database, I manually added several record for testing purpose:
article (id;title) > 1;"java example" and 6;"article1"
tag (id;tag_name) > 4;"java" and 5;"playframework"
article_tag (articles_id;tags_id) > 6;4 and 6;5 and 1;4
So then, I testing it with the controller action :
public static void test() {
Article article = Article.findById(6L); // find "article1"
Tag tag_java = Tag.findById(4L); // find java tag
render(article, tag_java);
}
and view below:
#{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>
and lastly, the result is what to be expected :
UPDATE
This @ManyToOne
relation is bi-directional. Providing with single data article, we can have all tag on that article, and also all of these tag can have all article data corresponding to each tag. The controller codes are similar, but without passing Tag
object directly and the views look like following:
#{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}
来源:https://stackoverflow.com/questions/16286700/manytomany-relation-with-play-framework-1-2-5-jpa