2017-07-25 1 views
1

Hibernateを使用してCRUDリポジトリを作成して、H2データベースにデータを保存したいとします。私のSpring BootアプリでH2がテーブルを作成/更新していません。私のエンティティに何か問題がありますか?

エントリを格納するデータベースを取得できません。現在、私はサンプルエントリを作成してdbを更新する際にこれを達成しようとしています。エントリはログ内で良好に見えますが、テーブルは作成/更新/生成されません。

この場合、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); 
} 

私はlocalhostの下に行くことによって、私のリポジトリをテスト:サンプルエントリがテーブルに挿入されなければならない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] 

データをパラメータにバインドされように見えますが、ゲーム FROM SELECT * H2コンソールでは、私を返します。 ゲームからSELECT *; テーブル「GAME」が見つかりませんでした。 SQL文: SELECT * FROM GAME [42102-193] 42S02/42102(ヘルプ)

私はcreate-dropやcreateなどの他のH2モードを試しましたが、成功しませんでした。私が心配しているのは、正しい行を持つ空のテーブルを作成するためのデータベースを取得することさえできず、エントリの準備ができているということです。

エンティティでもゲームレポジトリの設定でも何かが間違っていると思いますが、このエラーを修正するアイデアはもうありません。

私がここで何を達成したい: 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

*ゲームから何も返さないデータが存在しないことを意味し、テーブル –

+0

は、パラメータがバインドされている場合データが存在しないかがないではありませんか? Hibernateはデータを作成することを拒否しているため、テーブルには存在しません。 – VapeKop

+0

パッケージ構造を共有できますか? –

答えて

関連する問題