Embedded DB using JPA in Java Desktop Application

Before you start let know about Embedded DB, andJPA(Java Persistence API). Most of all confused about how to use JPA in Desktop Application- its just matter of configuration. For begginers let introduce the basics.

Create a Java Application and add Librarys for JPA. You can find many Vendors supporting JPA.

I will show it  with Eclipselink . You can download JAR or Maven Depentency

 <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.4.0</version>
 </dependency>
            

also add java persistance library.

 <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.3</version>
 </dependency>            

Next step is to create Entity Class – Entity class are Java Classes and is persisted in a Relational databse, each instance of this entity class are the indivigual rows of a table ie, Entity class usually repersented in a table, also we can configure different TableGeneration Strategy.Relational meta data are specified by Annotations in Entity class.


Next how to configure the persistance.xml – specifing the Persistance configuration.
see how to configure it in Embedded Derby .

<dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>10.9.1.0</version>
</dependency>

Persistance.xml

<properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:derby:dbname;create=true"/>
            <property name="javax.persistence.jdbc.password" value="app"/>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
            <property name="javax.persistence.jdbc.user" value="app"/>
            <property name="eclipselink.ddl-generation" value="create-tables"/>
            <property name="eclipselink.platform.class.name"    value="org.eclipse.persistence.platform.database.DerbyPlatform"/>
            <property name="eclipselink.target-database" value="Derby"/>
        </properties>

The properties eclipselink.target-database and eclipselink.platform.class.name are used to specify the target database – this change the Query generation strategy.Because in embedded mode some DB dont support all the features , so we need to specify which DB we are using- to avoid error.

Tags: , , ,
Mar 14, 2013 | Posted by in Java | 0 comments

Leave a Reply


  • two + = 8