gpt4 book ai didi

java - Spring Boot + Hibernate + Postgres - 不创建表

转载 作者:行者123 更新时间:2023-11-29 14:06:37 24 4
gpt4 key购买 nike

我正在尝试根据实体生成架构表。应用程序正确启动,生成了 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>

应用程序属性:

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

基础实体.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;
}
}

最佳答案

我已经解决了这个问题。 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

关于java - Spring Boot + Hibernate + Postgres - 不创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38053390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com