本文介绍了与Play Framework 1.2.5 JPA的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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());
}

这是我的模型, 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>();
}

推荐答案

我正在做类似您上面提供的代码(并使用Play 1.2.5)的代码,看来您提供的代码存在问题.这是我的步骤:

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:

首先,我创建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>();
}

然后,在数据库上,我手动添加了几条记录以进行测试:

Then, on the database, I manually added several record for testing purpose:

标签(id; tag_name)> 4;"java"和5;"playframework"

tag (id;tag_name) > 4;"java" and 5;"playframework"

article_tag (articles_id; tags_id)> 6; 4和6; 5和1; 4

article_tag (articles_id;tags_id) > 6;4 and 6;5 and 1;4

然后,我用 controller 动作对其进行测试:

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);
}

视图如下:

#{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 :

更新

@ManyToOne关系是双向的.通过提供单个数据商品,我们可以在该商品上拥有所有标签,并且所有这些标签都可以具有与每个标签相对应的所有商品数据.控制器代码相似,但是没有直接传递Tag对象,并且视图如下所示:

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}

这篇关于与Play Framework 1.2.5 JPA的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:57