在为使用playframework
的网络应用编写功能测试时,我创建了
@Before
public void setup() {
Fixtures.deleteDatabase();
}
@Test
public void testListTagged() {
Fixtures.loadModels("data.yml");
Response response = GET("/books/category/science");
assertNotNull(renderArgs("books"));
List<Book> books = (List<Book>)renderArgs("books");
assertEquals(3,books.size());
listTagged
方法检查Cache
是否包含属于给定类别的书籍列表的地图对象(String:List<Book>
),如果地图为空或列表为null,进行数据库查询,列表呈现为“books”。
public static void listTagged(String category) {
List<Book> books =null;
Map<String,List<Book>> tagMap = (Map<String, List<Book>>) Cache.get("tagmap");
if(tagMap!= null) {
books = tagMap.get(category);
}
if(tagMap==null || books == null) {
books= Book.findTaggedWith(category);
}
Book book = null;
if (books!=null && books.size()>0) {
book = books.get(0);
}
render(category,book, books);
}
图书课程
@Entity
public class Book extends Model implements Comparable<Book>{
@Required
@Column(unique = true)
public String isbn;
@Required
//@Field
public String name;
...
@ManyToMany(cascade=CascadeType.PERSIST)
public Set<Category> categories;
public Book(String isbn, String name, ...) {
super();
this.isbn = isbn;
this.name = name;
...
this.categories = new TreeSet<Category>();
}
...
public static List<Book> findTaggedWith(String categoryName) {
Map<String,List<Book>> tagMap = (Map<String, List<Book>>) Cache.get("tagmap");
if(tagMap==null) {
tagMap= new HashMap<String,List<Book>>();
}
List<Book> books = Book.find("select distinct book from Book book join book.categories as cat where cat.name=:name").bind("name", categoryName).fetch();
tagMap.put(categoryName, books);
Cache.add("tagmap", tagMap,"20mn");
return books;
}
单独运行上面的测试没有造成任何问题。但是当它与一些单元测试一起运行时,调用各种书籍的数据库会导致延迟初始化异常
A java.lang.RuntimeException has been caught, java.util.concurrent.ExecutionException: play.exceptions.TemplateExecutionException: failed to lazily initialize a collection of role: models.Book.categories, no session or session was closed
我该如何解决这个问题?有人可以建议吗?
答案 0 :(得分:1)
你可以使用:
@ManyToMany(cascade=CascadeType.PERSIST, fetch=FetchType.EAGER)
public Set<Category> categories;
这可能会解决问题。否则你必须小心,会话是开放的。经过测试并正在运行