2

I have a couple of unit tests for an application's JPA layer. This JPA layer consists in JPA entities and a service providing the basic API required in order to persist the entities. The unit tets directly use the javax.persistence classes in order to handle the PersistenceManager. Then it tests the persistence API and I can see in the log the SQL statements to create tables and sequences, etc.

The relevant part of the persistence.xml file looks like:

  <persistence-unit name="..." transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
  ...
  <properties>
    <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test"/>
    <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
    <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
    <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    <property name="hibernate.format_sql" value="false"/>
    <property name="hibernate.show_sql" value="true"/>
  </properties>
</persistence-unit>
...

I have downloaded H2 1.4.200, the Windows installer, and I installed it on Windows 10. Now using the H2 console I want to connect to the database and inspect the tables, sequences, etc. that were created automatically by Hibernate.

So, going to http://localhost:8082 I get the following:

enter image description here

But when I try to connect to my database, using the defined JDBC connection string, I get the following:

enter image description here

What am I doing wrong here ?

Many thanks in advance.

Nicolas

  • You'll have to run the H2 console from within your application. What you are now doing is running H2 separately; that separate H2 server cannot look into an embedded H2 database that is inside your application. – Jesper Dec 19 '19 at 14:28
  • See [this](https://stackoverflow.com/questions/438146/what-are-the-possible-values-of-the-hibernate-hbm2ddl-auto-configuration-and-wha). For your setup `hibernate.hbm2ddl.auto=create-drop` database will be dropped after disconnection. – SternK Dec 19 '19 at 14:32
  • @Jesper: What do you mean by "running H2 console from within your application" ? H2 console is a webapp while my use case requires to inspect the SQL object autmatically created by Hibernate in an unit test. –  Dec 19 '19 at 14:34
  • @SternK: I know and this is done on the purpose. When trying to connect to the database the session is active. And anyway, replacing create-drop by create doesn't change anything. –  Dec 19 '19 at 14:37
  • Yes, and you need to have a web server running inside your application that will serve the H2 console. How you do that depends on your application; if it's a Spring Boot application then it's as simple as setting `spring.h2.console.enabled=true` in `application.properties`. – Jesper Dec 19 '19 at 14:38
  • Also, be aware that when you use an in-memory database, the database disappears as soon as your application stops. In-memory means: in the memory that your application is using as long as it's running. – Jesper Dec 19 '19 at 14:41
  • @Jesper: I don't need a web server just to run a couple of unit tests ! And no, that's not a Spring Boot application. Not all the applications are Spring Boot. That's even not an application. –  Dec 19 '19 at 14:41
  • @Jesper: thanks for explaining me what "in-memory" means. –  Dec 19 '19 at 14:42
  • If you want to run a unit test and then after the fact inspect the database, then use a file-based H2 database instead of an in-memory DB. You can connect to that file-based DB with the H2 console later, even when the test has finished. – Jesper Dec 19 '19 at 14:44
  • @Jasper: what about a database based H2 database ? –  Dec 19 '19 at 14:46

1 Answers1

0

Finally, I've replaced H2 with Oracle.