0

In my spring project, i am using Hibernate to export my entity classes to a previously created database. But this will require the final user knows how to create a database in the Database manager system (Currently I am using Postgresql).

Is there any way of, given only the machine where the postgresql is installed (and the username and password, which is provided when the application is runned the first time), the Hibernate create a new database in the server if it doesn't exist?

Kleber Mota
  • 8,521
  • 31
  • 94
  • 188
  • possible duplicate of [Hibernate: Automatically creating/updating the db tables based on entity classes](http://stackoverflow.com/questions/306806/hibernate-automatically-creating-updating-the-db-tables-based-on-entity-classes) – Gabriel May 06 '14 at 13:29
  • No duplicate, I don't want create/update tables, but create the database. – Kleber Mota May 06 '14 at 13:31
  • http://stackoverflow.com/questions/306806/hibernate-automatically-creating-updating-the-db-tables-based-on-entity-classes – Pramod S. Nikam May 06 '14 at 13:32
  • possible duplicate of [Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?](http://stackoverflow.com/questions/18389124/simulate-create-database-if-not-exists-for-postgresql) – kazanaki May 06 '14 at 13:54
  • Duplicate of http://stackoverflow.com/questions/18389124/simulate-create-database-if-not-exists-for-postgresql – kazanaki May 06 '14 at 13:54

3 Answers3

2

If your configuration looks like this

<hibernate-configuration>

  <session-factory>

    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="connection.url">jdbc:postgresql://host:port/database</property>
    <property name="connection.username">username</property>
    <property name="connection.password">password</property>


    <property name="current_session_context_class">thread</property>
    <property name="hibernate.show_sql">false</property>
<property name="hbm2ddl.auto">update</property>
  </session-factory>
</hibernate-configuration>

Then the database will be created by Hibernate automatically.

Update:

Ok now I understand what you want. You want to start the Postgresql server with Hibernate. This is not possible. Hibernate does not do this.

You can do this with

  1. Another script that starts with your application
  2. A maven/ant target.
  3. A build job

But the best solution is to use an in-memory database that does not need an external server (for example H2, or Java derby)

See also Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?

and

Postgres database create if not exists

Community
  • 1
  • 1
kazanaki
  • 7,988
  • 8
  • 52
  • 79
  • No, this is not happening: if I run the application without create the database, I receive an error. My configuration file is that: https://github.com/klebermo/maven_example/blob/master/src/main/java/hibernate.properties. – Kleber Mota May 06 '14 at 13:36
  • What is the error? Post this on your question as well as the github project lin. – kazanaki May 06 '14 at 13:38
  • If I don't manually create the database before run the application, I receive this error when the application try access the database: http://www.klebermota.eti.br/wp-content/Apache-Tomcat_7.0.42-Error-report.html – Kleber Mota May 06 '14 at 13:45
0

Take a look of paramater hibernate.hbm2ddl.auto for your hibernate.cfg.xml file. I suggest you this link: Hibernate hbm2ddl.auto, possible values and what they do - any official explanation?

Community
  • 1
  • 1
angel_navarro
  • 1,757
  • 10
  • 11
  • I already creating/updating the tables in the database, I want now be able to create the DATABASE in the server through Hibernate, to avoid let this task to the user. – Kleber Mota May 06 '14 at 13:32
0

Run "CREATE DATABASE ..." (see http://www.postgresql.org/docs/9.0/static/sql-createdatabase.html) as a native SQL query ...

.createSQLQuery(" ... ").executeUpdate(); ...

Hibernate will - at least as far as I know - not create the database, only the tables in it.

I suppose you need to connect to postgresql via a second persistence unit/connection, because of the chicken-and-egg nature of this approach.

jon martin solaas
  • 459
  • 1
  • 4
  • 14