首页 » springcloud

jpa报错,Table 'DBNAME.hibernate_sequence' doesn't exist,这个解决办法是加上自增

尝试如下:

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long id;

或者配置

spring:
  jpa:
    hibernate:
      use-new-id-generator-mappings: false

h2内置的tool可以备份还原h2数据库。

1.首先引入了h2的依赖,注意scope是compile,spring boot 默认是runtime,没法使用

  <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>compile</scope>
        </dependency>

2.要使用文件数据库,内存数据库貌似不行,

spring.datasource.url=jdbc:h2:file:~/db/mydb
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=update

3.java 代码

import org.h2.tools.Backup;
import org.h2.tools.Restore;
@RequestMapping("/bak")
    public String bak() throws Exception {
        String file = "~/db/bak/backup-" + JodaDateUtil.formatDate(JodaDateUtil.set000000(new Date()), "yyyy-MM-dd") + ".zip";

        Backup.execute(file, "~/db", "mydb", true);
        return "0k";
    }

    @RequestMapping("/res")
    public String res() throws Exception {
        String file = "~/db/bak/backup-" + JodaDateUtil.formatDate(JodaDateUtil.set000000(new Date()), "yyyy-MM-dd") + ".zip";
        File f = new File(file);
        int i = 1;
        while (!f.exists()) {
            DateTime d = new DateTime();
            d.minusDays(i);
            file = "~/db/bak/backup-" + JodaDateUtil.formatDate(JodaDateUtil.set000000(d.toDate()), "yyyy-MM-dd") + ".zip";
            f = new File(file);
            i++;
        }
        Restore.execute(file, "~/db", "mydb");
        return "0k";
    }

embbeded 内嵌 mongodb对于只用来开发测试用的项目很有用

1.添加依赖,前提你是springboot 项目哈

  <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
            <scope>runtime</scope>
        </dependency>

2.配置mongo的url

spring.data.mongodb.uri=mongodb://localhost:27017/mydb

这个原因,要检查一下几点

1.pom中配置了jdk版本

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <jdk.version>1.8</jdk.version>
    </properties>
  1. idea的maven配置文件路径是否对

3.如果都对,那么重新reimport一下。