Spring Boot + Hibernate + Postgres - 不创建表格 [英] Spring Boot + Hibernate + Postgres - not creating tables

查看:84
本文介绍了Spring Boot + Hibernate + Postgres - 不创建表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的pom.xml:

 <?xml version =1.0encoding =UTF-8?> 
< project xmlns =http://maven.apache.org/POM/4.0.0xmlns: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>
<包装> jar< / packaging>

< name> ApplicationName< / name>
< description>应用程序名称< / description>

< parent>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-parent< / artifactId>
< version> 1.3.5.RELEASE< / version>
< relativePath /> <! - 从存储库查找父级 - >
< / parent>

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

<依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-actuator< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-data-jpa< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-security< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-web< / artifactId>
< /依赖关系>

< dependency>
< groupId> org.postgresql< / groupId>
< artifactId> postgresql< / artifactId>
<! - < scope>运行时< / scope> - >
< version> 9.4-1201-jdbc41< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-test< / artifactId>
< scope> test< / scope>
< /依赖关系>
< /依赖关系>

< 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 =创建
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;
私人日期createdOn;
public String description;
$ b $ 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;




$ b

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

  package com.agileplayers.applicationname.core.domain; 

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

@Entity
公共类条目扩展BaseEntity {

私有字符串类型;

@ManyToOne
私人账户;
$ b public 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;
}
}


解决方案

I已经解决了这个问题。而不是 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


I am trying to generate schema tables based on entities. The application starts correctly, the SQL is generated but there is no result - none of tables are created. What's wrong? I have used the same settings in plain Spring MVC + Hibernate JPA without Spring Boot and everything was working correctly.

Here is my 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;
    }
}

example of Entity which extends 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;
    }
}

解决方案

I have fixed the issue. Instead of spring.datasource.schema=schemaName there should be spring.jpa.properties.hibernate.default_schema=schemaName.

The set of necessary properties required for generating tables in my case is the following:

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

这篇关于Spring Boot + Hibernate + Postgres - 不创建表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆