Spring Boot + Hibernate + Postgres - 不创建表

Posted

技术标签:

【中文标题】Spring Boot + Hibernate + Postgres - 不创建表【英文标题】:Spring Boot + Hibernate + Postgres - not creating tables 【发布时间】:2016-10-29 10:24:57 【问题描述】:

我正在尝试基于实体生成架构表。应用程序正确启动,生成了 SQL 但没有结果 - 没有创建任何表。怎么了?我在没有 Spring Boot 的普通 Spring MVC + Hibernate JPA 中使用了相同的设置,并且一切正常。

这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.agileplayers</groupId>
    <artifactId>applicationname</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ApplicationName</name>
    <description>Application Name</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <!--<scope>runtime</scope>-->
            <version>9.4-1201-jdbc41</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.properties:

spring.datasource.url = jdbc:postgresql://localhost:5432/postgres
spring.datasource.driverClassName = org.postgresql.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL82Dialect
spring.datasource.schema=schemaName
spring.datasource.username=userName
spring.datasource.password=userPassword

spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true

BaseEntity.java:

package com.agileplayers.applicationname.core.domain;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.util.Date;

@MappedSuperclass
public class BaseEntity 
    @Id
    @GeneratedValue
    private int id;
    private Date createdOn;
    public String description;

    public BaseEntity() 
    

    public int getId() 
        return id;
    

    public void setId(int id) 
        this.id = id;
    

    public Date getCreatedOn() 
        return createdOn;
    

    public void setCreatedOn(Date createdOn) 
        this.createdOn = createdOn;
    

    public String getDescription() 
        return description;
    

    public void setDescription(String description) 
        this.description = description;
    

扩展 BaseEntity 的实体示例 - Entry.java:

package com.agileplayers.applicationname.core.domain;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import java.util.Date;

@Entity
public class Entry  extends BaseEntity

    private String type;

    @ManyToOne
    private Account account;

    public Entry() 
    

    public String getType() 
        return type;
    

    public void setType(String type) 
        this.type = type;
    

    public Account getAccount() 
        return account;
    

    public void setAccount(Account account) 
        this.account = account;
    

【问题讨论】:

【参考方案1】:

我已经解决了这个问题。而不是spring.datasource.schema=schemaName,应该是spring.jpa.properties.hibernate.default_schema=schemaName

在我的例子中,生成表格所需的一组必要属性如下:

spring.datasource.url = jdbc:postgresql://localhost:5432/postgres
spring.jpa.properties.hibernate.default_schema = schemaName
spring.datasource.username = userName
spring.datasource.password = userPassword
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.show-sql = true

【讨论】:

默认情况下,postgres 使用“公共”模式名称。 spring.jpa.properties.hibernate.default_schema - 就是这样! 10x

以上是关于Spring Boot + Hibernate + Postgres - 不创建表的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot/Hibernate 创建表失败

Spring Boot JPA Hibernate - 以毫秒精度存储日期

spring boot DAO之Hibernate

制作多个 EntityManager(Spring-Boot-JPA、Hibernate、MSSQL)

Spring Boot ManyToMany - *** - JPA,Hibernate

Hibernate 和 CRUDRepository Spring Boot