java.lang.IllegalArgumentException:找不到命名查询: [英] java.lang.IllegalArgumentException: Named query not found:

查看:123
本文介绍了java.lang.IllegalArgumentException:找不到命名查询:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了以下代码

@Stateless
public class BondecomandeDAO {

    @PersistenceContext
    private EntityManager em;

    public Bondecommande findBCbyid(int id)
    {
         Query q =em.createNamedQuery("select bc from Bondecommande bc where bc.idbc = :idbc");
         q.setParameter("idbc", id);
         return  (Bondecommande) q.getResultList().get(0);
     }
}

@Entity
@Table(name="bondecommande")
public class Bondecommande  implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="idbc")
    private int idbc;
    @Column(name="devise")
    private String devise;
    @Column(name="modepaiement")
    private String modepaiement;
    @Column(name="modelivraison")
    private String modelivraison;
    @Column(name="delaipaiement")
    private int delaipaiement;

      ////other attributes , getters and setters 
}

当我尝试运行功能findBCbyid(int id)时,出现此错误

When I try to run the function findBCbyid(int id) I get this error

java.lang.IllegalArgumentException:找不到命名查询:从Bondecommande bc中选择bc,其中bc.idbc =:idbc

java.lang.IllegalArgumentException: Named query not found: select bc from Bondecommande bc where bc.idbc = :idbc

尽管我在另一个项目中使用了这个命名查询,并且它起作用了,但这里可能是什么问题呢?

Although I used this named query in an another project, and it worked, what could be the problem here?

推荐答案

使用em.createQuery(...代替em.createNamedQuery()

Use em.createQuery(... instead of em.createNamedQuery()

如果要使用命名查询(我会建议这样做),则必须将查询放在实体类的@NamedQuery批注内.

If you work with named queries (what I would recommend) you have to place the query inside a @NamedQuery annotation on your entity class.

这篇关于java.lang.IllegalArgumentException:找不到命名查询:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆