0

刚刚检查了所有的帖子和主题,并没有真正找到解决方案,无论如何这就是问题所在 - 一个名为 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_.MissedAsnwers 作为 MissedAs5_6_,testresult0_.SavedTestName 作为 SavedTes6_6_,testresult0_作为 Status_6_,testresult0_。 , 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_.Date0_.sc recId 为 recId6_,testresult0_.CorrectAsnwers 为 CorrectA2_6_,testresult0_.DateTaken 为 DateTaken6_,testresult0_.IncorrectAsnwers 为 Incorrec4_6_,testresult0_。MissedAsnwers 作为 MissedAs5_6_、testresult0_.SavedTestName 作为 SavedTes6_6_、testresult0_.Score 作为 Score6_、testresult0_.Status 作为 Status6_、testresult0_.TestDuration 作为 TestDura9_6_、testresult0_.TestName 作为 TestName6_、testresult0_.TopicName 作为 TopicName6_、testresult0_.TotalAsnwers_作为 TotalAsnwers_、testresultLevels12 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 作为 SavedTes6_6_, testresult0_.Score 作为 Score6_, testresult0_.Status 作为 Status6_, testresult0_.TestDuration 作为 TestDura9_6_, testresult0_.TestName 作为 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.A 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__.TopicName as TestName6 , 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_.SavedTestResult0_SavedTes6_6_, testresult0_ScoreStatus asnwers. 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_ 按 testresult0_.DateTaken 排序: testresult0_.recId 作为 recId6_,testresult0_.CorrectAsnwers 作为 CorrectA2_6_,testresult0_.DateTaken 作为 DateTaken6_,testresult0_.IncorrectAsnwers 作为 Incorrec4_6_,testresult0_。MissedAsnwers 作为 MissedAs5_6_、testresult0_.SavedTestName 作为 SavedTes6_6_、testresult0_.Score 作为 Score6_、testresult0_.Status 作为 Status6_、testresult0_.TestDuration 作为 TestDura9_6_、testresult0_.TestName 作为 TestName6_、testresult0_.TopicName 作为 TopicName6_、testresult0_.TotalAsnwers_作为 TotalAsnwers_、testresultLevels12 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 作为 SavedTes6_6_, testresult0_.Score 作为 Score6_, testresult0_.Status 作为 Status6_, testresult0_.TestDuration 作为 TestDura9_6_, testresult0_.TestName 作为 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.A 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__.TopicName as TestName6 , 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_.SavedTestResult0_SavedTes6_6_, testresult0_ScoreStatus asnwers. 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_ 按 testresult0_.DateTaken 排序: testresult0_.recId 作为 recId6_,testresult0_.CorrectAsnwers 作为 CorrectA2_6_,testresult0_.DateTaken 作为 DateTaken6_,testresult0_.IncorrectAsnwers 作为 Incorrec4_6_,testresult0_。MissedAsnwers 作为 MissedAs5_6_、testresult0_.SavedTestName 作为 SavedTes6_6_、testresult0_.Score 作为 Score6_、testresult0_.Status 作为 Status6_、testresult0_.TestDuration 作为 TestDura9_6_、testresult0_.TestName 作为 TestName6_、testresult0_.TopicName 作为 TopicName6_、testresult0_.TotalAsnwers_作为 TotalAsnwers_、testresultLevels12 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 作为 SavedTes6_6_, testresult0_.Score 作为 Score6_, testresult0_.Status 作为 Status6_, testresult0_.TestDuration 作为 TestDura9_6_, testresult0_.TestName 作为 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.A 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__.TopicName as TestName6 , 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_.SavedTestResult0_SavedTes6_6_, testresult0_ScoreStatus asnwers. 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_ 按 testresult0_.DateTaken 排序: testresult0_.recId 作为 recId6_,testresult0_.CorrectAsnwers 作为 CorrectA2_6_,testresult0_.DateTaken 作为 DateTaken6_,testresult0_.IncorrectAsnwers 作为 Incorrec4_6_,testresult0_。MissedAsnwers 作为 MissedAs5_6_、testresult0_.SavedTestName 作为 SavedTes6_6_、testresult0_.Score 作为 Score6_、testresult0_.Status 作为 Status6_、testresult0_.TestDuration 作为 TestDura9_6_、testresult0_.TestName 作为 TestName6_、testresult0_.TopicName 作为 TopicName6_、testresult0_.TotalAsnwers_作为 TotalAsnwers_、testresultLevels12 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 作为 SavedTes6_6_, testresult0_.Score 作为 Score6_, testresult0_.Status 作为 Status6_, testresult0_.TestDuration 作为 TestDura9_6_, testresult0_.TestName 作为 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.A 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__.TopicName as TestName6 , 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_.SavedTestResult0_SavedTes6_6_, testresult0_ScoreStatus asnwers. 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_ 按 testresult0_.DateTaken 排序: testresult0_.recId 作为 recId6_,testresult0_.CorrectAsnwers 作为 CorrectA2_6_,testresult0_.DateTaken 作为 DateTaken6_,testresult0_.IncorrectAsnwers 作为 Incorrec4_6_,testresult0_。MissedAsnwers 作为 MissedAs5_6_、testresult0_.SavedTestName 作为 SavedTes6_6_、testresult0_.Score 作为 Score6_、testresult0_.Status 作为 Status6_、testresult0_.TestDuration 作为 TestDura9_6_、testresult0_.TestName 作为 TestName6_、testresult0_.TopicName 作为 TopicName6_、testresult0_.TotalAsnwers_作为 TotalAsnwers_、testresultLevels12 Toughne13_6_, testresult0_.Username as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc用户名 as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc用户名 as Username6_ from TestResult testresult0_ order by testresult0_.DateTaken desc

4

1 回答 1

2

问题不在于您的代码,而在于您的设计。默认情况下,JSF 将在每次遇到时执行 getter,#{bean.property}如下所示:Why JSF calls getter multiple times

简而言之,在托管 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注释文档中:

方法的返回类型必须为 void。

您还没有根据答案调整代码,只是在@PostConstruct上添加了注释get,因此得到了奇怪的结果。

这就是您的托管 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">
于 2013-04-28T23:54:40.250 回答