Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute sql CREATE TABLE query in Hibernate

Tags:

sql

hibernate

hql

I need to dynamically create a table using a Java method and tranform all its rows into a list of Mapping class objects. The questions are..

  • Is there a way to execute CREATE TABLE query dynamically?

  • I saw some examples using doInHibernate() but it didn't work when I tried it. Can I do this without the particular method?

like image 526
Best Avatar asked Jul 24 '26 14:07

Best


2 Answers

You could just execute a native sql query: session.createSQLQuery("create table .....").executeUpdate(); where "session" is your Hibernate session.

If you already have the mapping files, though, you can just set the hibernate.hbm2ddl.auto property in your hibernate configuration to generate the schema based on the mapping files.

like image 76
Chris Avatar answered Jul 27 '26 03:07

Chris


Try this:

session.createSQLQuery(query).executeUpdate();

another possibility would be:

createStatement().execute(someddl);
like image 26
dbrin Avatar answered Jul 27 '26 04:07

dbrin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!