开发者

Escaping colons in hibernate createSQLQuery

I am confused on how I can create an SQL statement containing colons. I am trying to create a view and I am using (notice the double colons):

create view MyView as (
  SELECT 
    tableA.colA as colA,
    tableB.colB as colB, 
    round(tableB.colD / 1024)::numeric, 2) as calcValue,
  FROM 
    tableA, tableB
  WHERE
    tableA.colC = 'someValue'
);

This is a postgres query and I am forced to use the double colons (::) in order to correctly run the statement.

I then pass the above statement through:

s.createSQLQuery(myQuery).executeUpdate();

and I get a:

Exception in thread "main" org.hibernate.exception.DataException: \
    could not execute native bulk manipulation query
 at org.hibernate.exception.SQLStateConverter.convert(\
    SQLStateConverter.java:102)
    ... more stacktrace...

with an output of my above statement changed as (notice the question mark):

create view MyView as (
  SELECT 
    tableA.colA as colA,
    tableB.colB as colB, 
    round(tableB.colD / 1024)?, 2) as calcValue,
  FROM 
    tableA, tableB
  WHERE
    tableA.colC = 'someValue'
);

Obviously, hibernate con开发者_高级运维fuses my colons with named parameters.

Is there a way to escape the colons (a google suggestion that mentions that a single colon is escaped as a double colon does NOT work) or another way of running this statement?

Thanks.


the executeUpdate() method is not intended for DDL statments as CREATE VIEW but for DML statements as UPDATE

you should instead acquire a plain JDBC connection, may be from the Session with s.connection() and run your SQL statement.

Connection connection = s.connection();
Statment statment = connection .createStatement();
try {
statment .execute("CREATE VIEW ...");
} finally {
 statment .close();
}


See Hibernate Native Query problem with named parameters for solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜