简体   繁体   中英

SQL Query to Grails Hibernate

With help from a collegue, I managed to get a query to return data that I want from a table that looks like this:

ID    APP_NAME    JVM_NAME    STARTUP_DATE    VERSION_FILE
1     appA        jvm1        xxx             1.3.23
1     appA        jvm2        xxx             1.3.23
1     appA        jvm1        xxx             1.3.23

The query is as follows:

SELECT b.*
  FROM ( SELECT app_name, jvm_name, MAX (startup_date) MDATE
         FROM application_log
         WHERE app_name = '${someValue}'
         GROUP BY app_name, jvm_name) a,
        application_log b
 WHERE a.app_name = B.APP_NAME
   AND a.jvm_name = b.jvm_name
   AND a.MDATE = b.startup_date;

Where I replace ${someValue} with like appA .

I have this table Mapped to a ApplicationLog Grails (2.3.4) domain object, however I'm trying to figure out how I can implement this Query through Grails and map it back to my POJO directly.

My backup plan is I'll get a distinct list of JVM_NAME 's for a Provided APP_NAME and then loop through the values. Per value then lookup the row with the matching JVM_NAME and APP_NAME with the results being ordered by the STARTUP_DATE with a LIMIT 1 applied.

Any suggestions welcome.

Cheers

You ought to be able to convert your query from native SQL to HQL. Should be pretty similar. One difference is that HQL aggregates don't need the "GROUP BY" clause. I'm not sure if this will work as written, but it's probably close...

def hql = """SELECT b.*
  FROM ( SELECT app_name, jvm_name, MAX(startup_date) mate
         FROM application_log
         WHERE app_name = :someValue ) a,
       application_log b
 WHERE a.app_name = b.app_nae
   AND a.jvm_name = b.jvm_name
   AND a.mdate = b.startup_date"""

def logs = ApplicationLog.executeQuery(hql)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM