1

I want to know if JPA provides a way to test the parsing of a JPQL query just before the query gets executed.

Best regards,

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Riadh Belkebir
  • 797
  • 1
  • 12
  • 34
  • you can use HSQLDB to test it, before your run your project (TDD) – LowLevel Jun 13 '16 at 10:48
  • thanks @LowLevel for your answer but I want to test the queries not only before running the project because in my application I add queries from external sources at runtime. – Riadh Belkebir Jun 13 '16 at 10:51
  • and if you catch an SQLException ? – LowLevel Jun 13 '16 at 10:54
  • Yes, this could be a solution to this problem. But this is the last option that I want to use. – Riadh Belkebir Jun 13 '16 at 10:56
  • why? what do you want to know with the test? – LowLevel Jun 13 '16 at 10:56
  • in the worst case, if you want to know what a jpql query tries to do, to prevent damage to your database, you can make your own parser. Kind of `QueryParser` > `QueryParser.isQueryValidated(String query)` – LowLevel Jun 13 '16 at 11:00
  • I might consider your solution. I was just wondering why they do not provide testing options before going to SQLException. – Riadh Belkebir Jun 13 '16 at 11:03
  • I just mean it's great we have lots of useful frameworks, APIs, libraries, but it's not the goal to become a "victim of scopes" at all. If you don't find something, make it yourself. We always need something specific or something very specific, and the key objective is to get what you want to get. – LowLevel Jun 13 '16 at 11:11
  • JPA doesn't have it, but your JPA provider likely has native methods you can call directly. Eclipse's Dali also has JPQL validation, and other IDEs might as well. – Chris Jun 13 '16 at 13:07
  • @Chris could you please give more details? – Riadh Belkebir Jun 13 '16 at 13:10
  • To which part? Eclipse's Dali project or using your ORM's native methods to parse the query without executing it? You are better off searching for info on Dali or your IDE yourself, and you haven't mentioned your JPA provider. If you are using EcllipseLink, see the code example in the answer here http://stackoverflow.com/a/12104085/496099 – Chris Jun 13 '16 at 13:15
  • Thanks @Chris for your answer. I use EcllipseLink. From the link you provided, I do not know how to test if a JPQL query is correct without executing it. – Riadh Belkebir Jun 13 '16 at 13:58
  • correct - do you mean it doesn't give you a validation exception when it gets processed into SQL, or that the SQL created is valid and returns you the expected results. The code example does the first, only execution will ensure the second. – Chris Jun 13 '16 at 15:57

1 Answers1

0

No. The JPA spec doesn't provide an option to "compile" the query (unlike what the JDO persistence spec provides), so there is no reliable way of doing that. Some implementations compile at creation of the Query, and others just before execute.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29