Skip to main content

Java Persistance

Recently I explored a bit how one can easily make use of the new Java Persistance API. The example below shows how one can very quickly create and search records from a Derby database table called "Course".

To keep things simple and at the same time examine exactly the code required for such a task I created a simple console application from my netbeans 5.5.1 IDE. The new persistance API is much lighter than the previous version and was taken out from the Enterprise API thus making it possible to use it from simple java applications. Once I created my console app I added a new "Entity class from Database" to the project. A dialog is displayed and the connection chosen must be previously defined from the Runtime->Databases section in Netbeans. In the dialog I as well chose the option to "Create Persistance Unit" using the Toplink Library and named it "PUnit".

Once one goes through these simple steps automatically Netbeans creates an Entity class named "Course" with getter and setter methods for each individual table field.

Then I went into my Main class generated initially for my console application and modified as follows:

package javaapplication4;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

* @author Owner
public class Main {

private EntityManagerFactory emf;
private EntityManager em;
private String PERSISTENCE_UNIT_NAME = "PUnit";

private void initEntityManager() {
emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
em = emf.createEntityManager();

private void closeEntityManager() {

private void create(String code, String title, short days, String details) {
Course c = new Course();


private void searchTitleByCode (String code) {
Course c = (Course)em.createNamedQuery("Course.findByCode").setParameter("code",code).getSingleResult();

/** Creates a new instance of Main */
public Main() {

* @param args the command line arguments
public static void main(String[] args) {
Main m = new Main();


The simple code above shows how one can create and search for a Course record. For searching I used the "Course.findByCode" named query which was created automatically in my Entity Class (by browsing in the Entity Class one can easily identify this query together with other generated named queries). To run the application you have to as well add the Java DB Library with the project libraries. Result = Very neat approach to DB persistance!


CresceNet said…
Gostei muito desse post e seu blog é muito interessante, vou passar por aqui sempre =) Depois dá uma passada lá no meu site, que é sobre o CresceNet, espero que goste. O endereço dele é . Um abraço.
Notebooks said…
Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Notebook, I hope you enjoy. The address is A hug.

Popular posts from this blog

Interfacing C# .Net and R - Integrating the best of both worlds

In specific software areas like in quantitative finance or else in other mathematical domains, data centric programming typically requires a good balance between three requirements - (1) a solid platform with rich mathematical/statistical functionality (2) having an easy to use, contemporary, programming environment which permits easy and flexible front end code development and (3) an easy to use interface between the two environments.
In this artcile I am going to explain how such a balance can be attained by using two of the best products in their specific worlds - using the rich R library as the mathematical/statistical component but then interfacing with C# for the front end application design. As an interfacing option I banked on using R (D)COM which provides an easy to use interfacing method which keeps you away from spending hours identifying interfacing problems.
The software required for this tutorial is the following: 1. R software (download from here) 2. R (D)COM Interface (dow…

Interfacing C# .Net and R - Integrating the best of both worlds (Part 3) - Trader Desk Example

In this third and final part of the series (Part 1, Part 2), I am going to continue shaping the examples in the previous posts by quickly building a small application - a simple Trading Desk application. In the example I will use the same C# and R interface method together with a specific Quantitative Financial Modelling R library called Quantmod.
Example Objective In the example the objective is to build a C# Web Form which acts as the main application controller that captures various user option parameters with the main functions being:
1. To automatically kick off an R routine to download data from Yahoo Finance. 2. To select specific dates of interest 3. To add or udpate different charts 4. To add different chart indicators
5. To calculate Period Return Statistics
Most of these functions are provided through the set of R functions exposed by the Quantmod R library.
Additions under the hood
Below I am showing some of the salient additions done to the code over and above the code in the pr…

Interfacing C# .Net and R - Integrating the best of both worlds (Part 2)

This post is a continuation from the previous post (Part I) focusing on interfacing C# with R using the R (D)COM. In this post I am going to enhance my previous exercise by creating a Facade .Net Class which facilitates access to specific functions in R.
Creating the R Facade Class
Creating a Facade Class (or a set of .Net classes) which acts as a .Net wrapper to R functions greatly facilitate the use of R functions and their integration within the .Net programming environment. Below I am showing an excerpt from the class RFacade that I have created in this example.
using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using StatConnectorCommonLib;
using System.Runtime.InteropServices;

namespace R

class RFacade : IDisposable

     private StatConnector rconn;
     private bool disposed = false;

     public RFacade()
          rconn = new STATCONNECTORSRVLib.StatConnector();

     public void D…