0

I am seeing mysql database creation script lie this:

DROP TABLE IF EXISTS `category_pages`;
CREATE TABLE `category_pages` (
  `id` bigint(20) NOT NULL,
  `pages` int(11) default NULL,
  KEY `FK71E8D943E46A97CC` (`id`),
  KEY `FK71E8D943BB482769` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

I believe it's created from entity classes by Hibernate, since the project uses Hibernate to access database. If I define a number of entity classes like this, how can I generate the database creation script like this by Hibernate in Eclipse? I have a web proejct developed in Eclipse.

Is this the normal way to create a database in using Hibernate?

user697911
  • 10,043
  • 25
  • 95
  • 169
  • 1
    setting the property `hibernate.hbm2ddl.auto` combined with the logging option `org.hibernate.tool.hbm2ddl` could provide what you want. http://stackoverflow.com/questions/3829597/how-to-make-hbm2ddl-schemaexport-to-log-schema-to-stdout – thst Mar 21 '16 at 01:11
  • One option would be to use `SHOW CREATE TABLE category_pages`. – Tim Biegeleisen Mar 21 '16 at 01:12
  • Would the script be output to a .sql file? And do I need to create xml logging file? I currently don't have such a file in my project. – user697911 Mar 21 '16 at 01:18
  • @TimBiegeleisen, could you be more specific about how to use "SHOW CREATE TABLE category_pages" for this? – user697911 Mar 21 '16 at 03:09
  • This is a MySQL function, having nothing really to do with Hibernate. Try Googling for it, you will find examples. – Tim Biegeleisen Mar 21 '16 at 03:19
  • Normally, should we use MySQL or Hibernate to create such database creation script? I had thought it should be done Hibernate. But now I guess probably it should come from MySql database, like you said. Do you have any knowledge of this? – user697911 Mar 21 '16 at 03:24
  • Hibernate has the ability to create tables, but once it has done so you can easily figure out what commands were used to create them. – Tim Biegeleisen Mar 21 '16 at 03:59
  • @user697911 - the answer to your question is; it depends - if the tables already exist, there is no need to have Hibernate create them, and the other way around; if they don't exist beforehand, Hibernate might as well create them. Anyway; I do not hope Hibernate use MyISAM tables as default in the generated DDL. If you so not specifically want MyISAM tables, or cannot somehow specify that Hibernate should create InnoDB tables, then do not let Hibernate create them. In general; You have better control if you create the tables yourself. – jon martin solaas Mar 25 '16 at 10:37

0 Answers0