I can not call @NamedQueries

拜拜、爱过 提交于 2020-01-17 12:39:12

问题


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?


回答1:


You must use something like this

em.createNamedQuery("Customer.findByUsername")
.setParameter("username", "Smith")
.getResultList();



回答2:


You can use TypedQuey:

final TypedQuery<Customer> query = em.createNamedQuery(
            "Customer.findByName", Customer.class)
            .setParameter("name", "Ron");
List<Customer> = query.getResultList();


来源:https://stackoverflow.com/questions/33234225/i-can-not-call-namedqueries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!