Sleep options

Our project should be able to run in both Oracle and SQL Server. The problem is that we have a number of our own HQL + queries with non-standard operators (for example, bitand and ||) and functions (i.e. SUBSTR) that work fine in Oracle, but not in SQL Server.

I wonder if Hibernate is able to translate them dynamically. I suppose that using HQL, perhaps this happens because it creates an AST, but I doubt that this applies to native queries as well.

Additional question: what is the best approach related to these nasty requests? Conditional, subclasses, others ... the goal is not to greatly modify the code.

Thanx in advance

+3
source share
2 answers

Use custom dialects for HQL. Instead of using || create your own concat function. Then, in the SQL Server dialog box, add this to the constructor:

registerFunction("concat", new VarArgsSQLFunction(Hibernate.STRING, "", "+", ""));

You do not need to change the dialect of Oracle, because Oracle already has a concat function, so it just passes, but for other functions you may need to register new functions in both.

For SQL queries, since you dynamically create them, you can use base class methods such as super.addBitAndClause (leftSide, rightSide).

You can even switch to a dialect dynamically, although Hibernate did not simplify it by adding an interface:

Dialect d = ((SessionFactoryImpl)sessionFactory).getDialect()
+3
source

HQL .hbm . HQL , . - , . , , sessionfactory .hbm , , HQL , :

Query query = session.getNamedQuery("YourNamedHQLorSQLQuery");
+1

Source: https://habr.com/ru/post/1724762/


All Articles