helloWorld
helloWorld

Reputation: 315

How to implement @NamedQuery in Java Web application using JSF and EJB

So I am a noob here, I have created Java Web Application in NetBeans created Entity Classes from Data base ( lets say I have only one table in data base named USER it generated

  1. User.java
  2. UserController.java
  3. UserFacade.java
  4. AbstractFacade.java

and than have created JSF pages from entity classes. It generated

  1. Create.xhtml
  2. Edit.xhtml
  3. List.xhtml
  4. View.xhtml

In User.java I have

@NamedQuery(name = "Users.findByName", query = "SELECT u FROM Users u WHERE u.name = :name"),

I need up jump on how to inplement this query in my View.xhtml, because I only need to show users by Name not all. Can someone please give me an instruction.

Upvotes: 1

Views: 2171

Answers (1)

Jaqen H'ghar
Jaqen H'ghar

Reputation: 4345

As already said its difficult to know what you want. To "implement the query in View.xhtml" does'nt make sense, since the NamedQuery will return a List of Users, and View.xhtml can only show 1 User.

It would make more sense to use it from List.xhtml, however then it would be easier to use datatable filtering; something like

<p:column filterBy="#{item.name}" filterMatchMode="contains">
    <f:facet name="header">
         Name
    </f:facet>
    <h:outputText value="#{item.name}"/>
</p:column>

If you want/need to use the NamedQuery you need to create a method on the EJB ( = UserFacade); something like

public List<User> findByName(String name) {
    return em.createNamedQuery("Users.findByName").setParameter("name", name).getResultList();
}

How you would want to call this method I don't know, since the requirements are not clear.

Upvotes: 1

Related Questions