java.lang.IllegalArgumentException:未找到命名查询:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20497816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:21:34  来源:igfitidea点击:

java.lang.IllegalArgumentException: Named query not found:

javajpaejb-3.0entitymanager

提问by Bellil Med Samouel

I got the following code

我得到以下代码

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

and

@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 
}

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

当我尝试运行该函数时,出现findBCbyid(int id)此错误

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

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

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

虽然我在另一个项目中使用了这个命名查询,并且它有效,但这里可能有什么问题?

采纳答案by thomas.scheuchzer

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

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

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

如果您使用命名查询(我建议这样做),您必须将查询放在实体类的 @NamedQuery 注释中。

回答by treeno

createNamedQuery()takes a name of a query and not the query itself.

createNamedQuery()采用查询的名称而不是查询本身。

The Query can be defined by an annotation @NamedQueryTake a look at this: http://docs.oracle.com/javaee/6/api/javax/persistence/NamedQuery.html

查询可以通过注释来定义@NamedQuery看看这个:http: //docs.oracle.com/javaee/6/api/javax/persistence/NamedQuery.html

回答by Flying Dumpling

In JPA query and named query are not the same things.

在 JPA 中查询和命名查询不是一回事。

Named queries have "names" and you define them by adding @NamedQueriesannotation on your entity type class:

命名查询具有“名称”,您可以通过@NamedQueries在实体类型类上添加注释来定义它们:

@Entity
@Table(name="bondecommande")
@NamedQueries({
    @NamedQuery(name="Bondecommande.findByIdbc", query="select bc from Bondecommande bc where bc.idbc = :idbc"),
    @NamedQuery(name="...", query="..."),
}) 
public class Bondecommande  implements Serializable {

    ....
}

You use named queries by passing the name of the query to create method:

您可以通过将查询的名称传递给 create 方法来使用命名查询:

Query q =em.createNamedQuery("Bondecommande.findByIdbc");

I recommend to use TypedQuery<T>not Queryif you are using JPA 2.x

如果您使用的是 JPA 2.x,我建议TypedQuery<T>不要Query使用

回答by matos

Query q =em.createNamedQuery("select bc from Bondecommande bc where bc.idbc = :idbc");

after em.createQueryhere, and good

em.createQuery这之后,很好