播放框架2:节省很多 [英] play framework 2: saving many to many

查看:76
本文介绍了播放框架2:节省很多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Play Framework 2的新手.我有2种模型:书籍"和作者",一本书可以有很多作者,所以我认为这本书很多.这是我的模特:

I'm new to Play Framework 2. I have 2 models: Book and Author, one book can have many authors, so I think its many to many. Here are my models:

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

}

并且我在输入表单中使用了这个选择标签,如下所示:

and I'm using this select tag in the entry form, like this:

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

,但是这些代码不起作用.身体可以帮助我吗? 谢谢

but these codes does not work. can some body help me? Thanks

推荐答案

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

Don't annotate your relations as required instead perform a check while item save attempt:

public static Result saveBook() {
    Map<String, String[]> formUrlEncoded = request().body().asFormUrlEncoded();
    Form<Book> bookForm = form(Book.class).bindFromRequest();
    Set<Author> authors = new HashSet<Author>();

    // iterate through the keys to find values and pre-fill required Set(s)
    for (String key : formUrlEncoded.keySet()) {
        String[] values = formUrlEncoded.get(key);
        for (String val : values) {
            if ("authors.id".equals(key)) authors.add(Author.find.ref(Long.valueOf(val)));
        }
    }

    // Check if form hasn't errors and if it contains authors
    if (bookForm.hasErrors() || authors.size() < 1) {
        return badRequest(createForm.render(bookForm));
    }

    // Create a Book, fill with data from form, add relations, save
    Book book = new Book();
    book = bookForm.get();
    book.authors = authors;
    book.save();

    flash("generalInfo", "Book saved, thank you!");
    return redirect(routes.Application.index());
}

这篇关于播放框架2:节省很多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆