I can not call @NamedQueries

2019-09-09 09:42发布

I'm new in the JSP/EL/EJB WORLD so take it in count please... heheh

My question is a continuation of a PREVIOUS Q?

so in the comments/answers of the question above is recommended to use customerFacade.findByUserName(String userName), but the problem is that I can't do use of such @NamedQuery.

My code is as Follow:

CustomerFacade

package Session;

//imports **

@Stateless
public class CustomerFacade extends AbstractFacade<Customer> {

  @PersistenceContext(unitName = "OnlineStorePU")
  private EntityManager em;

  @Override
  protected EntityManager getEntityManager() {
    return em;
  }

  public CustomerFacade() {
    super(Customer.class);
  }

}

Customer

package Entity;

//imports***

@Entity
@Table(name = "customer")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c"),
@NamedQuery(name = "Customer.findById", query = "SELECT c FROM Customer c WHERE c.id = :id"),
@NamedQuery(name = "Customer.findByName", query = "SELECT c FROM Customer c WHERE c.name = :name"),
@NamedQuery(name = "Customer.findByMobile", query = "SELECT c FROM Customer c WHERE c.mobile = :mobile"),
@NamedQuery(name = "Customer.findByEmail", query = "SELECT c FROM Customer c WHERE c.email = :email"),
@NamedQuery(name = "Customer.findByAddress", query = "SELECT c FROM Customer c WHERE c.address = :address"),
@NamedQuery(name = "Customer.findByFax", query = "SELECT c FROM Customer c WHERE c.fax = :fax"),
@NamedQuery(name = "Customer.findByTelephone", query = "SELECT c FROM Customer c WHERE c.telephone = :telephone"),
@NamedQuery(name = "Customer.findByUsername", query = "SELECT c FROM Customer c WHERE c.username = :username")})

public class Customer implements Serializable {
  /**
  *  all generated attributes from DB import Wizard
  *  no findAll() or findByXxxxx() is declared here
  *  So that is the reason I Assume that all @NamedQueries{}
  *  are somehow creating functions with Name as @NameQuery
  *
  */
}

My question is Do I need to implement the @NamedQuery as functions, if so HOW?

2条回答
一纸荒年 Trace。
2楼-- · 2019-09-09 09:57

You must use something like this

em.createNamedQuery("Customer.findByUsername")
.setParameter("username", "Smith")
.getResultList();
查看更多
太酷不给撩
3楼-- · 2019-09-09 10:06

You can use TypedQuey:

final TypedQuery<Customer> query = em.createNamedQuery(
            "Customer.findByName", Customer.class)
            .setParameter("name", "Ron");
List<Customer> = query.getResultList();
查看更多
登录 后发表回答