对数据库bu Hibernate3的请求相乘 [英] Multiplying requests to database bu Hibernate3

查看:58
本文介绍了对数据库bu Hibernate3的请求相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是检查了所有帖子和主题,却找不到真正的解决方案,无论如何,这就是问题所在-一个名为TestResult.java的主要类有两个集合List和List,两个集合都设置为Lazy:

@ViewScoped
@Entity
@NamedQueries({
@NamedQuery(name="TestResult.getTestResults",query="FROM TestResult tr order by DateTaken desc"),
@NamedQuery(name="TestResult.getTestStatistic",query="Select testStatistic FROM TestResult tr where tr.recId = :recId"),
@NamedQuery(name="TestResult.getQuestions",query="Select questionsList FROM TestResult tr where tr.recId = :recId ")

})
@Table(name="TestResult")
public class TestResult implements Serializable {

private transient static final long serialVersionUID = 1L;

transient static final private String pass = "PASSED";
transient static final private String fail = "FAILED";

@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int recId;
private String Username;
private String SavedTestName;
private Date DateTaken;
private int TestDuration;
private int TotalAsnwers;
private int MissedAsnwers;
private int CorrectAsnwers;
private int IncorrectAsnwers;
private int Score;
private String Status;
@OneToMany(fetch=FetchType.LAZY)
private List<UserQuestion> questionsList = new ArrayList<UserQuestion>();
@OneToMany(fetch=FetchType.LAZY)
@Cascade(CascadeType.ALL)
@Fetch(FetchMode.SELECT)
private List<TestStatistic> testStatistic = new ArrayList<TestStatistic>();

getters and setters are here ... 

数据按如下方式加载到TableView.java类中:

@ManagedBean
@ViewScoped
public class TableView {

private boolean showChart = false;
private boolean _isFiltered;
private List<TestResult> listResults = new ArrayList<TestResult>();

采用这种方法

    @PostConstruct
public void loadListResults() {

    sessionFactory = HibernateUtil.getSessionFactory();
    session = sessionFactory.openSession();
    session.beginTransaction();

    Query query = session.getNamedQuery("TestResult.getTestResults");
    listResults = query.list();

    session.getTransaction().commit();
    session.close();

}

xhtml文件显示在这里:

    <p:layoutUnit position="center">  
    <h:form>  
        <p:dataTable id="datatable" value="#{tableView.listResults}" var="results" paginator="true" rows="20" 
            paginatorTemplate=
                "{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
            rowStyleClass="#{results.status == 'FAILED' ? 'failed-test' : 'passed-test'}" rowsPerPageTemplate="10,5">
        <p:column>
            <f:facet name="header">Action</f:facet>
            ajax="false" update="panel,display"/>
        <p:commandButton value="Review test" ajax="false" immediate="true"/>
        </p:column>
        <p:column>
            <f:facet name="header">Username</f:facet>
            <p:outputLabel value="#{results.username}"/>
        </p:column>         
        <p:column>
            <f:facet name="header">Test name</f:facet>
            <p:outputLabel value="#{results.savedTestName}"/>
        </p:column>
        <p:column>
            <f:facet name="header">Date taken</f:facet>
            <p:outputLabel value="#{results.dateTaken}"/>
        </p:column>
        <p:column>
            <f:facet name="header">Test name</f:facet>
            <p:outputLabel value="#{results.testName}"/>
        </p:column>         
        <p:column>
            <f:facet name="header">Test duration</f:facet>
            <p:outputLabel value="#{results.testDuration} min"/>
        </p:column>         
        <p:column>
            <f:facet name="header">Total / Correct / Incorrect / Missed</f:facet>
            <p:outputLabel value="#{results.totalAsnwers} / #{results.correctAsnwers} / 
                #{results.incorrectAsnwers} / #{results.missedAsnwers}"/>
        </p:column> 
        <p:column>
            <f:facet name="header">Score</f:facet>
            <p:outputLabel value="#{results.score} %"/>
        </p:column> 
        <p:column>
            <f:facet name="header">Status</f:facet>
            <p:outputLabel value="#{results.status}"/>
        </p:column> 
        </p:dataTable>

在休眠初始化所有我在控制台中看到的东西之后: 即它执行两次查询, 但是,如果我单击p:dataTable的分页控件只是将其从5更改为10,例如,我在控制台中看到以下内容:

我已经检查了所有可能出现问题的来源,但仍然找不到解决方案.那不应该是与控制有关的问题吗?

这是完整的查询输出,同一查询运行18次!

休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_.MissedAsds6res.状态为Status6_,testresult0_.TestDuration为TestDura9_6_,testresult0_.TestName为TestName6_,testresult0_.TopicName为TopicName6_,testresult0_.TotalAsnwers作为TotalAs12_6_,testresult0_. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_Test_resultD_0从用户名作为结果. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_test_res_Dres作为从用户名到用户的结果. 休眠状态:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_Test_resultD_0从用户名作为结果. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_test_res_Dres作为从用户名到用户的结果. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_Test_resultD_0从用户名作为结果. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_test_res_Dres作为从用户名到用户的结果. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_test_res_Dres作为从用户名到用户的结果. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_test_res_Dres作为从用户名到用户的结果. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_Test_resultD_0从用户名作为结果. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_test_res_Dres作为从用户名到用户的结果. 休眠状态:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_test_res_Dres作为从用户名到用户的结果. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_Test_resultD_0从用户名作为结果. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_test_res_Dres作为从用户名到用户的结果. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_Test_resultD_0从用户名作为结果. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_test_res_Dres作为从用户名到用户的结果. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_test_res_Dres作为从用户名到用户的结果. 休眠:选择testresult0_.recId作为recId6_,testresult0_.CorrectAsnwers作为CorrectA2_6_,testresult0_.DateTaken作为DateTaken6_,testresult0_.IncorrectAsnwers作为Incorrec4_6_,testresult0_. ,将testresult0_.TestDuration作为TestDura9_6_,将testresult0_.TestName作为TestName6_,将testresult0_.TopicName作为TopicName6_,将testresult0_.TotalAsnwers作为TotalAs12_6_,将testresult0_.ToughnessLevel作为Toughne13_6_,将testresult0_ults_Dres作为从用户名到用户的结果.

解决方案

问题不在您的代码中,而在您的设计中.默认情况下,JSF每次遇到#{bean.property}时都会执行吸气剂,如此处详细说明:为什么JSF多次调用吸气剂

简而言之,在托管bean的getter/setter方法中,您绝对不能具有任何业务逻辑.将代码移动到将列表(和其他复杂对象)加载到您控制的其他方法中.

如果在加载页面时需要初始化此列表的数据,请使用@PostConstruct方法:

@ManagedBean
@ViewScoped
public class Bean {
    private List<TestResult> testResults;
    @PostConstruct
    public void init() {
        //here you will load the data for your testResults variable
    }
    public List<TestResult> getTestResults() {
         //clean getter
         return this.testResults;
    }
}

另一条建议:您应该在问题中发布代码,而不是包含代码片段的图片.这样,很容易搜索单词并将其复制/粘贴以在答案中重复使用.


根据您的问题的最新动态进行

来自 @PostConstruct 注释文档:

方法的返回类型必须为空.

您尚未根据答案修改代码,只是在get上添加了@PostConstruct批注,因此得到了奇怪的结果.

这是托管bean的外观:

@ManagedBean
@ViewScoped
public class TableView {
    private boolean showChart = false;
    private boolean _isFiltered;
    private List<TestResult> listResults = null;

    @PostConstruct
    public void init() {
        sessionFactory = HibernateUtil.getSessionFactory();
        session = sessionFactory.openSession();
        session.beginTransaction();
        Query query = session.getNamedQuery("TestResult.getTestResults");
        listResults = query.list();
        session.getTransaction().commit();
        session.close();
    }

    public List<TestResult> getListResults() {
        return this.listResults;
    }
}

在JSF代码中,应该使用#{tableView}而不是#{TableView}.显示示例:

 <p:dataTable id="datatable" value="#{tableView.listResults}" var="results">
 

Just checked all the posts and topics and could not really find a solution, anyway here is the problem - one of the main classes called TestResult.java have two collections List and List with both set to Lazy:

@ViewScoped
@Entity
@NamedQueries({
@NamedQuery(name="TestResult.getTestResults",query="FROM TestResult tr order by DateTaken desc"),
@NamedQuery(name="TestResult.getTestStatistic",query="Select testStatistic FROM TestResult tr where tr.recId = :recId"),
@NamedQuery(name="TestResult.getQuestions",query="Select questionsList FROM TestResult tr where tr.recId = :recId ")

})
@Table(name="TestResult")
public class TestResult implements Serializable {

private transient static final long serialVersionUID = 1L;

transient static final private String pass = "PASSED";
transient static final private String fail = "FAILED";

@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int recId;
private String Username;
private String SavedTestName;
private Date DateTaken;
private int TestDuration;
private int TotalAsnwers;
private int MissedAsnwers;
private int CorrectAsnwers;
private int IncorrectAsnwers;
private int Score;
private String Status;
@OneToMany(fetch=FetchType.LAZY)
private List<UserQuestion> questionsList = new ArrayList<UserQuestion>();
@OneToMany(fetch=FetchType.LAZY)
@Cascade(CascadeType.ALL)
@Fetch(FetchMode.SELECT)
private List<TestStatistic> testStatistic = new ArrayList<TestStatistic>();

getters and setters are here ... 

data loads in the TableView.java class as follows:

@ManagedBean
@ViewScoped
public class TableView {

private boolean showChart = false;
private boolean _isFiltered;
private List<TestResult> listResults = new ArrayList<TestResult>();

in this method

    @PostConstruct
public void loadListResults() {

    sessionFactory = HibernateUtil.getSessionFactory();
    session = sessionFactory.openSession();
    session.beginTransaction();

    Query query = session.getNamedQuery("TestResult.getTestResults");
    listResults = query.list();

    session.getTransaction().commit();
    session.close();

}

the xhtml file is shown here :

    <p:layoutUnit position="center">  
    <h:form>  
        <p:dataTable id="datatable" value="#{tableView.listResults}" var="results" paginator="true" rows="20" 
            paginatorTemplate=
                "{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
            rowStyleClass="#{results.status == 'FAILED' ? 'failed-test' : 'passed-test'}" rowsPerPageTemplate="10,5">
        <p:column>
            <f:facet name="header">Action</f:facet>
            ajax="false" update="panel,display"/>
        <p:commandButton value="Review test" ajax="false" immediate="true"/>
        </p:column>
        <p:column>
            <f:facet name="header">Username</f:facet>
            <p:outputLabel value="#{results.username}"/>
        </p:column>         
        <p:column>
            <f:facet name="header">Test name</f:facet>
            <p:outputLabel value="#{results.savedTestName}"/>
        </p:column>
        <p:column>
            <f:facet name="header">Date taken</f:facet>
            <p:outputLabel value="#{results.dateTaken}"/>
        </p:column>
        <p:column>
            <f:facet name="header">Test name</f:facet>
            <p:outputLabel value="#{results.testName}"/>
        </p:column>         
        <p:column>
            <f:facet name="header">Test duration</f:facet>
            <p:outputLabel value="#{results.testDuration} min"/>
        </p:column>         
        <p:column>
            <f:facet name="header">Total / Correct / Incorrect / Missed</f:facet>
            <p:outputLabel value="#{results.totalAsnwers} / #{results.correctAsnwers} / 
                #{results.incorrectAsnwers} / #{results.missedAsnwers}"/>
        </p:column> 
        <p:column>
            <f:facet name="header">Score</f:facet>
            <p:outputLabel value="#{results.score} %"/>
        </p:column> 
        <p:column>
            <f:facet name="header">Status</f:facet>
            <p:outputLabel value="#{results.status}"/>
        </p:column> 
        </p:dataTable>

after hibernate initializes all the stuff i see in console this : i.e. it executes a query twice, but if i click to the paging controls of the p:dataTable just to change it from 5 to 10 for example, i see in console this:

i have checked all the possible source where the problem could be but still cannot find the solution. that should not be a control related problem?

Here it is a full query output same query runs 18 times!

Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc Hibernate: select testresult0_.recId as recId6_, testresult0_.CorrectAsnwers as CorrectA2_6_, testresult0_.DateTaken as DateTaken6_, testresult0_.IncorrectAsnwers as Incorrec4_6_, testresult0_.MissedAsnwers as MissedAs5_6_, testresult0_.SavedTestName as SavedTes6_6_, testresult0_.Score as Score6_, testresult0_.Status as Status6_, testresult0_.TestDuration as TestDura9_6_, testresult0_.TestName as TestName6_, testresult0_.TopicName as TopicName6_, testresult0_.TotalAsnwers as TotalAs12_6_, testresult0_.ToughnessLevel as Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc

解决方案

The problem is not in your code but in your design. JSF by default will execute the getters everytime it encounters #{bean.property} as heavily explained here: Why JSF calls getters multiple times

In short, you must not have any business logic in your getter/setter methods on managed beans. Move the code to load lists (and other complex objects) to other methods that you control.

If you need to initialize the data of this list when loading the page, use the @PostConstruct method:

@ManagedBean
@ViewScoped
public class Bean {
    private List<TestResult> testResults;
    @PostConstruct
    public void init() {
        //here you will load the data for your testResults variable
    }
    public List<TestResult> getTestResults() {
         //clean getter
         return this.testResults;
    }
}

Another advice: you should post code in your questions, not images that contains fragments of your code. In this way, it is easy to search words and copy/paste it to reuse it in answers.


EDIT based on the update on your question:

From the @PostConstruct annotation documentation:

The return type of the method MUST be void.

You haven't adapted your code based on the answer, you just added the @PostConstruct annotation on your get, thus getting odd results.

This is how your managed bean should look like:

@ManagedBean
@ViewScoped
public class TableView {
    private boolean showChart = false;
    private boolean _isFiltered;
    private List<TestResult> listResults = null;

    @PostConstruct
    public void init() {
        sessionFactory = HibernateUtil.getSessionFactory();
        session = sessionFactory.openSession();
        session.beginTransaction();
        Query query = session.getNamedQuery("TestResult.getTestResults");
        listResults = query.list();
        session.getTransaction().commit();
        session.close();
    }

    public List<TestResult> getListResults() {
        return this.listResults;
    }
}

And in your JSF code, you should use #{tableView} instead of #{TableView}. Showing an example:

<p:dataTable id="datatable" value="#{tableView.listResults}" var="results">

这篇关于对数据库bu Hibernate3的请求相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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