PrimeFaces DataTable列表显示和选择

时间:2013-04-30 13:32:34

标签: hibernate jsf-2 primefaces

我想请你帮忙,因为我坚持以下问题。我在开始时要说的另一件事是我在JSF开发中是一个非常新的东西。

我正在使用JSF,PrimeFaces,Hibernate和PostgreSQL。

现在我正在为新用户注册表单。该测试应用程序由四页组成。第一页包含基本的静态信息,第二页是填写用户主要信息的表格,第三页用户将选择免费期限(这是我的问题!),最后一个是重演和按钮保存所有填写或选择信息。

第三页:

这是一个带有无线电复选框和单选的PrimeFaces DataTable。我关注PrimeFaces show caseBalusC's Datatables tutorial。问题  是不可能显示来自db的填充列表。主要问题是属性私有CollectionTerm selectedTerm 应该存储选定行。

错误日志

[javax.el.ELException: Error reading 'dataList' on type de.ibmt.upb.model.CollectionTerm] with root cause
org.postgresql.util.PSQLException: ERROR: column collection0_.selectedterm does not exist
  Position: 78
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302)
    at sun.reflect.GeneratedMethodAccessor38.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at com.sun.proxy.$Proxy25.executeQuery(Unknown Source)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2031)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1832)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1811)
    at org.hibernate.loader.Loader.doQuery(Loader.java:899)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
    at org.hibernate.loader.Loader.doList(Loader.java:2516)
    at org.hibernate.loader.Loader.doList(Loader.java:2502)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332)
    at org.hibernate.loader.Loader.list(Loader.java:2327)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:490)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1247)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    at de.ibmt.upb.model.CollectionTerm.loadDataList(CollectionTerm.java:90)
    at de.ibmt.upb.model.CollectionTerm.getDataList(CollectionTerm.java:83)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
    at org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:169)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    at org.apache.myfaces.view.facelets.el.ContextAwareTagValueExpression.getValue(ContextAwareTagValueExpression.java:96)
    at javax.faces.component._DeltaStateHelper.eval(_DeltaStateHelper.java:246)
    at javax.faces.component.UIData.getValue(UIData.java:2028)
    at org.primefaces.component.datatable.DataTable.getValue(DataTable.java:867)
    at org.primefaces.component.api.UIData.getDataModel(UIData.java:579)
    at org.primefaces.component.api.UIData.setRowModel(UIData.java:409)
    at org.primefaces.component.api.UIData.setRowIndex(UIData.java:401)
    at javax.faces.component.UIData.encodeEnd(UIData.java:1690)
    at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:63)
    at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:47)
    at org.primefaces.component.fieldset.FieldsetRenderer.encodeContent(FieldsetRenderer.java:90)
    at org.primefaces.component.fieldset.FieldsetRenderer.encodeMarkup(FieldsetRenderer.java:72)
    at org.primefaces.component.fieldset.FieldsetRenderer.encodeEnd(FieldsetRenderer.java:46)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:580)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:762)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:758)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:758)
    at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1900)
    at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:285)
    at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:115)
    at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:241)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:199)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

管理bean:

@ManagedBean
@Entity
@Table(name="COLLECTION_TABLE")
public class CollectionTerm implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private Integer c_id;
    private Date dateCollection;
    private Time timeCollection;

    @OneToMany(mappedBy="coll")
    private Collection<UserDetails> userDetails = new ArrayList<UserDetails>();

    @ElementCollection(targetClass=CollectionTerm.class)
    private List<CollectionTerm> dataList;

    private CollectionTerm selectedTerm;

    public CollectionTerm() {

    }

        ///// getters and setters /////

    public List<CollectionTerm> getDataList() {
        if (FacesContext.getCurrentInstance().getRenderResponse()) {
                loadDataList();
        }
        return dataList;
    }

    private void loadDataList() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        dataList = session.createQuery("from CollectionTerm").list();
    }

}

JSF:

<h:form id="collection">
    <p:fieldset legend="Date and time">
        <p:dataTable id="collection" value="#{coll.dataList}" var="coll" rowKey="#{coll.c_id}" selection="coll.selectedTerm">
            <p:column selectionMode="single" style="width:2%" />  
            <p:column >
                #{coll.timeCollection}
            </p:column>
            <p:column>
                #{coll.dateCollection}
            </p:column>
            <p:column>
                #{coll.c_id}
            </p:column>
        </p:dataTable>
        <p:commandButton value="Back" action="registrationback" ajax="false" />
        <p:commandButton value="Next" action="recap" ajax="false" />
        <p:commandButton value="Test" action="testcollection" ajax="false" />
     </p:fieldset>
</h:form>

面 - 配置:

<managed-bean>
    <managed-bean-name>coll</managed-bean-name>
    <managed-bean-class>de.ibmt.upb.model.CollectionTerm</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

<managed-bean>
    <managed-bean-name>userService</managed-bean-name>
    <managed-bean-class>de.ibmt.upb.model.UserDetails</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>term</property-name>
        <value>#{term}</value>
    </managed-property>
    <managed-property> 
        <property-name>coll</property-name>
        <value>#{coll}</value>
    </managed-property>  
</managed-bean>

我做错了什么?

感谢您的回复或推荐!

2 个答案:

答案 0 :(得分:0)

  

org.hibernate.exception.SQLGrammarException:错误:列collection0_.selectedterm不存在

这是你的根本原因。您需要首先解决该映射/数据库错误。然后

  1. 作为最佳实践和可用性的问题,请不要将JPA实体和JSF Managed Bean结合使用。这意味着不应在同一个POJO上声明@ManagedBean@Entity您的bean应该是模型或控制器,而不是两者。

  2. 使用@ManagedBean注释或 faces-config.xml 文件,而不是两者。虽然该文件优先于(我相信)优于注释,但它最终会引起混淆

答案 1 :(得分:0)

Entity bean应该仅仅是你的表的反映。每列应映射到列名。一旦构造了Entity bean,就可以调用它并获得所需的结果。

@Entity
@Table(name="COLLECTION_TABLE")
public class CollectionTerm implements Serializable {
..
..
..
@Id
        @Column(name = "c_id")
        private Integer c_id;
        @Column(name = "date")
        private Date dateCollection;
        @Column(name = "time")
        private Time timeCollection;
...
..

}

与上面的内容匹配您的基础表结构。这应该会给你一个良好的开端。