Java Spring Hibernate没有根据我的实体创建表[重复]

Posted

技术标签:

【中文标题】Java Spring Hibernate没有根据我的实体创建表[重复]【英文标题】:Java Spring Hibernate is not creating tables based on my entities [duplicate] 【发布时间】:2021-10-11 16:04:46 【问题描述】:

我正在尝试在 Spring 中创建一个项目。我想使用 Hibernate 来生成我的表。不幸的是,由于某些我似乎找不到的原因,Hibernate 没有创建我的表。我希望它根据我的实体创建我的表。

这是我的实体:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Book 
    @Id
    private Long id;
    @Column(name = "title")
    private String title;
    @Column(name = "author")
    private String author;
    @Column(name = "description")
    private String description;
    @Column(name = "release_year")
    private Integer releaseYear;
    @Column(name = "rentalRate")
    private Integer rentalRate;
    @Column(name = "pages")
    private Integer pages;
    @Column(name = "replacement_cost")
    private Float replacementCost;
    @Column(name = "rating")
    private Float rating;

    @ManyToOne
    @JoinColumn(name="languageId", nullable=false)
    private Language language;


和:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Table(name="Language")
public class Language 
    @Id
    private Long languageId;
    @Column(name = "name")
    private String name;
    @Column(name = "last_updated")
    private String lastUpdated;

    @OneToMany(mappedBy = "language")
    private Set<Book> books;

另外,这是我的 POM 文件:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>ro.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <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.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--Hibernate-->

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.27.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>


        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <version>7.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>

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

还有我的应用属性:

server.port=8080
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=practice_user
spring.datasource.password=practice_user
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.show-sql=true
spring.flyway.enabled=false
spring.jpa.hibernate.ddl-auto=update
spring.security.user.name=admin
spring.security.user.password=admin
spring.jpa.generate-ddl=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

我已经尝试过的事情:

更改spring.jpa.hibernate.ddl-auto=create 更改spring.jpa.database-platform=Postgres

【问题讨论】:

@GiorgiTsiklaurinope,我碰到它并尝试过,但仍然没有 运行应用时是否有警告日志? 应用正常启动,但没有创建表 看来配置设置都是正确的。你的项目中有不同的包吗?确保两个实体类都可用于 Spring Boot。 @Superchamp 这是我的项目结构:gyazo.com/faad2fe981de20dd6fe23c6cec1eab49 【参考方案1】:

以下 application.yml 文件有效:

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=practice_user
spring.datasource.password=practice_user
spring.datasource.hikari.schema=public
spring.jpa.generate-ddl = true

如果默认未设置为公共,您可以替换架构名称。

另外,将@Table(name = "BOOK") 注释添加到您的图书实体

【讨论】:

-在应用程序属性中添加了建议的行-我确保架构名称是“公共”-我将@Table注释添加到实体它仍然不生成表 如果您对 pom 文件进行了任何更改,您之后是否运行了 maven clean install ?另外,您的 postgres 服务器是否已启动并正在运行? 是的,先生,我确实运行了 maven clean install

以上是关于Java Spring Hibernate没有根据我的实体创建表[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Java Spring JPA Hibernate没有更新列

使用 Hibernate 和 Spring 根据用户登录连接到租户数据库

spring和hibernate整合之---java.lang.ClassNotFoundException: javax.el.ELManager 大坑

在 Java 9 中删除 sun.misc.Unsafe 将破坏 Spring、Hibernate

另一个jdbc:mysql没有合适的驱动程序,Spring + Hibernate [重复]

Spring Boot / JPA / Hibernate,如何根据 Spring 配置文件切换数据库供应商?