-1

When I tried to execute this query, I got this exception

Exception in thread "main" java.lang.IllegalArgumentException: NamedQuery of name: odcalls.call not found.

This is my named query

 @NamedQuery(name="odcalls.call", 
             query="SELECT C.count(id)," 
                        + "C.date(CalllocalTime)," 
                        + "C.hour(CalllocalTime) from ODCalls as C " 
                  + "where "
                  + " C.date(CallLocalTime) between date_sub(curdate(), Interval 12 month) and date_sub(curdate(),Interval 1 day) : Date " 
                  + " and C.FirstQueue != 0 : String " 
                  + " and C.Calltype in (1) : int " 
                  + "GROUP BY C.hour(CallLocalTime), C.date(CallLocalTime)")

This is my DAO

     public class ODCallsImpl implements IDao <ODCalls> {
         EntityManager em = EntitiyManagerUtil.getInstance().getEntityManager();
         private List<ODCalls> calls = new ArrayList<ODCalls>();

         @SuppressWarnings("unchecked"  @Override
         @PersistenceContext
         public List<ODCalls> getAll() {
              calls = em.createNamedQuery("odcalls.call",ODCalls.class).getResultList();
              em.close();
              return calls;
         }
RubioRic
  • 2,442
  • 4
  • 28
  • 35
  • 1
    Please format your code (in Eclipse, select and `Ctrl + Shift + F`). Now it is absolutely impossible to read. – Alex Salauyou Apr 13 '16 at 14:57
  • problem is when tired than stack overglow said × Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon. – user3656547 Apr 14 '16 at 06:39
  • @user3656547 I've formated your text. I think that you can accept the changes. – RubioRic Apr 14 '16 at 06:51
  • Thank you Rubio for the formatting – user3656547 Apr 14 '16 at 07:18
  • You're welcome. I've just found this "Use em.createQuery(... instead of em.createNamedQuery(). " from the accepted answer http://stackoverflow.com/questions/20497816/java-lang-illegalargumentexception-named-query-not-found – RubioRic Apr 14 '16 at 07:23
  • Where is your `namedQuery` defined? In your Entity class? – Patrick Apr 14 '16 at 07:30
  • Nope @Entity @Table(name="odcalls", schema= "`sogedes-db`") public class ODCalls { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="ID") private String id; @Column(name="CallLocalTime") @Temporal(TemporalType.TIMESTAMP) private Date calllocaltime; @Column(name="CallType") private int calltype; This is my entity class. i also create setter or getter of this class – user3656547 Apr 14 '16 at 07:40
  • @Rubio I tired with em.createQuery but this one is also not working its say Exception in thread "main" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: Exception Description: Syntax error parsing [odcalls.call] – user3656547 Apr 14 '16 at 07:44
  • You're right. It was not the same problem. Sorry, my mistake. – RubioRic Apr 14 '16 at 08:58

1 Answers1

0

Try to declare your @NamedQuery in your Entity class.

@Entity 
@Table(name="odcalls", schema= "sogedes-db")
@NamedQueries({
    @NamedQuery(name="odcalls.call", 
             query="SELECT C.count(id)," 
                        + "C.date(CalllocalTime)," 
                        + "C.hour(CalllocalTime) from ODCalls as C " 
                  + "where "
                  + " C.date(CallLocalTime) between date_sub(curdate(), Interval 12 month) and date_sub(curdate(),Interval 1 day) : Date " 
                  + " and C.FirstQueue != 0 : String " 
                  + " and C.Calltype in (1) : int " 
                  + "GROUP BY C.hour(CallLocalTime), C.date(CallLocalTime)")
}) 
    public class ODCalls {  
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    @Column(name="ID") 
    private String id; 
    @Column(name="CallLocalTime") 
    @Temporal(TemporalType.TIMESTAMP)   
    private Date calllocaltime; 
    @Column(name="CallType") 
    private int calltype;
Patrick
  • 12,336
  • 15
  • 73
  • 115
  • @ Patrick this code is not working. its say Exception in thread "main" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: Exception Description: Syntax error parsing [odcalls.call]. – user3656547 Apr 14 '16 at 12:22
  • @user3656547 do you want to create a jpql/hql query or a native query? If you want to create a native one, try it with this `@NamedNativeQuery`. If not, youre query is not valid – Patrick Apr 14 '16 at 12:30
  • I want to create JPQL query.I think i have problem with parsing[odcalls.call] – user3656547 Apr 14 '16 at 12:39
  • @user3656547 For me your sql looks not like a valid jpql. Be sure that your query is a valid jpql. Instead you can thest the `@NamedNativeQuery` with your example query. – Patrick Apr 14 '16 at 13:05
  • why it is not look like? this is my sql query and its work.SELECT count(id), date(CalllocalTime), hour(CalllocalTime) from ODCalls where date(CallLocalTime) between date_sub(curdate(), Interval 12 month) and date_sub(curdate(),Interval 1 day) and FirstQueue != 0 and Calltype in (1) GROUP BY hour(CallLocalTime),date(CallLocalTime) above is my JPQL query.If you find error please tell me – user3656547 Apr 14 '16 at 13:14
  • @user3656547 For example the keyword `interval`ist not valid in jpql I think. – Patrick Apr 14 '16 at 13:30
  • Okay. I tired without keyword Interval but its gave me same exception. – user3656547 Apr 14 '16 at 14:33