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?

5

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

No, this is not happening: if I run the application without create the database, I receive an error. My configuration file is that: github.com/klebermo/maven_example/blob/master/src/main/java/….
What is the error? Post this on your question as well as the github project lin.
If I don't manually create the database before run the application, I receive this error when the application try access the database: klebermota.eti.br/wp-content/…
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?

1 Comment

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.