h ketab
h ketab

Reputation: 117

how to create and insert data from a list into a hibernate query

I need to create a temporary table with hibernate and then insert data from a list into this table. here is what I mean :

String hql = "create temporary table temp (id int)";
session.createQuery(hql);

and inside this for loop i insert the data from a list into table 'temp' :

for(Example example : examples) {
    hql = "insert into temp (" + example.getId() + ")";
    query = session.createQuery(hql);
    query.executeUpdate();
}

when i fill the table with the valuse from examples (which is a list), then i can use this table for another query. create and insert to this table has to be done automatically in my code and cant do it in database manually because the list 'examples' is made is the code.

this is what i think about the code, but it does not work. can anybody tell me how i can do this in correct way? thanks.

Upvotes: 1

Views: 2952

Answers (1)

skioppetto
skioppetto

Reputation: 153

I don't think Hibernate Query Language manage "create" operation. Probably you need to run a native query in using createNativeQuery if you are using EntityManager or createSQLQuery if you are using Hibernate Session:

 String hql = "create temporary table temp (id int)";
 session.createSQLQuery(hql).executeUpdate();

Upvotes: 1

Related Questions