Insert and Query data using JPA

This a very simple  yet complete example on how you insert and read data from a simple table using JPA.

First if all you will need a table, below is the Oracle table I will use in our example:


CREATE TABLE TABLE_EXAMPLE
 (
ID NUMBER(10,0) NOT NULL,
NOME VARCHAR2(20 BYTE) NOT NULL,
BIRTH DATE NOT NULL,
DECIMAL_VALUE NUMBER(10,2) NOT NULL
 );

Next you will need an Entity class, like the following:

package com.wordpress.vrsbrazil.jpa;

import java.io.Serializable;
import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;

@Entity
@Table(name="TABLE_EXAMPLE") //Relation with the table name
public class ExampleObject implements Serializable{

private static final long serialVersionUID = 1L;
 @Id
 @Column(name="ID") //Relation with the column name
 private int id;
 @NotNull
 @Column(name="NOME") //Relation with the column name
 private String nome;
 @NotNull
 @Past
 @Column(name="BIRTH")//Relation with the column name
 private Date birthday;
 @NotNull
 @Column(name="DECIMAL_VALUE")//Relation with the column name
 private double randomDecimalValue;

 public ExampleObject() {
 }

public ExampleObject(int id, String nome, Date birthday,
 double randomDecimalValue) {
 super();
 this.id = id;
 this.nome = nome;
 this.birthday = birthday;
 this.randomDecimalValue = randomDecimalValue;
 }

public int getId() {
 return id;
 }

public void setId(int id) {
 this.id = id;
 }

public String getNome() {
 return nome;
 }

public void setNome(String nome) {
 this.nome = nome;
 }

public Date getBirthday() {
 return birthday;
 }

public void setBirthday(Date birthday) {
 this.birthday = birthday;
 }

public double getRandomDecimalValue() {
 return randomDecimalValue;
 }

public void setRandomDecimalValue(double randomDecimalValue) {
 this.randomDecimalValue = randomDecimalValue;
 }

 @Override
 public String toString(){

 return this.getNome() + " " + this.getBirthday();

 }

}

That is it for coding on the JPA layer, all you need now is the persistence.xml file, a config file for your connection, mine is:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
 <persistence-unit name="VRSExample"> <!-- You will need this name to instantiate your call -->
 <description>My persistence unit</description>
 <jar-file>vrsbrazilJPA.jar</jar-file>
 <class>com.wordpress.vrsbrazil.jpa.ExampleObject</class>

<!-- I only have one class here since I only made one table with no relations, you can have as many class tags as you like -->

<properties>

<!-- Connection properties -->
 <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/>
 <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
 <property name="javax.persistence.jdbc.user" value="USR"/>
 <property name="javax.persistence.jdbc.password" value="PWD"/>
 </properties>
 </persistence-unit>
</persistence>

OK, now you will need to put those files into a jar, my jar name is vrsbrazilJPA.jar, and the files will be organized as following:

[-]vrsbrazilJPA.jar
|-com/wordpress/vrsbrazil/jpa/ExampleObject.class
|-META-INF/persistence.xml

We are all set here, below is a call to this class inserting a register and then qrerying it withou commiting anything:

package com.wordpress.vrsbrazil.call;

import java.text.ParseException;
import java.text.SimpleDateFormat;

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

import com.wordpress.vrsbrazil.jpa.ExampleObject;

public class JPAExapleCaller {

@PersistenceContext
 private EntityManager em;
 private EntityManagerFactory emf;

 private SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");

 public JPAExapleCaller() {
 }

 public String callExample() throws ParseException{

 emf = Persistence.createEntityManagerFactory("VRSExample");

 em = emf.createEntityManager();

 ExampleObject eo = new ExampleObject(1, "Name", new java.sql.Date(sdf.parse("1980-01-01").getTime()), 10.2d);

 em.persist(eo);

 ExampleObject eonew = em.find(ExampleObject.class, 1);

 return eonew.toString();

 }

}

The sourcecode with a jsp calling the class above is here for you.

Thank you.

Advertisements

One thought on “Insert and Query data using JPA”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s