PostgreSQL + Hibernate + Spring auto create database

后端 未结 6 1294
时光说笑
时光说笑 2020-12-18 07:08

I\'m working with PostgreSQL and Spring 4 and want my app auto create database when it running.

My Entity Class is:

@Entity
@Table(name = \"user\", s         


        
相关标签:
6条回答
  • 2020-12-18 07:45

    The property hibernate.hbm2ddl.auto will do the trick for you. It automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.

    Hibernate can accept these options for the above property.

    validate: validate the schema, makes no changes to the database.

    update: update the schema.

    create: creates the schema, destroying previous data.

    create-drop: drop the schema at the end of the session.

    0 讨论(0)
  • 2020-12-18 07:48

    The problem is about hibernate dialect. You are using old one. You should use newer one like this.

    spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL95Dialect
    
    0 讨论(0)
  • 2020-12-18 07:50

    Postgres unlike mysql does not support Create Database If not exist.

    Thus changing hibernate.hbm2ddl.auto=create and changing URL jdbc.url=jdbc:postgresql://localhost/database?createDatabaseIfNotExist=true won't work for you.

    However you can try simulating the behavior as in below questions:

    Create Postgres database on the fly, if it doesn't exists using Hibernate

    Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?

    0 讨论(0)
  • 2020-12-18 07:50

    you can have a schema.sql script with "CREATE SCHEMA IF NOT EXISTS x;" with

    spring.jpa.properties.hibernate.hbm2ddl.auto=update 
    

    It should work

    0 讨论(0)
  • 2020-12-18 08:03

    Try this way

    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.generate-ddl=true
    spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL94Dialect
    
    spring.datasource.driverClassName=org.postgresql.Driver
    spring.datasource.url= jdbc:postgresql://localhost:5432/postgres
    spring.datasource.username=postgres
    spring.datasource.password=123
    
    spring.jpa.show-sql=true
    spring.session.store-type=none
    

    This is work for me.

    From the Automatic schema generation section of the Hibernate User Guide:

    javax.persistence.schema-generation.database.action

    Setting to perform SchemaManagementTool actions automatically as part of the SessionFactory lifecycle. Valid options are defined by the externalJpaName value of the Action enum:

    • none - No action will be performed.

    • create - Database creation will be generated.

    • drop - Database dropping will be generated.

    • drop-and-create - Database dropping will be generated followed by database creation.

    there spring.jpa.hibernate.ddl-auto=update ==> update, you can change according to your scenario.

    0 讨论(0)
  • 2020-12-18 08:05

    Just change

    from:

    @Table(name = "user") || @Entity(name="user")
    

    to:

    @Table(name = "users") || @Entity(name="users")
    

    Because PostgreSQL has default "user"

    0 讨论(0)
提交回复
热议问题