2

I am trying a sample code to run my Spring Boot data project with JPA for an H2 DB.

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)
frido
  • 13,065
  • 5
  • 42
  • 56
user2334926
  • 321
  • 2
  • 7
  • 16

3 Answers3

1

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.

You can also log the SQL statements to your console, by adding spring.jpa.show-sql: true to your application.properties

Community
  • 1
  • 1
gtonic
  • 2,295
  • 1
  • 24
  • 32
  • 1
    No need for extra tools, he already has the h2 console (spring.h2.console.enabled=true, spring.h2.console.path=/console) – GeertPt Oct 11 '16 at 22:04
1

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).

Don't forget to set spring.jpa.generate-ddl to true ( default is false) and make you loadData method transaction (see Spring @Transactional annotation).

Sana
  • 360
  • 3
  • 13
Sébastien Helbert
  • 2,185
  • 13
  • 22
1

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.

The embedded h2 console does not show the correct connection URL by default, it forgets the :mem: part IIRC.

GeertPt
  • 16,398
  • 2
  • 37
  • 61