Playframework 播放框架2:保存多对多

Playframework 播放框架2:保存多对多,playframework,many-to-many,Playframework,Many To Many,我刚开始玩框架2。我有两种模式:书和作者,一本书可以有很多作者,所以我认为是多对多。以下是我的模型: @Entity public class Book extends Model { @Id public Long id; @Constraints.Required public String title; @Constraints.Required @ManyToMany(cascade=CascadeType.ALL,mappedBy="books"

我刚开始玩框架2。我有两种模式:书和作者,一本书可以有很多作者,所以我认为是多对多。以下是我的模型:

@Entity 
public class Book extends Model {

  @Id
  public Long id;

  @Constraints.Required
  public String title;

  @Constraints.Required    
  @ManyToMany(cascade=CascadeType.ALL,mappedBy="books") 
  public Set<Author> authors = new HashSet<Author>(); 
  public static Model.Finder<Long,Book> find = new Model.Finder<Long,Book>(Long.class, Book.class);

  public static Page<Book> page(int page, int pageSize, String sortBy, String order, String filter) {
      return 
        find.where()
            .ilike("title", "%" + filter + "%")                
            .orderBy(sortBy + " " + order)     
            .findPagingList(pageSize)
            .getPage(page);
  }

}

@Entity 
public class Author extends Model {

  @Id
  public Long id;

  @Constraints.Required
  public String name;

  @ManyToMany(cascade=CascadeType.ALL) 
  public Set<Book> books = new HashSet<Book>(); 

  public static Model.Finder<Long,Author> find = new Model.Finder<Long,Author>(Long.class, Author.class);

  public static Page<Author> page(int page, int pageSize, String sortBy, String order, String filter) {
      return 
        find.where()
            .ilike("name", "%" + filter + "%")
            .orderBy(sortBy + " " + order)                
            .findPagingList(pageSize)
            .getPage(page);
  }

}
@实体
公共类图书扩展模型{
@身份证
公共长id;
@约束条件。必需
公共字符串标题;
@约束条件。必需
@ManyToMany(cascade=CascadeType.ALL,mappedBy=“books”)
public Set authors=new HashSet();
publicstaticmodel.Finder=newmodel.Finder(Long.class,Book.class);
公共静态页面(整型页面、整型页面大小、字符串排序、字符串顺序、字符串筛选器){
返回
find.where()
.ilike(“标题”,“百分比”+过滤器+“%”)
.orderBy(排序方式+“”+订单)
.findPagingList(页面大小)
.getPage(第页);
}
}
@实体
公共类作者扩展模型{
@身份证
公共长id;
@约束条件。必需
公共字符串名称;
@多个(级联=级联类型.ALL)
public Set books=new HashSet();
publicstaticmodel.Finder=newmodel.Finder(Long.class,Author.class);
公共静态页面(整型页面、整型页面大小、字符串排序、字符串顺序、字符串筛选器){
返回
find.where()
.ilike(“名称”,“百分比”+过滤器+“%”)
.orderBy(排序方式+“”+订单)
.findPagingList(页面大小)
.getPage(第页);
}
}
我在输入表单中使用了这个select标记,如下所示:

<select name="authors.id" multiple="multiple">
  <option value="1">bejo</option>
  <option value="2">joko</option>
</select>

贝霍
乔科
这是我的控制器代码:

Map<String, String> newData = new HashMap<String, String>();
Map<String, String[]> urlFormEncoded = play.mvc.Controller.request().body().asFormUrlEncoded();
if (urlFormEncoded != null) {
  for (String key : urlFormEncoded.keySet()) {
    String[] value = urlFormEncoded.get(key);
    if (value.length == 1) {                    
      newData.put(key, value[0]);
    } else if (value.length > 1) {
      for (int i = 0; i < value.length; i++) {
        newData.put(key + "[" + i + "].id", value[i]);          
      }
    }
  }
}        
Form<Book> bookForm = new Form<Book>(Book.class).bind(newData);
if(bookForm.hasErrors()) {
 return badRequest(createForm.render(bookForm));
}
bookForm.get().save();
Map newData=newhashmap();
Map urlFormEncoded=play.mvc.Controller.request().body().asFormUrlEncoded();
if(urlFormEncoded!=null){
for(字符串键:urlFormEncoded.keySet()){
字符串[]值=urlFormEncoded.get(键);
如果(value.length==1){
newData.put(键,值[0]);
}否则如果(value.length>1){
for(int i=0;i
但这些代码不起作用。有人能帮我吗?
谢谢

请不要根据需要注释您的关系,而是在尝试保存项目时执行检查:

public静态结果存储簿(){
Map formUrlEncoded=request().body().asFormUrlEncoded();
Form bookForm=Form(Book.class).bindFromRequest();
Set authors=new HashSet();
//迭代键以查找值并预填充所需的集合
for(字符串键:formUrlEncoded.keySet()){
字符串[]值=formUrlEncoded.get(键);
for(字符串值:值){
if(“authors.id”.equals(key))authors.add(Author.find.ref(Long.valueOf(val));
}
}
//检查表单是否没有错误,是否包含作者
if(bookForm.hasErrors()| | authors.size()<1){
返回badRequest(createForm.render(bookForm));
}
//创建一本书,填写表单中的数据,添加关系,保存
书=新书();
book=bookForm.get();
book.authors=作者;
book.save();
flash(“generalInfo”,“图书已保存,谢谢!”);
返回重定向(routes.Application.index());
}

也许在2016年,我们有更好的方法来实现这一目标?我找不到