gpt4 book ai didi

java - 我无法调用@NamedQueries

转载 作者:行者123 更新时间:2023-12-01 11:02:23 28 4
gpt4 key购买 nike

我是 JSP/EL/EJB 世界的新手,所以请仔细考虑...呵呵

我的问题是 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 实现为函数,如果需要的话如何实现?

最佳答案

你必须使用这样的东西

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

关于java - 我无法调用@NamedQueries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33234225/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com