• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

how to create table in hibernate if table not exist and then do insert operation?

Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want that if table not exist then it will create table and the insert can be done.
I have written the following code.

But is showing error

Here my problem is that these command is not working same time. I am first creating a table and then doing inserting and i have to change the xml file after creating the table. I want that customer will not have to worry that is there table exist or not.

Now my question that is there any way that i could will able to create table if there table not exist and able to do insert operation same time in same xml file.
Posts: 24594
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first of all you can only provide one value for the property "hibernate.hbm2ddl.auto". It is after all a property and properties can only have a single value. So probably whatever is processing that XML file of which you posted a fragment uses the last value it finds, namely "insert". As far as I can see this isn't even a valid value for the property, but my Hibernate experience is out of date; can you show us a document which suggests that you should use "insert"?

When I was using Hibernate I just used "create" for that property and Hibernate would create the tables and permit inserts right away. (There's little point in allowing somebody to create a table and then preventing them from inserting records into it, empty tables are pretty useless.)
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all remove the property :

Then if you are having your entity class with annotation @Table("tablename") and as per exception,
there is a possibility that you are using some fieldname which might be reserved keyword. Can you show your Entity?
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!