Hibernate 在forEach循环中多次调用JSF托管bean
我有一个index.xhtmlHibernate 在forEach循环中多次调用JSF托管bean,hibernate,jsf,managed-bean,Hibernate,Jsf,Managed Bean,我有一个index.xhtml <c:forEach items="${postsBean.homePagePosts}" var="category"> #{category.name} <c:forEach items="#{category.posts}" var="post"> #{post.postTitle} #{post.user.userName} #{post.rating} #{post.totalR
<c:forEach items="${postsBean.homePagePosts}" var="category">
#{category.name}
<c:forEach items="#{category.posts}" var="post">
#{post.postTitle}
#{post.user.userName}
#{post.rating}
#{post.totalReads}
</c:forEach>
</c:forEach>
#{category.name}
#{post.postTitle}
#{post.user.userName}
#{后期评级}
#{post.totalReads}
一个名为PostsBean.java的托管bean,其方法homePagePosts()使用hibernate从数据库获取记录并返回列表
public List getHomePagePosts() {
session = helper.getSessionFactory().openSession();
session.beginTransaction();
System.out.println("method called "+ i++ +" times");
List posts;
List categoryList = new ArrayList();
HashMap<String, Object> categoryName = new HashMap<String, Object>();
String HQL = "from Posts as post where post.category= :category order by post.totalReads desc";
Query query = session.createQuery(HQL);
Posts post;
query.setString("category", TextitConstants.BUSINESS);
posts = query.list();
if (!posts.isEmpty()) {
categoryName.put("name", TextitConstants.BUSINESS);
categoryName.put("posts", posts);
categoryList.add(categoryName);
categoryName = new HashMap<String, Object>();
}
query.setString("category", TextitConstants.EDUCATION);
posts = query.list();
if (!posts.isEmpty()) {
categoryName.put("name", TextitConstants.EDUCATION);
categoryName.put("posts", posts);
categoryList.add(categoryName);
categoryName = new HashMap<String, Object>();
}
query.setString("category", TextitConstants.ENTERTAINMENT);
posts = query.list();
if (!posts.isEmpty()) {
categoryName.put("name", TextitConstants.ENTERTAINMENT);
categoryName.put("posts", posts);
categoryList.add(categoryName);
categoryName = new HashMap<String, Object>();
}
query.setString("category", TextitConstants.HEALTH);
posts = query.list();
if (!posts.isEmpty()) {
categoryName.put("name", TextitConstants.HEALTH);
categoryName.put("posts", posts);
categoryList.add(categoryName);
categoryName = new HashMap<String, Object>();
}
query.setString("category", TextitConstants.POLITICS);
posts = query.list();
if (!posts.isEmpty()) {
categoryName.put("name", TextitConstants.POLITICS);
categoryName.put("posts", posts);
categoryList.add(categoryName);
categoryName = new HashMap<String, Object>();
}
query.setString("category", TextitConstants.SKILLS);
posts = query.list();
if (!posts.isEmpty()) {
categoryName.put("name", TextitConstants.SKILLS);
categoryName.put("posts", posts);
categoryList.add(categoryName);
categoryName = new HashMap<String, Object>();
}
query.setString("category", TextitConstants.TECHNOLOGY);
posts = query.list();
if (!posts.isEmpty()) {
categoryName.put("name", TextitConstants.TECHNOLOGY);
categoryName.put("posts", posts);
categoryList.add(categoryName);
categoryName = new HashMap<String, Object>();
}
query.setString("category", TextitConstants.SPORTS);
posts = query.list();
if (!posts.isEmpty()) {
categoryName.put("name", TextitConstants.SPORTS);
categoryName.put("posts", posts);
categoryList.add(categoryName);
}
return categoryList;
}
public List getHomePagePosts(){
session=helper.getSessionFactory().openSession();
session.beginTransaction();
System.out.println(“称为“+i++”times”的方法);
列出员额;
List categoryList=新建ArrayList();
HashMap categoryName=新HashMap();
String HQL=“from Posts as post where post.category=:category order by post.totalReads desc”;
Query=session.createQuery(HQL);
员额;
query.setString(“category”,TextitConstants.BUSINESS);
posts=query.list();
如果(!posts.isEmpty()){
categoryName.put(“name”,TextitConstants.BUSINESS);
categoryName.put(“posts”,posts);
categoryList.add(categoryName);
categoryName=新HashMap();
}
query.setString(“category”,TextitConstants.EDUCATION);
posts=query.list();
如果(!posts.isEmpty()){
categoryName.put(“name”,TextitConstants.EDUCATION);
categoryName.put(“posts”,posts);
categoryList.add(categoryName);
categoryName=新HashMap();
}
query.setString(“category”,TextitConstants.ENTERTAINMENT);
posts=query.list();
如果(!posts.isEmpty()){
categoryName.put(“name”,TextitConstants.ENTERTAINMENT);
categoryName.put(“posts”,posts);
categoryList.add(categoryName);
categoryName=新HashMap();
}
query.setString(“category”,TextitConstants.HEALTH);
posts=query.list();
如果(!posts.isEmpty()){
categoryName.put(“name”,TextitConstants.HEALTH);
categoryName.put(“posts”,posts);
categoryList.add(categoryName);
categoryName=新HashMap();
}
query.setString(“category”,TextitConstants.POLITICS);
posts=query.list();
如果(!posts.isEmpty()){
categoryName.put(“name”,TextitConstants.POLITICS);
categoryName.put(“posts”,posts);
categoryList.add(categoryName);
categoryName=新HashMap();
}
query.setString(“category”,TextitConstants.SKILLS);
posts=query.list();
如果(!posts.isEmpty()){
categoryName.put(“name”,TextitConstants.SKILLS);
categoryName.put(“posts”,posts);
categoryList.add(categoryName);
categoryName=新HashMap();
}
query.setString(“category”,TextitConstants.TECHNOLOGY);
posts=query.list();
如果(!posts.isEmpty()){
categoryName.put(“name”,TextitConstants.TECHNOLOGY);
categoryName.put(“posts”,posts);
categoryList.add(categoryName);
categoryName=新HashMap();
}
query.setString(“category”,TextitConstants.SPORTS);
posts=query.list();
如果(!posts.isEmpty()){
categoryName.put(“name”,TextitConstants.SPORTS);
categoryName.put(“posts”,posts);
categoryList.add(categoryName);
}
返回类别列表;
}
问题在于,在呈现index.xhtml页面之前,多次调用了postsBean.homePagePosts方法。它应该只被调用一次,并向forEach循环返回一个列表,然后forEach循环将迭代该列表并显示结果
下面是我发现的使用表中可变数据量运行网站的测试结果
- 5条记录=26次执行
- 4条记录=22次执行
- 3条记录=16次执行
- 2条记录=10次执行
- 1条记录=7次执行
- 0条记录=1次执行
需要注意的是,我没有在postsBean.homePagePosts()方法中关闭Hibernate会话,因为如果我关闭了,EL{post.user.userName}会在会话关闭时给出一个异常。(post.user是类user的对象)您正在使用的JSF的参考实现是什么?什么版本?ManagedBean
PostsBean.java
的范围是什么?可能的重复使geter简单易用。试着用某种init方法来获取数据库。