java - H2 not creating/updating table in my Spring Boot app. Something's wrong with my Entity? -
i want keep data in h2 database making crud repository, using hibernate.
i can't database store entries whatsoever. currently, i'm trying achieve during updating db making sample entry. entry looking in logs, table not created/updated/generated.
why hibernate not unable create table in case? (if problem lies in structure of data)
here's entity, game.java class (i've tried without @column annotations, no difference. id not auto-generated, need able enter own id everytime):
@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; } }
and here's cover.java class:
@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; } }
i configured h2 database here, in application.properties file:
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
repository configured this:
import org.springframework.data.repository.crudrepository; import java.util.list; public interface gamerepository extends crudrepository<game, long> { list<game> findallbyname(string name); }
i test repository going under localhost:8080/test, sample entry should inserted table:
@requestmapping("/test") public string savesth(){ gamerepository.save(new game(127, "assassin's creed ii", "the lineage continues new chapter introduces ezio, inheritor of talents , creed of assassins. family murdered rival families, ezio resolves learn ancient art of assassin in order seek revenge. not alone though, allying historical figures such philosopher , writer niccolò machiavelli. able master art of assassin new weapons , instruments created renowned inventor , genius of 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"; }
i following log:
2017-07-25 13:09:58.873 debug 9442 --- [nio-8080-exec-1] org.hibernate.sql : select game0_.id id1_0_0_, game0_.aggregated_rating aggregat2_0_0_, game0_.cloudinary_id cloudina3_0_0_, game0_.height height4_0_0_, game0_.url url5_0_0_, game0_.width width6_0_0_, game0_.first_release_date first_re7_0_0_, game0_.name name8_0_0_, game0_.storyline storylin9_0_0_ game game0_ game0_.id=? hibernate: select game0_.id id1_0_0_, game0_.aggregated_rating aggregat2_0_0_, game0_.cloudinary_id cloudina3_0_0_, game0_.height height4_0_0_, game0_.url url5_0_0_, game0_.width width6_0_0_, game0_.first_release_date first_re7_0_0_, game0_.name name8_0_0_, game0_.storyline storylin9_0_0_ game game0_ game0_.id=? 2017-07-25 13:09:58.875 trace 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.basicbinder : binding parameter [1] [bigint] - [127] 2017-07-25 13:09:58.894 debug 9442 --- [nio-8080-exec-1] org.hibernate.sql : insert game (aggregated_rating, cloudinary_id, height, url, width, first_release_date, name, storyline, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?) hibernate: insert 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] [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] [varchar] - [doczeiofd1ckpapdhqs7] 2017-07-25 13:09:58.896 trace 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.basicbinder : binding parameter [3] [integer] - [1426] 2017-07-25 13:09:58.897 trace 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.basicbinder : binding parameter [4] [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] [integer] - [1000] 2017-07-25 13:09:58.897 trace 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.basicbinder : binding parameter [6] [bigint] - [1258416000000] 2017-07-25 13:09:58.897 trace 9442 --- [nio-8080-exec-1] o.h.type.descriptor.sql.basicbinder : binding parameter [7] [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] [varchar] - [the lineage continues new chapter introduces ezio, inheritor of talents , creed of assassins. family murdered rival families, ezio resolves learn ancient art of assassin in order seek revenge. not alone though, allying historical figures such philosopher , writer niccolò machiavelli. able master art of assassin new weapons , instruments created renowned inventor , genius of 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] [bigint] - [127]
it looks data binded parameters, in h2 console select * game returns me: select * game; table "game" not found; sql statement: select * game [42102-193] 42s02/42102 (help)
i've tried other h2 modes such create-drop or create, no success. worries me that, can't database create empty table correct rows, ready entries.
i think something's wrong either entity or missing gamerepository configuration, have no more ideas fix error.
i want achieve what's here: http://javasampleapproach.com/spring-framework/spring-boot/integrate-h2-database-springboot-spring-jpa-embedded-mode , here: http://www.simplecodestuffs.com/value-object-entity-object-in-hibernate-mapping/
also, i've tried set of tutorials change: 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/
but no luck far.
it looks data binded parameters, in h2 console select * game returns me nothing. table doesn't exist.
you using anin-memory
instance of h2 :
spring.datasource.url=jdbc:h2:mem:test;db_close_delay=-1
in mode, cannot see content of changes client 1 started in-memory
database.
see changes other clients, have use tcp mode.
you have 2 solutions :
- using file persist instance of h2.
where database files stored?
when using database urls jdbc:h2:~/test, database stored in user directory. windows, c:\documents , settings\ or c:\users\. if base directory not set (as in jdbc:h2:./test), database files stored in directory application started (the current working directory). when using h2 console application start menu, /bin. base directory can set in database url. fixed or relative path can used. when using url jdbc:h2:file:./data/sample, database stored in directory data (relative current working directory). directory created automatically if not yet exist. possible use qualified directory name (and windows, drive name). example: jdbc:h2:file:c:/data/test
- keeping use in-memory instance using tcp mode.
replace :
spring.datasource.url=jdbc:h2:mem:test;db_close_delay=-1
by :
spring.datasource.url=jdbc:h2:tcp://localhost/~/test
generally, switch mode during jpa entity unit testing when want know inserted in database.
from the official documentation :
in-memory databases
for use cases (for example: rapid prototyping, testing, high performance operations, read-only databases), may not required persist data, or persist changes data. database supports in-memory mode, data not persisted. ...
in cases, 1 connection in-memory database required. means database opened private. in case, database url jdbc:h2:mem: opening 2 connections within same virtual machine means opening 2 different (private) databases.
sometimes multiple connections same in-memory database required. in case, database url must include name. example: jdbc:h2:mem:db1. accessing same database using url works within same virtual machine , class loader environment.
to access in-memory database process or computer, need start tcp server in same process in-memory database created. other processes need access database on tcp/ip or tls, using database url such as: jdbc:h2:tcp://localhost/mem:db1.
Comments
Post a Comment