2

I've created a user defined function in hibernate called bitwiseAnd, and registered it in the dialect constructor as follows:

public SQLServerDialect() {
    super();
    registerFunction("bitwiseAnd", new SqlBitwiseFunction("bitwiseAnd", StandardBasicTypes.INTEGER));
}

I needed to do the same for Oracle as we support that too, and because they have different syntax's with regards to bitwise operations. I was hoping to use this in the @DiscriminatorFormula, but it doesn't seem to see the registered function. Is this possible, or does the discriminator formula only use native SQL?

Thanks, R.

Ruaghain
  • 135
  • 1
  • 14

1 Answers1

3

The javadoc of DiscriminatorFormula links to the javadoc of Formula, which contains the following:

The formula has to be a valid SQL fragment.

So yes, the formula must be valid SQL, and not HQL.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Cheers JB, I figured as much. I was however hoping that someone may have come up against this, and thought of something clever! Bit of a sickner that it forces you to use native SQL in certain circumstances, kind of defeats the purpose of Hibernate IMO! – Ruaghain May 01 '12 at 13:39
  • Remember that this formula must be used everywhere (when using session.get(), the Criteria API, etc.) and not just in HQL queries. Also remember that a formula, by definition, is used to transform data in database table columns into an entity property or into a discriminator value. If HQL was used, how would you reference these columns since, by definition, they're not mapped to entity properties? – JB Nizet May 01 '12 at 13:43
  • Ah, of course. I hear what you're saying (my lack of experience with hibernate coming to the fore here). Where I'm looking to run the HQL, the relevant objects haven't been created as yet! Hmmm, I'll look into seeing if I can change the structure of the table to not require the use of a bitwise operator. Thanks JB. – Ruaghain May 01 '12 at 14:23