java - JPA + Hibernate + CrudRepository 刷新?

标签 java spring hibernate jpa

我想刷新我的数据,为此我有以下方法

@RequestMapping(value = "/add", method = RequestMethod.POST)
@Transactional
public String musicSubmit(@ModelAttribute Music music, Model model) {
    musicRepository.saveAndFlush(music);
    model.addAttribute("music", music);
    return "result";
}

我的模型

public interface MusicRepository extends JpaRepository<Music, Integer> {

}

我的主要

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

我的数据是可见的,但如果我关闭应用程序并再次运行,即使我像这样设置 applications.properties,它们也会消失

spring.jpa.hibernate.ddl-auto = update

即使关闭应用程序或重新启动计算机,如何保留我的数据?

最佳答案

application.properties中使用这些:

spring.datasource.url = jdbc:h2:file:~/some_path/YourVeryDbFile
spring.datasource.username = sa
spring.datasource.password =
spring.datasource.driverClassName = org.h2.Driver

spring.jpa.database = H2

spring.jpa.show-sql = true

请注意,这使用了H2;如果您想要其他内容(MYSQL),您应该以其他方式指定它(并相应地更改值)。

看看here .

关于java - JPA + Hibernate + CrudRepository 刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29378329/

相关文章:

Java - 将整数值设置为最大图像大小

java - 如何使用 pom.xml 将参数传递给 Maven 构建?

java.io.FileNotFoundException : class path resource even though file exists in src/main/resources

java - 当用户名用 @email 注释时,Spring 或 Hibernate 不生成 id

java - 从同一类中的方法调用构造函数

java - 仅在特定情况下保留 Vaadin 中的 View 状态

spring - 如何使用 Thymeleaf 处理 TEXT 电子邮件模板?

java - gradle构建时未生成Clojure(声明规则)文件

java - Hibernate 写入后读取导致找不到对象

mysql - Hibernate 主键重复条目