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:
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() {
em.close();
emf.close();
}
private void create(String code, String title, short days, String details) {
em.getTransaction().begin();
Course c = new Course();
c.setCode(code);
c.setTitle(title);
c.setDays(days);
c.setDetails(details);
em.persist(c);
em.getTransaction().commit();
}
private void searchTitleByCode (String code) {
Course c = (Course)em.createNamedQuery("Course.findByCode").setParameter("code",code).getSingleResult();
System.out.println(c.getTitle());
}
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main m = new Main();
m.initEntityManager();
m.searchTitleByCode("ABC");
m.closeEntityManager();
}
}
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!
Comments