简体   繁体   English

带有 H2 数据库的 Spring Boot jpa

[英]Spring boot jpa with H2 database

I am trying a sample code to run my Spring Boot data project with JPA for an H2 DB.我正在尝试使用示例代码为 H2 DB 使用 JPA 运行我的 Spring Boot 数据项目。

The code is below, application is running properly, but I don't see any table getting created, I don't see any error in the server console as well.代码如下,应用程序运行正常,但我没有看到任何表被创建,我也没有在服务器控制台中看到任何错误。 I checked the logs, I don't see any queries getting created an fired.我检查了日志,我没有看到任何查询被创建和解雇。 Am I am doing anything wrong?我做错了什么吗?

Domain class:域类:

@Entity
public class Account {

    @Id
    @GeneratedValue
    private Integer accountId;

    private String name;

    private String accType;

    public Integer getAccountId() {
        return accountId;
    }

    public String getName() {
        return name;
    }

    public String getAccType() {
        return accType;
    }

    public Account(Integer accountId, String name, String accType) {
        super();
        this.accountId = accountId;
        this.name = name;
        this.accType = accType;
    }

    public Account(String name, String accType) {
        super();
        this.name = name;
        this.accType = accType;
    }
    public Account() {

    }

Data Loader class:数据加载器类:

@Component
public class AccountLoaded {

    private AccountRepository accountRepository;

    public AccountLoaded(AccountRepository accountRepository) {
        this.accountRepository = accountRepository;
    }

    @PostConstruct
    private void loadData() {
        Account account = new Account(1,"Madhu","Savings");
        accountRepository.save(account);
        System.out.println("Loaded Account " + account.toString());
    }

}

Repository class:存储库类:

@Repository
public interface AccountRepository extends CrudRepository<Account, Integer> {

}

application.properties:应用程序属性:

spring.datasource.jdbc-url=jdbc:h2:mem:test
spring.h2.console.enabled=true
spring.h2.console.path=/console
spring.datasource.platform=h2

My Logs in the server console:我在服务器控制台中的日志:

2016-10-11 12:38:26.202  INFO 20020 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2016-10-11 12:38:26.217  INFO 20020 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2016-10-11 12:38:26.285  INFO 20020 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.11.Final}
2016-10-11 12:38:26.287  INFO 20020 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2016-10-11 12:38:26.288  INFO 20020 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2016-10-11 12:38:26.327  INFO 20020 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2016-10-11 12:38:26.554  INFO 20020 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2016-10-11 12:38:26.995  INFO 20020 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
2016-10-11 12:38:27.004  INFO 20020 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
2016-10-11 12:38:27.044  INFO 20020 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
Loaded Account Account [accountId=1, name=Madhu, accType=Savings]
2016-10-11 12:38:27.587  INFO 20020 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@b472aa: startup date [Tue Oct 11 12:38:23 CDT 2016]; root of context hierarchy
2016-10-11 12:38:27.663  INFO 20020 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-10-11 12:38:27.664  INFO 20020 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-10-11 12:38:27.696  INFO 20020 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-10-11 12:38:27.696  INFO 20020 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-10-11 12:38:27.732  INFO 20020 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-10-11 12:38:27.959  INFO 20020 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-10-11 12:38:28.011  INFO 20020 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-10-11 12:38:28.015  INFO 20020 --- [           main] com.example.SpringdataApplication        : Started SpringdataApplication in 4.898 seconds (JVM running for 5.255)

Change spring.datasource.jdbc-url=jdbc:h2:mem:test to spring.datasource.jdbc-url=jdbc:h2:file:test (test is the name of the db file, could also have a path with the name) and use one of these tools to view the DB.spring.datasource.jdbc-url=jdbc:h2:mem:test更改为spring.datasource.jdbc-url=jdbc:h2:file:test (test 是 db 文件的名称,也可以有一个带有名称的路径) 并使用这些工具之一来查看数据库。

You can also log the SQL statements to your console, by adding spring.jpa.show-sql: true to your application.properties您还可以通过将spring.jpa.show-sql: true添加到 application.properties 来将 SQL 语句记录到您的控制台

Even if all is done in memory you should see SQL logs when spring.jpa.show-sql is set to true (in application.properties file).即使所有操作都在内存中完成,当spring.jpa.show-sql设置为true (在application.properties文件中)时,您应该会看到 SQL 日志。

Don't forget to set spring.jpa.generate-ddl to true ( default is false) and make you loadData method transaction (see Spring @Transactional annotation).不要忘记将spring.jpa.generate-ddl设置为true (默认为 false)并使您 loadData 方法事务(请参阅 Spring @Transactional注释)。

If you go to the embedded h2 console ( http://localhost:8080/console ), and there type the correct connection URL (jdbc:h2:mem:test), you should see things.如果您转到嵌入式 h2 控制台 ( http://localhost:8080/console ),并在那里输入正确的连接 URL (jdbc:h2:mem:test),您应该会看到一些东西。

The embedded h2 console does not show the correct connection URL by default, it forgets the :mem: part IIRC.默认情况下,嵌入式 h2 控制台不显示正确的连接 URL,它忘记了 :mem: 部分 IIRC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM