Hibernate + create table = ?

A

aDeamon

Hi,

I am a (new) Hibernate 3.2 user and trying out diffrent things.

For example I have managed to setup an application that inserts,
retrives, and delete objects in a database. I have also instructed
hibernate to update the set of tables when ever it restarts (with
hibernate.hbm2ddl.auto=update)

But what I am instructed to do is also to create a new table after
that the application has started. Probably not a good idea cause you
cant do the "fancy" Hibernate things on this new table. But
instructions are instructions, right...

Now this is not so hard to do when you use plain SQL statements such
as

CREATE TABLE table_name (id biginteger, name varchar(50))


When I use this as String query in the following

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.createQuery(query);


I get an error

unexpected token: create


Probably because create isn't allowed. My question then - is there
someway to create a new table with Hibernate after application started?
 
S

steen

CREATE TABLE table_name (id biginteger, name varchar(50))

When I use this as String query in the following

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.createQuery(query);

I get an error

unexpected token: create

The reason you get "unexpected token" is because you feed it an SQL
query, but the method createQuery expects a HQL query
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#createQuery(java.lang.String)

If you really, really want to do the create by hand, you should use
the method createSQLQuery, which takes an SQL query as parameter.
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#createSQLQuery(java.lang.String)

/Steen
 
A

aDeamon

The reason you get "unexpected token" is because you feed it an SQL
query, but the method createQuery expects a HQL queryhttp://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#c...)

If you really, really want to do the create by hand, you should use
the method createSQLQuery, which takes an SQL query as parameter.http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#c...)

/Steen

Yes I tryed this to...

Session session =
HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();

String query = "CREATE TABLE testTable (id INTEGER NOT NULL
PRIMARY KEY AUTO_INCREMENT, col VARCHAR(255))";

session.createSQLQuery(query);


....the problem then is that the table dose not show up in the table
afterwards. In MySQL the command "show tables;" show nothing.

Is there some configuration that I need to set?

/Sam
 
S

steen

Yes I tryed this to...

Session session =
HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();

String query = "CREATE TABLE testTable (id INTEGER NOT NULL
PRIMARY KEY AUTO_INCREMENT, col VARCHAR(255))";

session.createSQLQuery(query);

Hint: remember to execute the query. The code you pasted just creates
the query...it doesn't actually execute it unless u ask it to.

/Steen
 
A

aDeamon

Hint: remember to execute the query. The code you pasted just creates
the query...it doesn't actually execute it unless u ask it to.

/Steen

LOL - I noticed that, how stuipd am I...

thanks anyway. Sometimes the obvius are to obvius to see..

/Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top