2017-07-25 111 views
1

我想通過使用Hibernate製作一個CRUD存儲庫來保留H2數據庫中的一些數據。H2在我的Spring Boot應用程序中沒有創建/更新表格。我的實體出了什麼問題?

我無法獲得數據庫來存儲我的任何條目。目前,我正在嘗試通過製作示例條目來更新數據庫。條目在日誌中看起來不錯,但表格不會被創建/更新/生成。

爲什麼在這種情況下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; 
    } 

} 

我配置了H2數據庫在這裏,在application.properties中文件:

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); 
} 

我通過在本地主機要考驗我的資料庫:8080 /測試,其中一個樣本條目應被插入到表:

@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_.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] 

它看起來像數據綁定到參數,但在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/value-object-entity-object-in-hibernate-mapping/

而且,我已經嘗試了改變本套教程: https://springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/ https://springframework.guru/spring-boot-web-application-part-3-spring-data-jpa/

但至今沒有運氣。

+0

select * from遊戲返回沒有意義沒有數據,沒有表 –

+0

如果參數綁定,怎麼沒有數據?該表並不存在,因爲Hibernate拒絕創建它,因爲數據中的某些內容不正確。 – VapeKop

+0

你能分享你的包裝結構嗎? –

回答

0

它看起來像數據綁定到參數,但在H2控制檯SELECT * 從遊戲返回我什麼都沒有。該表不存在。

您正在使用H2的in-memory實例:

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

在這種模式下,你不能看到的內容發生了變化,從另一個客戶端,其中之一是啓動了in-memory數據庫。
要查看來自其他客戶端的更改,必須使用TCP模式。

有兩種解決方法:

  • 使用文件堅持H2的實例。

數據庫文件存儲在哪裏?

使用數據庫URL(如jdbc:h2:〜/ test)時,數據庫在用戶目錄中存儲爲 。對於Windows,這通常是C:\ Documents and Settings \或C:\ Users \。如果未設置基礎目錄 (如在jdbc:h2:./ test中),則數據庫文件將存儲在應用程序啓動的 目錄中(當前工作的 目錄)。當從開始菜單使用H2 Console應用程序時,這是/ bin。基礎目錄可以在數據庫URL中設置爲 。可以使用固定或相對路徑。當使用 URL jdbc:h2:file:./ data/sample時,數據庫存儲在 目錄數據中(相對於當前工作目錄)。如果目錄不存在,則自動創建 目錄。也可以使用 使用完全限定的目錄名稱(以及用於 Windows,驅動器名稱)。例如:JDBC:H 2:文件:C:/數據/檢驗

  • 保持使用一個內存中的實例,但是使用TCP模式。

替換:

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

由:

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

一般來說,我JPA實體單元測試過程中切換到該模式時,我真的很想知道這是插入到數據庫中。

the official documentation

內存數據庫

對於某些使用情況下(例如:快速原型,測試,高 演奏操作,只讀數據庫),它可不要求 堅持數據,或堅持更改數據。此數據庫 支持內存模式,其中數據未保留。 ...

在某些情況下,只有一個到內存數據庫的連接是 必需的。這意味着要打開的數據庫是私有的。在這個 的情況下,數據庫URL是jdbc:h2:mem:在 內打開兩個連接,同一虛擬機意味着打開兩個不同的(私有) 數據庫。

有時需要對同一內存數據庫的多個連接是 。在這種情況下,數據庫URL必須包含一個名稱。示例: jdbc:h2:mem:db1。使用此URL訪問相同的數據庫僅在相同的虛擬機和類加載器環境中運行 。

從另一個進程訪問內存數據庫或從另一個 電腦,你需要在同一個進程來啓動一個TCP服務器作爲 內存數據庫的創建。其他進程則需要使用數據庫URL(例如: jdbc:h2:tcp:// localhost/mem:db1)通過TCP/IP或TLS訪問數據庫 。

+0

2017-07-25 13:58:41.311錯誤10690 - - [restartedMain] oatomcat.jdbc.pool.ConnectionPool:無法創建池的初始連接。 org.h2.jdbc.JdbcSQLException:連接中斷:「java.net.ConnectException:連接被拒絕(連接被拒絕):localhost」[90067-193] – VapeKop

+0

/〜/ test是因爲我正在測試localhost下的db :8080 /測試? – VapeKop

+0

不,它不。我認爲你有一個已經打開的連接。嘗試殺死正在執行的Spring Boot進程。然後再試一次。 – davidxxx

相關問題