Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Hibernate + create table = ?

Thread view: 
aDeamon - 06 May 2007 16:40 GMT
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?
steen - 07 May 2007 08:29 GMT
> CREATE TABLE table_name (id biginteger, name varchar(50))
>
[quoted text clipped - 7 lines]
>
> 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#createSQLQue
ry(java.lang.String
)

/Steen
aDeamon - 07 May 2007 12:53 GMT
> > CREATE TABLE table_name (id biginteger, name varchar(50))
>
[quoted text clipped - 15 lines]
>
> /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
steen - 07 May 2007 19:37 GMT
> Yes I tryed this to...
>
[quoted text clipped - 6 lines]
>
>         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
aDeamon - 08 May 2007 21:54 GMT
> > Yes I tryed this to...
>
[quoted text clipped - 11 lines]
>
> /Steen

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

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

/Sam


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.