java - hibernate可以在实体表创建后初始化数据吗?

标签 java sql spring-boot hibernate spring-data-jpa

在 Spring Boot 中,Hibernate 有没有办法仅在创建特定实体表后添加一些 data.sql 。 (我不想在初始化 Spring Boot 应用程序时初始化架构和数据)

    package io.fall.model;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @Entity
    @Table(name = "userprofile")
    public class UserProfile {
        
        @Id
        @GeneratedValue
        private int id;
        private int theme;
        private String userName;

        UserProfile(){}

        public int getTheme() {
            return theme;
        }

        public void setTheme(int theme) {
            this.theme = theme;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }
    }

我想在hibernate创建实体表后插入这些值userprofile

INSERT INTO userprofile (id,theme,userName,summary) 
VALUES (1, 1 , 'John Doee'),
       (2, 2 , 'Jenny Doee'),
       (3, 3 , 'James Doee');

最佳答案

如果使用 hibernate schema generation那么你可以尝试使用importing script files为了您的目的。

spring.jpa.properties.hibernate.hbm2ddl.import_files=data.sql

Hibernate会在schema自动生成后执行脚本文件。

或者,您也可以使用 javax.persistence.schema- Generation.create-script-source 属性(请参阅文档的 this section)来达到相同目的。

关于java - hibernate可以在实体表创建后初始化数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68448853/

相关文章:

java - 如何从图像数据创建 java.awt.Image?

c# - 更改日期格式

sql - join 子句与 where 子句中的 oracle sql 自连接过滤器

sql - 性能问题 : difference between select s. * 与 select *

java - 如何在包含的 JAR 中制作 Spring 的 @ComponentScan 搜索组件

java - 如何在java类中使用tomee.xml中声明的数据源

java - 在启动Intent之前更改布局内容,例如文本和图像

java - 从 API 检索艺术家 ID——将它们处理为 long 还是 int?

java - 将 CascadeType.MERGE 与 Spring Data Repositories 一起使用的正确方法是什么?

java - 开发环境中的 Gradle 构建参数