Thanks Catalina, yes it is a workable solution, my only issue is that my application abstracts my configuration such that I will need to enhance it in order to add this functionality. My result set is used to populate some rows in an Excel spreadsheet, so I need my results in a single query result row. I was trying to avoid making a custom type to return the required result row. Specifically, my result row needs to return a normal query row from this query:
Select ref, sum(bwCount), sum(bwCount * bwRate), sum(clrCount), clrRate, sum(clrCount * clrRate), sum(bwCount * bwRate) + sum(clrCount * clrRate) Since this query will change depending on user configuration I can't really make a new type to simply support this query result row and then use the native query method, since it requires me to specify a return class at design time and I won't know the return type until run time. This why PSQL is so nice because I can return a result list and work out what the list content is at run time. It's be nice if I could mix PSQL an native SQL like this: Select dl.ref, sum(dl.bwCount), sum(dl.bwCount * dl.bwRate) NATIVE(select dl. Blah blah blah)... Anyway I guess I am thinking out loud now. Thanks for all your assistance! Chris From: catalina wei [mailto:[email protected]] Sent: Wednesday, 24 February 2010 8:29 PM To: [email protected] Subject: Re: Query confusion Chris, you are welcome. I should have caught it on my first glance at your JPQL query string... If it can not be done in JPQL, you can use native SQL. For example, if result is expecting a Long value: List rs =em.createNativeQuery("select sum(c.age * 2) + sum(c.userid * 2) from compuser c",Long.class).getResultList(); Catalina On Tue, Feb 23, 2010 at 11:41 PM, C N Davies <[email protected]> wrote: Thanks Catalina. I didn't see that in the spec so I will take another look at it. It would be nice to have it as a feature but personally I prefer strong adherence to specifications and if I want a feature to try to get the spec improved. I'm going to have to rethink my strategy on how to make this work for me L Appreciate your clarification. Chris From: catalina wei [mailto:[email protected]] Sent: Wednesday, 24 February 2010 6:24 PM To: [email protected] Subject: Re: Query confusion Hi Chris, I did some further analysis on your JPQL query string: Select sum(dl.bwCount * dl.bwRate) + sum(dl.clrCount * dl.clrRate) from DataLine dl It is found in the JPA2 spec that aggregate expressions do not allow arithmetic expression as the input argument. SUM can only take state_field_path_expression as its input argument. Here is the EBNF taken from the spec: aggregate_expression ::= { AVG | MAX | MIN | SUM } ([DISTINCT] state_field_path_expression) | COUNT ([DISTINCT] identification_variable | state_field_path_expression | single_valued_object_path_expression) That is why syntax error is reported for your JPQL query. There are 2 options for you: 1. modify JPQL to do: select dl.bwCount * dl.bwRate + dl.clrCount * dl.clrRate from DataLine dl then caculate SUM in object space from the query result. 2. a JIRA feature can be opened for extending aggregate expression to accept arithmetic expression as input argument. In the investigation of your problem, I did find a bug in OpenJPA 2.x code where the following JPQL should be valid but resulted in syntax error: select sum(c.age) + sum(c.age) from CompUser c This issue will be addressed in https://issues.apache.org/jira/browse/OPENJPA-1533. Catalina On Tue, Feb 23, 2010 at 7:12 PM, C N Davies <[email protected]> wrote: Hi Kevin, Yes mixed luck is always frustrating, but I am working around the issues in different ways so I can keep moving forward. It looks like the 2.0Beta is now working ok for me except for my query issue. My best guess is that my classpath environment was the root of my problem, after cleaning out the previously compiled classes and then doing a restart of my system which would have cleared my environment and restarted my TomCat (6.2) service. Originally I was experiencing the PSQL parsing problem on 1.2.1 so I did some research and came to the conclusion that Catalina later advised me of. As a result I decided to upgrade to 2.0Beta because it would support JPA 2.0. Because of this class loader issue I went back to 1.2.1 and resolved my original issue which was trying to use: Sum(clrCount) * clrRate Changing it to: Sum(clrCount * clrRate) Fixed that issue parsing the esterisk, then I needed to add the two Sum(clrCount * clrRate) + Sum(bwCount * bwRate) Which is where I am now at a dead end L I've been double checking the application to confirm I haven't mistakenly ended up on 1.2.1 again but I am absolutely sure that I am using the 2.0 Beta jar. I am surprised that these symbols are causing an issue because I would have thought loads of people use them in day to day queries, but I guess 2.0 is quite new so maybe I am the first J Thanks for your advice Chris From: Kevin Sutter [mailto:[email protected]] Sent: Wednesday, 24 February 2010 1:21 PM To: [email protected] Cc: [email protected]; catalina wei Subject: Re: Query confusion HI Chris, It sounds like you are having mixed luck... It's difficult when things are not consistent... :-) Let's assume the classloader issue is no longer applicable. Are you experiencing the JPQL issues with the 2.0 Beta? Or, with the 1.2.x code base? Catalina had originally replied that one of your JPQL issues was not defined by JPA 1.0, but was defined in JPA 2.0. So, I'm just clarifying your environment. Thanks for your patience. We'll get to the bottom of your questions soon. Thanks, Kevin On Tue, Feb 23, 2010 at 7:11 PM, C N Davies <[email protected]> wrote: Hi Kevin, It's a bit strange, because I had this issue when I was trying move up to 2.0Beta from 1.2.1 on the weekend. I found that Jira and used Nicolas' patch instead but the same issue occurred even after rebooting and running build clean on my application. So I went back to 1.2.1 because I had other things to do. When you replied to my email on Monday I couldn't remember the error so I put Nicolas' patch back in so I could reproduce the error, but it no longer occurred. Then this morning I went back to the standard 2.0Beta jar and it no longer has the error either. Nuts! After all of this I still have my original issue though, I can't use the "+" symbol in my query, how is everyone else doing addition? This is the piece of my query that will fail: Select sum(dl.bwCount * dl.bwRate) + sum(dl.clrCount * dl.clrRate) from DataLine .blah blah I am trying to add the result of the two calculations, but will always receive the error: Encountered "+" at character 222, but expected: [",", "FROM"]. at org.apache.openjpa.kernel.jpql.JPQL.generateParseException(JPQL.java:9501) at org.apache.openjpa.kernel.jpql.JPQL.jj_consume_token(JPQL.java:9378) at org.apache.openjpa.kernel.jpql.JPQL.from_clause(JPQL.java:245) at org.apache.openjpa.kernel.jpql.JPQL.select_statement(JPQL.java:88) at org.apache.openjpa.kernel.jpql.JPQL.parseQuery(JPQL.java:63) at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder$ParsedJPQL.parse(JPQLEx pressionBuilder.java:1740) Also I found that I can't do this: Sum(dl.bwCount) * dl.bwRate because it will give me a similar parse error with the "*" symbol, so I had to move it inside the brackets instead to et it to work. I can't do that with this new query, I did try using it like this below, where I enclosed the whole calculation inside brackets, but it then bitches about the starting bracket: Select ( sum(dl.bwCount * dl.bwRate) + sum(dl.clrCount * dl.clrRate) ) from DataLine .blah blah Chris From: Kevin Sutter [mailto:[email protected]] Sent: Wednesday, 24 February 2010 2:37 AM To: [email protected] Cc: [email protected]; Donald Woods; Rick Curtis Subject: Re: Query confusion Hi Chris, Looked at the referenced JIRA [1] a bit closer... You mentioned that you ran with Nicolas' patch, but the call stack you posted still has the validator getting loaded. It looks like Nicolas' patch comments out the loading of the Validator and Agent in order to get around the original errors. So, if you run with Nicolas' patch, do you still experience the classloader problem or some other problem? Since you can't currently get around the classloading issue, then you haven't been able to validate Catalina's statements about supporting the jpql updates. Correct? I know that we have tested these validation and agent loaders in the JSE environment and within the WebSphere environment. I'm wondering if the classloading issue is related to the Tomcat classloading mechanism. Kevin [1] https://issues.apache.org/jira/browse/OPENJPA-1410 On Tue, Feb 23, 2010 at 8:40 AM, Kevin Sutter <[email protected]> wrote: Hi Chris, I think I found the JIRA you were referring to... https://issues.apache.org/jira/browse/OPENJPA-1410. I'm not sure what's in Nicolas's patched version. Since Donald is the current assignee, I'll copy him on this reply to see if he has any other ideas. And, I'll copy Rick since the JIRA seems related to some work that he has done in the past. Kevin On Mon, Feb 22, 2010 at 8:49 PM, C N Davies <[email protected]> wrote: Hi Kevin, I can't find it in the Jira, but I added a comment on last Saturday, I downloaded the patched version apache-openjpa-2.0.0-beta\openjpa-all-2.0.0-svn-910423.jar to fix the issue however it did not fix the other issue of being able to use "+" symbol in my query. Chris From: Kevin Sutter [mailto:[email protected]] Sent: Tuesday, 23 February 2010 1:19 AM To: [email protected]; [email protected] Subject: Re: Query confusion Hi Chris, Which classloader issue are you referring to? Thanks, Kevin On Sat, Feb 20, 2010 at 5:49 AM, C N Davies <[email protected]> wrote: Thanks for the info Catalina, I would like to switch to OpenJPA 2.0 Beta but I hit one of the open issues regarding the class loader. So I'm stuck! Thanks Chris From: catalina wei [mailto:[email protected]] Sent: Saturday, 20 February 2010 10:33 PM To: [email protected]; [email protected] Subject: Re: Query confusion Chris, sum(dl.bwCount) * dl.bwRate is a scalar expression. In Java Persistence 1.0 spec, scalar expression is not allowed to appear as a select item. That is why you are getting a syntax error. JPQL in Java Persistence 2.0 spec has updated the syntax and the scalar expression can be a select item. This support is available in OpenJPA 2.0.x releases. Could you try any OpenJPA 2.0.x releases ? Catalina On Sat, Feb 20, 2010 at 12:03 AM, C N Davies <[email protected]> wrote: Thanks Daryl, Pretty obvious, why couldn't I see it! Now I see my next issue, here is my query truncated down to the specific issue: Select sum(dl.bwCount) * dl.bwRate from DLine as dl; <openjpa-1.2.1-r752877:753278 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Encountered "*" at character 111, but expected: [",", "FROM"]. at org.apache.openjpa.kernel.jpql.JPQL.generateParseException(JPQL.java:9501) What is wrong with my "*" symbol? It is documented as supported. I tried like this: (sum(dl.bwCount) * dl.bwRate) Same issue L Thanks! Chris From: Daryl Stultz [mailto:[email protected]] Sent: Saturday, 20 February 2010 12:56 AM To: [email protected]; [email protected] Subject: Re: Query confusion On Fri, Feb 19, 2010 at 8:46 AM, C N Davies <[email protected]> wrote: Hi, Select Name, Age, EmailAddress, Weight from com.mypackage.User were Age > 25 Using OpenJPA I only know I can query like this: Select u from User where Age > 25 You can do this in JPA QL: select u.name, u.age, u.emailAddress, u.weight from User as u But getResultList() will return a list if User entities, but I only want specific fields as would be returned by my standard SQL above. My above example will return an Object[]. Not especially easy to use, a Map would be nice. But there it is. -- Daryl Stultz _____________________________________ 6 Degrees Software and Consulting, Inc. http://www.6degrees.com mailto:[email protected]
