java.lang.IllegalArgumentException: Named query not found:
Asked Answered
T

5

5

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

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?

Termitarium answered 10/12, 2013 at 15:2 Comment(0)
K
17

Use em.createQuery(... instead of 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.

Kelson answered 10/12, 2013 at 15:25 Comment(0)
H
6

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

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

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

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

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

Huda answered 10/12, 2013 at 15:31 Comment(0)
H
1

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

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

Hiller answered 10/12, 2013 at 15:27 Comment(0)
F
1

I had a similar error when I migrated working code from Java 8 to 17 (and also with 11). I was using EclipseLink (2.5).

The fix was to upgrade EclipseLink to 2.7 and it worked again.

Flita answered 30/8, 2023 at 11:13 Comment(0)
P
-3
Query q =em.createNamedQuery("select bc from Bondecommande bc where bc.idbc = :idbc");

after em.createQuery here, and good

Pirouette answered 10/7, 2015 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.