3

I would like to implement virtual views with a preprocessor. A simple example:

HQL before:

FROM PublishedArticle a

Effective HQL after:

FROM Article a
WHERE a.published = true

Essentially I need a way to process queries before they get executed (instead of creating views on-the-fly which would have a high cost).

kapa
  • 77,694
  • 21
  • 158
  • 175
vbence
  • 20,084
  • 9
  • 69
  • 118
  • HQL is parsed at deployment – NimChimpsky Apr 23 '14 at 15:37
  • Can't you create a `PublishedArticle` view in your DBMS directly? What DBMS are you using? – sp00m Apr 23 '14 at 15:42
  • @sp00m These dynamic views could be user dependent. At least that's the plan. – vbence Apr 23 '14 at 15:43
  • @vbence What is meant by user-dependant? A `PublishedArticle` view in your DBMS would simply lists any `Article` where `a.published = true` for example. – sp00m Apr 23 '14 at 15:50
  • @sp00m Imagine an example with the `WHERE a.owner='3'` clause. `3` being the ID of the current user. Or a more difficult `WHERE` clause, when creating a view for every permutation is impossible. - That's why I called them `virtual views`. – vbence Apr 23 '14 at 15:57
  • this is just a dynamic query isn't it ? Use the criteria api, or build up a sql string. – NimChimpsky Apr 23 '14 at 16:00
  • @NimChimpsky The query is not necessarily dynamic, at least I'm looking for a way to pre-process HQLs. - A way to hook into the process. – vbence Apr 23 '14 at 16:09
  • 1
    From what I know, Hibernate uses ANTLR to convert HQL to SQL and I haven't seen ANTLR expose any hooks or events to customise the tree generation process. I therefore doubt that Hibernate would provide any hooks either. I would say that creating custom, dynamic queries would be the only way. In my current application we allow users to create fully dynamic queries. We use QueryDSL with Hibernate JPA to construct the queries based on user inputs as we have no upfront knowledge of which fields will be chosen, what joins will be applied or even which tables will be involved. – manish Apr 27 '14 at 05:25
  • Possible duplicate of [How I can configure StatementInspector in Hibernate?](https://stackoverflow.com/questions/39112308/how-i-can-configure-statementinspector-in-hibernate) – Alex R Jun 05 '19 at 05:37
  • @AlexR How could it be? Finding that question implies that you know that there is such a thing as `StatementInspector` - that you just mentioned 5 years after the question. – vbence Jun 11 '19 at 16:06

2 Answers2

1

You can use a StatementInspector to completely rewrite the SQL to suit your needs.

For further reading: How I can configure StatementInspector in Hibernate?

Alex R
  • 11,364
  • 15
  • 100
  • 180
0

Couldn't you do that with a Hibernate filter? Of course for things more complex than WHERE it wouldn't work: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/filters.html

jods
  • 4,581
  • 16
  • 20