Carlos Schuldiner
Carlos Schuldiner

Reputation: 161

Hibernate named query with parameter

I want to create a table with a named query, but on another database. And get the name of the database with a parameter.

I'm trying to do it like this

<sql-query name="createStatement">
    <query-param name="dbName" type="string"/>
    CREATE TABLE `:dbName`.`test` (
      `customer_id` int(11) NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (`customer_id`)
    );
</sql-query>

But I'm getting the Unknown Parameter name Exception. I have tried adding spaces or remove the ` character, but still not working.

What is the correct way to set this parameter?

Upvotes: 0

Views: 260

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562260

Query parameters cannot be used for database identifiers. Nor for table identifiers, column identifiers, or SQL keywords or expressions.

Query parameters can be used only for a single lexical element, and only where you could otherwise use a constant data value, i.e. a quoted string literal, quoted date/time literal, or a numeric literal.

Upvotes: 1

Related Questions