H2不在我的Spring Boot应用程序中创建/更新表我的实体出了什么问题?

如何解决H2不在我的Spring Boot应用程序中创建/更新表我的实体出了什么问题?

看起来数据已绑定到参数,但是在H2控制台中SELECT * FROM GAME不返回任何内容。该表不存在。

您正在使用in-memoryH2 的实例:

spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1

在这种模式下,您无法从启动in-memory数据库的另一个客户端看到更改的内容 。 要查看其他客户端的更改,您必须使用TCP模式。

您有两种解决方案:

  • 使用文件保留H2实例。

使用jdbc:h2:〜/ test之类的数据库URL时,数据库存储在用户目录中。对于Windows,通常为C:\ Documents and Settings \或C:\ Users \。如果未设置基本目录(如在jdbc:h2:./ test中),则数据库文件存储在启动应用程序的目录(当前工作目录)中。从开始菜单使用H2控制台应用程序时,该目录为/ bin。可以在数据库URL中设置基本目录。可以使用固定或相对路径。使用URL jdbc:h2:file:./ data / sample时,数据库存储在目录数据中(相对于当前工作目录)。如果该目录尚不存在,则会自动创建。也可以使用完全限定的目录名(对于Windows,则为驱动器名)。示例:jdbc:h2:file:C:/ data / test

  • 继续使用内存中的实例,但使用TCP模式。

更换:

spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1

创建人:

spring.datasource.url=jdbc:h2:tcp://localhost/~/test

通常,在我真的想知道数据库中插入了哪个时,我在JPA实体单元测试期间切换到此模式。

官方文档中

对于某些用例(例如:快速原型制作,测试,高性能操作,只读数据库),可能不需要保留数据或保留对数据的更改。该数据库支持内存模式,在该模式下数据不会持久保存。…

在某些情况下,仅需要与内存数据库的一个连接。这意味着要打开的数据库是私有的。在这种情况下,数据库URL为jdbc:h2:mem:在同一虚拟机中打开两个连接意味着打开两个不同的(专用)数据库。

有时需要到同一内存数据库的多个连接。在这种情况下,数据库URL必须包含一个名称。示例:jdbc:h2:mem:db1。使用此URL访问相同的数据库仅在相同的虚拟机和类加载器环境中有效。


实际上,H2数据库提供了一个基于浏览器的控制台,Spring Boot可以为您自动配置该控制台。满足以下条件时,将自动配置控制台:

  • 您正在开发基于servlet的Web应用程序。
  • com.h2database:h2在类路径上。
  • 您正在使用Spring Boot的开发人员工具。

因此,这意味着只能在dev中访问。通常您想要什么。

默认情况下,控制台位于/h2-console。 设置spring.h2.console.path属性以更改该设置。

解决方法

我想通过使用Hibernate创建一个CRUD存储库,在H2数据库中保留一些数据。

我无法获得数据库来存储我的条目。目前,我正在尝试通过输入示例来实现在更新数据库期间实现这一目标。日志中的条目看起来不错,但是未创建/更新/生成表。

在这种情况下,为什么Hibernate不能创建表? (如果问题出在我的数据结构中)

这是我的实体 Game.java 类(我尝试了@Column注释,没有区别 。ID 不是自动生成的,我需要每次都能输入自己的ID):

@Entity
@Table(name = "GAME")
public class Game {

    @Id
    @Column (name = "ID")
    private long id;

    @Column (name = "NAME")
    private String name;

    @Column(name = "STORYLINE",length = 4000)
    private String storyline;

    @Column(name = "AGGREGATED_RATING")
    @JsonProperty("aggregated_rating")
    private double aggregatedRating;

    @Column(name = "FIRST_RELEASE_DATE")
    @JsonProperty("first_release_date")
    private long firstReleaseDate;

    @Embedded
    private Cover cover;

    public Game(){

    }

    public Game(long id,String name,String storyline,double aggregatedRating,long firstReleaseDate,Cover cover) {
        this.id = id;
        this.name = name;
        this.storyline = storyline;
        this.aggregatedRating = aggregatedRating;
        this.firstReleaseDate = firstReleaseDate;
        this.cover = cover;
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getStoryline() {
        return storyline;
    }

    public double getAggregatedRating() {
        return aggregatedRating;
    }

    public long getFirstReleaseDate() {
        return firstReleaseDate;
    }

    public Cover getCover() {
        return cover;
    }


}

这是 Cover.java 类:

@Embeddable
public class Cover {

    @Column (name = "URL")
    private String url;
    @JsonProperty("cloudinary_id")
    @Column (name = "CLOUDINARY_ID")
    private String cloudinaryId;
    @Column (name = "WIDTH")
    private Integer width;
    @Column (name = "HEIGHT")
    private Integer height;

    public Cover(){
    }

    public Cover(String url,String cloudinaryId,Integer width,Integer height) {
        this.url = url;
        this.cloudinaryId = cloudinaryId;
        this.width = width;
        this.height = height;
}

    public String getUrl() {
        return url;
    }

    public String getCloudinaryId() {
        return cloudinaryId;
    }

    public Integer getWidth() {
        return width;
    }

    public Integer getHeight() {
        return height;
    }

}

我在 application.properties 文件中配置了H2数据库:

spring.h2.console.enabled=true
spring.h2.console.path=/h2_console
spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.hibernate.ddl-auto = update
spring.jpa.show-sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

存储库 的配置如下:

import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface GameRepository extends CrudRepository<Game,Long> {
    List<Game> findAllByName(String name);
}

我通过在localhost:8080 / test下测试我的存储库,该示例条目应插入表中:

@RequestMapping("/test")
public String saveSth(){
    gameRepository.save(new Game(127,"Assassin's Creed II","The lineage continues as this new chapter introduces Ezio,inheritor of the talents and creed of the Assassins. His family murdered by rival families,Ezio resolves to learn the ancient art of the Assassin in order to seek revenge. He will not do so alone though,allying with historical figures such as philosopher and writer Niccolò Machiavelli. You will also be able to master the art of the assassin with all new weapons and instruments created by the renowned inventor and genius of the Renaissance,Leonardo Da Vinci himself.",90.25,1258416000000L,new Cover("//images.igdb.com/igdb/image/upload/t_thumb/doczeiofd1ckpapdhqs7.jpg","doczeiofd1ckpapdhqs7",1000,1426)));
    return "success";
}

我得到以下日志:

2017-07-25 13:09:58.873 DEBUG 9442 --- [nio-8080-exec-1] org.hibernate.SQL                        : select game0_.id as id1_0_0_,game0_.aggregated_rating as aggregat2_0_0_,game0_.cloudinary_id as cloudina3_0_0_,game0_.height as height4_0_0_,game0_.url as url5_0_0_,game0_.width as width6_0_0_,game0_.first_release_date as first_re7_0_0_,game0_.name as name8_0_0_,game0_.storyline as storylin9_0_0_ from game game0_ where game0_.id=?
Hibernate: select game0_.id as id1_0_0_,game0_.storyline as storylin9_0_0_ from game game0_ where game0_.id=?
2017-07-25 13:09:58.875 TRACE 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [127]
2017-07-25 13:09:58.894 DEBUG 9442 --- [nio-8080-exec-1] org.hibernate.SQL                        : insert into game (aggregated_rating,cloudinary_id,height,url,width,first_release_date,name,storyline,id) values (?,?,?)
Hibernate: insert into game (aggregated_rating,?)
2017-07-25 13:09:58.895 TRACE 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [DOUBLE] - [90.25]
2017-07-25 13:09:58.896 TRACE 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [doczeiofd1ckpapdhqs7]
2017-07-25 13:09:58.896 TRACE 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [INTEGER] - [1426]
2017-07-25 13:09:58.897 TRACE 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARCHAR] - [//images.igdb.com/igdb/image/upload/t_thumb/doczeiofd1ckpapdhqs7.jpg]
2017-07-25 13:09:58.897 TRACE 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [INTEGER] - [1000]
2017-07-25 13:09:58.897 TRACE 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [6] as [BIGINT] - [1258416000000]
2017-07-25 13:09:58.897 TRACE 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [7] as [VARCHAR] - [Assassin's Creed II]
2017-07-25 13:09:58.897 TRACE 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [8] as [VARCHAR] - [The lineage continues as this new chapter introduces Ezio,Leonardo Da Vinci himself.]
2017-07-25 13:09:58.897 TRACE 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [9] as [BIGINT] - [127]

看起来数据已绑定到参数,但是在H2控制台中 SELECT * FROM GAME 返回了我:SELECT * FROM GAME; 找不到表格“
GAME”;SQL语句:SELECT * FROM GAME [42102-193] 42S02 / 42102(帮助)

我尝试了其他H2模式,例如create-drop或create,但没有成功。令我担心的是,我什至无法让数据库创建具有正确行的空表,准备输入。

我认为我的实体有问题,或者GameRepository配置中有问题,但是我没有更多的想法来解决此错误。

我想实现以下功能:http :
//javasampleapproach.com/spring-framework/spring-boot/integrate-h2-database-
springboot-spring-jpa-embedded-mode 此处:http : //www.simplecodestuffs.com / value-object-entity-
object-hibernate-mapping /

另外,我已经试过这一套教程一变: https://springframework.guru/using-the-h2-database-console-
in-spring-boot-with-spring-security/
https://开头springframework的。大师/ spring-boot-web-application-part-3-spring-
data-jpa /

但到目前为止没有运气。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-