在JSF dataTable中列出

问题描述 投票:0回答:3

我有两个实体,Employee和publication:我想在数据表中显示发布标题,日期和它的作者存储在数据库中:这是我使用JPQL的查询:

private static EntityManager em;
private static EntityManagerFactory factory;

public PublicationDAO() {
    if(factory==null)
        factory=Persistence.createEntityManagerFactory("mavenTest");
    if(em==null)
        em=factory.createEntityManager();
}

public List<Object[]> getAllPublication() {
     em.getTransaction().begin();
     List<Object[]> pubs = em.createQuery("SELECT c.titrePublication, p.login FROM Publication c  JOIN c.employee p ").getResultList();
     em.getTransaction().commit();
     return pubs;
}

所以我想在XHTML页面的数据表中显示这些信息。

jsf jpa datatable
3个回答
0
投票

目前查询将返回Array,您需要的是Construct返回类型..

  1. 创建自定义类(这将是您的返回类型,它必须具有构造函数,该构造函数接受您在查询字符串中定义的结果值。) 公共类publicationModel { public String publicationName; public String author; public publicationModel (String publicationName, String author) { this.publicationName= publicationName; this.author= author; } }
  2. 使用typedQuery String queryStr =“SELECT NEW example.publicationModel(c.titrePublication,p.login)”+“FROM Publication c JOIN c.employee p”; TypedQuery query = em.createQuery(queryStr,publicationModel .class); List <publicationModel> results = query.getResultList();

我无法格式化答案..但我希望这有助于..


0
投票

我想知道你为什么要把列表的类型设为“对象数组”。将其渲染为实体类的类型就足够了:

public List<Publication> getAllPublication() {
    ...
    List<Publication> pubs = ...
    ...
}

在表示层中指定managedBean类,以执行以下任务:

@RequestScoped
@ManagedBean    
public class PublicationBean {

    // Inject your PublicationDAO here
    private PublicationDAO publicationDao;

    private List<Publication> publications;

    @PostConstruct 
    public retrievePublications () {
        try {
            publications = publicationDao.getAllPublication();
        } catch (Exception e) {
        }
    }

    public List<Publication> getPublications() {
        return this.publications;
    }
    public void setPublications(List<Publication> publications) {
        this.publications= publications;
    }

// getters/setters

}

然后,使用<h:dataTable>显示数据,如下所示:

<h:dataTable value="#{publicationBean.publications}" var="p" rules="all">
    <h:column>
        <f:facet name="header">Publication title</f:facet>
        #{p.titrePublication}
    </h:column>
    <h:column>
        <f:facet name="header">Publication author</f:facet>
        #{p.employee.name}
    </h:column>
 </h:dataTable>

-1
投票

嗨,我终于找到了解决方案:正确的查询应该是:

List<Publication>=em.createQuery("select p from Publication p",Publication.Class).getResultList();

在托管bean中,我应该声明作者是具有getter和setter的Employee实体。然后创建一个名为getAllpublications的方法:

public List<Publication> getAllpublications (){
publicationDao.getAllPublication();//this method is declared and specified in the DAO class
}

之后,xhtml页面应如下所示:

<h:dataTable value="#{publicationBean.publications}" var="p" rules="all">
    <h:column>
        <f:facet name="header">Publication title</f:facet>
       <h:outputText value="#{p.titrePublication}"></h:outputText>
    </h:column>
    <h:column>
        <f:facet name="header">Publication author</f:facet>
        <h:outputText value="#{p.employee.name}"></h:outputText>
    </h:column>
 </h:dataTable>

所以解决方案非常接近你的答案奥马尔非常感谢你的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.