我不能打电话给@NamedQueries

时间:2015-10-20 10:28:34

标签: java jsp ejb facade

我是JSP / EL / EJB WORLD的新手,所以请把它计算在内...... heheh

我的问题是PREVIOUS Q?

的延续

所以在上述问题的评论/答案中建议使用customerFacade.findByUserName(String userName),但问题是我不能使用这样的@NamedQuery。

我的代码如下:

为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);
  }

}

客户

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
  *
  */
}

我的问题是我是否需要将@NamedQuery实现为函数,如果是这样的话?

2 个答案:

答案 0 :(得分:1)

你必须使用这样的东西

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

答案 1 :(得分:0)

您可以使用TypedQuey

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