In the previous articles I have shown you how to get started with Hibernate 4: create entities, manage relations and inheritance and how to query the stored data.
However every time I used an in-memory database which means that every time you stop the application the inserted data vanishes.
Now it is time to show you how to switch to another database which actually persists the data and you can retrieve it later, and what you have to re-configure to make things work like you expect.
Changing the database
I will use a local H2 database for the examples in this article, however I will show you what you have to change to use popular relational databases with Hibernate.
The only difference in the configurations is the Hibernate driver to use. The dialect is Hibernate’s mapping from its internal keywords (annotations for example) to the database specific commands.
Where to change
The changes will occur in the hibernate.cfg.xml file under src/main/resources/. Here you have to set the fields in the block under the <!– Database connection settings –> comment.
This means the following parameters:
- driver_class to the right database driver
- url to point to the right database
- username for the right user
- password with the right password (for H2 this is mostly not set)
- dialectto let Hibernate know how to map internal keywords to database commands
Dialects tell Hibernate how to map the internal keywords according to the database version connected to because some keywords are new in newer releases of the database — or they simply change with time.
However Hibernate is a tool which has some configuration options which are optional. Most of the time Hibernate knows from the version of the driver which dialect to use. So you can omit the dialect configuration — and Hibernate will still work. If you encounter problems, try passing along the right dialect.
Some drivers of interest
I will list here some drivers for commonly used databases. However this means that you need your driver on the classpath too when running the application.
- MySQL5: hibernate.dialect.MySQL5InnoDBDialect for InnoDB tables, org.hibernate.dialect.MySQL5Dialect for other.
- Oracle: jdbc.OracleDriver for 9i and above
- MS SQL Server: microsoft.sqlserver.jdbc.SQLServerDriver
- PostgreSQL: postgresql.Driver
These are the most common relational databases to use. To have these drivers on the classpath in the example project just import the right JAR as a dependency in the project’s pom.xml.
Updating the example
Now it is time to switch to a persistent storage. In the example I will use H2 because it is already there as a dependency and unlike other databases it needs no installation and configuration so you do not have to do anything either.
To get a persistent H2 store I have simply changed the connection.url from jdbc:h2:mem:db1;… to jdbc:h2:file:./example;…. As you can see the difference is to have file instead of mem to tell H2 it has to look for a file and a path is needed for the file location.
Do not worry if your example.db file does not exists locally: H2 creates the new database file if it is missing.
I changed the application to add each element again into the database when running the application. This means if you run the application three times you will see 3 books in the database — although they will have the same parameters (except the id of course). This should give you the brief idea how to use a persistent store with Hibernate 4 behind your application.
Changing the database is not hard. What you only need to change is the driver and the connection parameters.
You can download chapter specific code from Github Here.<< Hibernate 4 with Query languages Batch execution with Hibernate 4 >>