4

Consider a Cat entity like this (pseudocode):

@Entity
class Cat {
    @Id
    Long id;

    @Column
    String name;

    @Column
    String size; /* may be "BIG", "SMALL", "TINY" see (1) */

    @OneToMany
    List<Cat> offsprings;
}

This HQL query, which searches by 'TINY' sized kittens

from Cat c join c.offsprings o where o.size = 'TINY'

will blow up with

ERROR: invalid input syntax for integer: "TINY"

because size is a special property name, which will, in this case, count number of cats' offsprings.

In a scenario like this, is there a proper way to make Hibernate treat o.size (or possibly o.class and others) as a regular property name?


(1) I'm not an author of this regrettable design, nor do I condone it. However, I'm not in position to modify it at this moment.

Matt
  • 74,352
  • 26
  • 153
  • 180
prook
  • 371
  • 2
  • 13
  • Well, the backticks are a way to make Hibernate force-quote the column names so that **they don't clash with underlying DB's keywords**. IOW, it hints Hibernate how to build a SQL query. I'm after something else. I need to tell Hibernate **not to trigger a HQL-specific behavior** on a special property name `o.size`, and treat it as a regular property instead. – prook Feb 17 '16 at 15:56
  • 1
    seems that the parser prioritizes the special properties. Good example of a badly defined syntax which introduces ambiguities. Why define a special property `.size` when there is already a function `size()`. You should raise a bug report... – wero Feb 17 '16 at 16:31
  • @wero, right, that's a conclusion I'm coming to after spending my afternoon in Hibernate sources with a debugger. (I might be biased from past experience, but I assume my bug report will be rejected, I'll get gavinsplained how Hibernate is a superior framework, and how my design sucks.) – prook Feb 17 '16 at 16:47
  • +1 for gavinsplained. Examining the sources of any high prestige framework is usually a depressing experience. Anyway hope you find a solution. – wero Feb 17 '16 at 17:06

0 Answers0