Skip to content

Latest commit

 

History

History

persistance-example

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Java Persistance Example

The purpose of this example is to show you how to user persistance layer to save data to database.

Persistance layer in Java is very popular. Java is object oriented and this method allow you to persist (save) data to db and again to retrieve it back using objects and object related language - something like LINQ in .NET world.

To make connection from java to db you mainly need this three things:

  • persistance config (database connection information)
  • database driver
  • entity manager implementation

Persistance config

This config is located here:

This file is configuration file for db connection. All settings and database connection properties are set there. It's very important to have this file in right location otherwise will not be found.

Database driver

In this example we use HSQLDB memory embed database and we need correct driver for that. We define it as maven dependency.

<dependency>
	<groupId>org.hsqldb</groupId>
	<artifactId>hsqldb</artifactId>
	<version>2.4.1</version>
</dependency>

If we would use MSSQL or Oracle we would use differend dependency for example this:

<dependency>
    <groupId>oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.3</version>
</dependency>

Entity manager implementation

Persistance is basically interface for storing object in database and we need some implementation. This example use Hibernate as it's engine.

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.3.6.Final</version>
</dependency>

Alternative is for example EcliseLink or some other library