0

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 : enter image description here i.e. it executes a query twice, enter image description here 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: enter image description here

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

1 Answers1

2

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">
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • thanks in fact i forgot to mention that - the first and second screenshots are located in different packages : in TestResult.java and TableShow.java as a model and controller respectively –  Apr 29 '13 at 00:04
  • @user1324100 people here can understand it easily if you post the name of the class before posting the code. – Luiggi Mendoza Apr 29 '13 at 00:08
  • @user1324100 by the way, please focus on the provided answer. – Luiggi Mendoza Apr 29 '13 at 00:09
  • well so far add changed both beans to viewscoped and and mark method where records are loading as PostContrust and get no data shown in the main page –  Apr 29 '13 at 00:23
  • @user1324100 please update your question and add your actual implementation showing both managed bean and JSF code in order to provide exact guidance. – Luiggi Mendoza Apr 29 '13 at 00:24
  • ok i got it looks like it should be tableView insead of TableView –  Apr 29 '13 at 00:44
  • regarding the main question - in the paging control change its not loading data anymore but at the start there are still 2 queries execution –  Apr 29 '13 at 00:45