1

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.

5
  • are you getting any error if yes then please attach stack trace with question!!! Commented Apr 22, 2015 at 12:51
  • @Dev : I get an exception ''unexpected token: create'' . As I guessed creating a table in hibernate is different from what I did. I dont know how to do it. Sorry I cant copy the stack trace here. Commented Apr 22, 2015 at 13:11
  • please attach stack-trace It will be really helpful Commented Apr 22, 2015 at 13:18
  • I am not allowed to do that. I removed the line query.executeUpdate(); and now there is no exception but i do not get any results. I searched more about 'create and insert table in hibernate' and seems I have to create .hbm.xml file.... Commented Apr 22, 2015 at 14:01
  • It's hard to understand why you want to use Hibernate for this task. ORM is for mapping your domain objects to a database layer, but you are creating some temporary, probably arbitrarily defined, objects. Why not use sql for this job? Commented Apr 23, 2015 at 11:32

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

3 Comments

what about insert? any idea?
The insert syntax should be INSERT INTO TEMP VALUES ('VAL1')
to solve the CREATE TABLE you could make a procedure and call it using call myprocedure() on a createSQLQuery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.