0

I have two entities , Employee and publication : I want to show in a data table the publication title, date, and it's author stored in database : this is my query using 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;
}

so I want to show this information in XHTML page's data table.

Tiny
  • 27,221
  • 105
  • 339
  • 599
fati lem
  • 69
  • 2
  • 12

3 Answers3

0

I wonder why you make the type of the list "array of objects". Rendering it as type of your entity class is enough :

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

Specify a managedBean class in your presentation tier, to do the job :

@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

}

Then, display the data with <h:dataTable> like this :

<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>
Omar
  • 1,430
  • 1
  • 14
  • 31
  • Don't populate the list in a constructor, do it in a `@Postconstruct` annotated method.https://stackoverflow.com/questions/5765853/how-and-when-should-i-load-the-model-from-database-for-hdatatable – Kukeltje Sep 13 '18 at 06:45
  • Yeah, it was not good idea at that time.. I had preferred to use the least approach later. – Omar Sep 13 '18 at 20:49
0

Currently the query would return Array and what you need is a Construct return type..

  1. create custom class (this will be your return type and it must have constructor which takes result values you define in query string. )

    public class publicationModel

    {

    public String publicationName; public String author;

      public publicationModel (String publicationName, String author) 
      {
        this.publicationName= publicationName;
        this.author= author;
      } 
    }
    
  2. use 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();

m not able to format the answer.. but i hope this helps..

-1

hi finally I find the solution : the correct query should be :

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

in the managed bean I should declare The author which is The Employee entity with getter and setter. then create a method called getAllpublications as :

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

after that the xhtml page should look like:

<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>

so the solution is very close to your answer Omar thanks a lot for your help.

fati lem
  • 69
  • 2
  • 12
  • Bad practice to call the dao non-lazy in the getter of the publications. You'll call the dao waaay to many times then. https://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times, https://stackoverflow.com/questions/5765853/how-and-when-should-i-load-the-model-from-database-for-hdatatable – Kukeltje Sep 13 '18 at 06:44