How to add table name as one of the hibernation template query parameters

Obviously, the hibernate pattern query inserts the string parameter "tableName" into the quoted query. This will cause SQL grammar syntax errors.

How do I add the table name as one of the query parameters?

For example, the following code won't work because of the table name:

SQLQuery query = session.createSQLQuery("SELECT ID FROM :tableName");
query.setFlushMode(FlushMode.MANUAL);
query.setReadOnly(true);
query.setParameter("tableName", "STUDENT");
List<Object[]> list = query.list();

      

+3


source to share


3 answers


Table names cannot be parameterized in queries.

What you actually want to build String

is to be used to build the query:

SQLQuery query = session.createSQLQuery(getIdsQuery("STUDENT"));
...
private String getIdsQuery(String tableName) {
   return "SELECT ID FROM " + tableName;
}

      

The inability to parameterize table names has nothing to do with Hibernate - databases don't support it. And it would be pointless to support him.

Bind variables exist mainly for performance reasons. The point is that you are submitting a request to the database (via the jdbc driver) with placeholders for variables that will change between calls to the same request:



select id from STUDENT where name = ?

      

The database will analyze this query, calculate the cost for it, draw up an execution plan and cache it. Subsequent calls to the query will be much faster because all these steps are skipped and the execution plan cache is used .

How can a database cache a statement and an execution plan if the table name is unknown (parameterized)? He will not know the cost and the optimal path to retrieve the data if he does not know where to get the data from.

In addition, the database will not be able to fully validate the query when parsing it unless it knows if the referenced columns actually exist in the table (which would not be known during parsing the syntax if the table names were parameterized).

+3


source


Your HQL query should look like this:

Query query = session.createQuery("from Conversation as c where c.userConversation.id=:senderid and c.user1Conversation.id=:receiverid");

      

Here "Conversation" is the name of the model class, "c" is the alias, and userConversation is the many-to-one mapping I have. So to answer your question, you can put the table name in the above way. Don't forget to set your query parameters.

Also, you don't need to manually set FlushMode as you are just reading the list, you don't even need to call session.flush after that. Second, why are you setting hard-coded values ​​for query.setParameter ???



Update

I just read your post. Your SQL query should look like this:

select id,username from Conversation where id=1000;

      

The above is an example, but conversation is still a table name. Also, you tagged your post as Hibernate, so I wrote an HQL query, it is much easier in HQL to do something that I experienced.

0


source


Query query = session.createQuery(String.format("select id from %s", "Student"));

      

this is the same as in the first answer (but using some Java help): you need to construct a String to pass as a request

0


source







All Articles