Solved

convert sql to JPA

Posted on 2012-03-22
3
2,485 Views
Last Modified: 2012-03-22
I had the below sql and it works well in my data admin..
but I am using this as a named query in eclipse JPA.

SQL:
select * from eventTable eT
where eT.logon_timestamp between (SELECT DATEADD(day, -1, GETDATE()))
and (SELECT DATEADD(day, 0, GETDATE()))

JPA:
@NamedQuery(name = "selectLatestLogonValues", query = "select eventTable from eventTable eT \r\nwhere  eT.logon_timestamp between (SELECT DATEADD(day, -1, GETDATE()))\r\nand (SELECT DATEADD(day, 0, GETDATE()))")})

I got the below error...

java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode
 \-[METHOD_CALL] MethodNode: '('
    +-[METHOD_NAME] IdentNode: 'DATEADD' {originalText=DATEADD}
    \-[EXPR_LIST] SqlNode: 'exprList'
       +-[IDENT] IdentNode: 'day' {originalText=day}
       +-[UNARY_MINUS] UnaryArithmeticNode: '-'
       |  \-[NUM_INT] LiteralNode: '1'
       \-[METHOD_CALL] MethodNode: '('
          +-[METHOD_NAME] IdentNode: 'GETDATE' {originalText=GETDATE}
          \-[EXPR_LIST] SqlNode: 'exprList'


I think the DATEADD(day, 0, GETDATE())) function is not available in JPA...
if not do we have an alternative to this in JPA...

all I am trying to do is... get the table entries who logged in last 24 hours..
0
Comment
Question by:shragi
  • 2
3 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 37753487
what about

@NamedQuery(name = "selectLatestLogonValues", query = "select * from eventTable eT where  eT.logon_timestamp between DATEADD(day, -1, GETDATE()) and DATEADD(day, 0, GETDATE())")
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 37753502
or simpler form

@NamedQuery(name = "selectLatestLogonValues", query = "select * from eventTable eT where  eT.logon_timestamp between GETDATE()-1 and GETDATE()")
0
 
LVL 7

Expert Comment

by:gudii9
ID: 37754298
Here is interesting explanation, links

Hibernate 3.0 will have a new parser for HQL which will make extending current DB function support feature set and adding arithmetic operations much easier. How much is completed above current support and when cannot currently be discussed since its not in stone as yet. The final is some time off but beta will start soon(ish).


https://forum.hibernate.org/viewtopic.php?f=1&t=931857&sid=890fcf751e8e3b2eecc8986b5b39b6b3

http://www.java2s.com/Questions_And_Answers/JPA/Database/hql.htm
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now