Vap*_*Kop 8 java hibernate jpa h2 spring-boot
我想通过使用Hibernate创建一个CRUD存储库来在H2数据库中保留一些数据.
我无法让数据库存储我的条目.目前,我正在尝试通过制作样本条目来更新数据库.条目在日志中看起来很好,但不创建/更新/生成表.
为什么Hibernate在这种情况下无法创建表?(如果问题出在我的数据结构中)
这是我的Entity,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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是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;
}
}
Run Code Online (Sandbox Code Playgroud)
我在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
Run Code Online (Sandbox Code Playgroud)
存储库配置如下:
import org.springframework.data.repository.CrudRepository;
import java.util.List;
public interface GameRepository extends CrudRepository<Game, Long> {
List<Game> findAllByName(String name);
}
Run Code Online (Sandbox Code Playgroud)
我通过在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";
}
Run Code Online (Sandbox Code Playgroud)
我得到以下日志:
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_.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=?
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, cloudinary_id, height, url, width, first_release_date, name, storyline, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
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, 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.]
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]
Run Code Online (Sandbox Code Playgroud)
看起来数据绑定到参数,但在H2控制台SELECT*FROM GAME返回我:SELECT*FROM 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 /值对象实体对象功能于休眠映射/
另外,我已经试过这一套教程一变: https://springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/ https://开头springframework的.大师/弹簧引导web的应用程序部分-3-弹簧数据JPA /
但到目前为止没有运气.
Dev*_*v M 18
只需转到H2控制台,例如:http:// localhost:9090/h2-console / 并在JDBC URL字段中,键入jdbc:h2:mem:testdb以配置与RAM中testdb数据库的连接.
小智 13
将这一行:spring.jpa.hibernate.ddl-auto = update在您的 application.properties 文件中启动内存数据库和 H2 数据库的基于文件的数据库中的人口数据。希望这对任何人都有帮助。
dav*_*xxx 12
看起来数据绑定到参数,但在H2控制台中,SELECT*FROM GAME不返回任何内容.该表不存在.
您正在使用in-memoryH2 的实例:
spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
Run Code Online (Sandbox Code Playgroud)
在此模式下,您无法从另一个客户端看到启动in-memory数据库的更改内容 .
要查看其他客户端的更改,您必须使用TCP模式.
你有两个解决方案:
存储的数据库文件在哪里?
使用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
替换:
spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
Run Code Online (Sandbox Code Playgroud)
通过:
spring.datasource.url=jdbc:h2:tcp://localhost/~/test
Run Code Online (Sandbox Code Playgroud)
通常,我在JPA实体单元测试期间切换到此模式,当我真的想知道哪些插入数据库时.
从官方文档:
内存数据库
对于某些用例(例如:快速原型设计,测试,高性能操作,只读数据库),可能不需要保留数据或持久更改数据.此数据库支持内存模式,其中数据不会持久存在....
在某些情况下,只需要一个与内存数据库的连接.这意味着要打开的数据库是私有的.在这种情况下,数据库URL为jdbc:h2:mem:在同一虚拟机中打开两个连接意味着打开两个不同的(私有)数据库.
有时需要与同一内存数据库的多个连接.在这种情况下,数据库URL必须包含名称.示例:jdbc:h2:mem:db1.使用此URL访问同一数据库仅适用于同一虚拟机和类装入器环境.
要从另一个进程或另一台计算机访问内存数据库,您需要在创建内存数据库的同一进程中启动TCP服务器.然后,其他进程需要使用数据库URL通过TCP/IP或TLS访问数据库,例如:jdbc:h2:tcp:// localhost/mem:db1.
独立H2控制台的替代方案:使用可从Spring Boot应用程序访问的H2控制台
实际上,H2数据库提供了一个基于浏览器的控制台,Spring Boot可以为您自动配置.满足以下条件时,控制台将自动配置:
- 您正在开发基于servlet的Web应用程序.
- com.h2database:h2在类路径上.
- 您正在使用Spring Boot的开发人员工具.
所以这意味着只能在dev中访问.一般你想要什么.
默认情况下,控制台可用于/h2-console.
设置spring.h2.console.path属性以更改它.
检查您的主类(Spring Boot 应用程序类)是否能够扫描定义的实体。当实体位于与主类不同的包中时,通常会发生这种情况。
| 归档时间: |
|
| 查看次数: |
30380 次 |
| 最近记录: |