JPA: Part 2

This is continued from JPA: part 1.

Other Annotations:

  • @Table(name = “TABLENAME”)
    • Used before class name to explicitly specify table name.
  • @Column(name = “COLUMN_NAME”)
    • Used before instance variables to explicitly set column names.


@Table(name = "EMP")
public class Employee {
    @Column(name = "EMP_ID")
    private int id;

    @Column(name = "EMP_NAME")
    private String name;

    //getters and setters

this maps to
Table: EMP


JPA automatically maps ‘int’ to ‘NUMBER’ and ‘String’ to ‘CHAR’ or ‘VARCHAR’. But it cannot automatically convert Date or Time datatypes. So we need to specify the Temporal type to be used. (Temporal = related to time)

Other Annotations:

  • @Temporal(TemporalType.TYPE)
    • TYPE can be DATE, TIME or TIMESTAMP. Used before ‘Date’ or ‘Calendar’ datatypes. Oracle only has DATE datatype. It doesn’t have TIME or TIMESTAMP.
  • @Transient
    • Used before those instance variables that need not be persisted.


public class Student {
    private int regNo;
    private String name;

    private Calendar dob; //'dob' column od database is of 'DATE' datatype

    private int mark1;
    private int mark2;
    private int mark3;

    private float averageMark; //Not stored in database

    //getters and setters

Configuring JPA

Configure ‘persistence unit’ in ‘persistence.xml‘:
Persistence unit name is specified as:

<persistence-unit name="EmployeeService" transaction-type="RESOURCE_LOCAL">

Here EmployeeService is the persistence unit.

We should then specify all Entity classes that need to be persisted in <class> tags


We then configure the db related stuff like URL,username,password etc.

    <property name="toplink.jdbc.url" value="jdbc:oracle:thin:@"/>
    <property name="toplink.jdbc.user" value="username"/>
    <property name="toplink.jdbc.password" value="password"/>
    <property name="toplink.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="toplink.ddl-generation" value="create-tables"/>

This is for TopLink implementation of JPA to connect to Oracle. It might be different for other implementations.

Persisting an Entity using JPA

  1. Creating an instance of ‘EntityManagerFactory
    • Persistence.createEntityManagerFactory(persistence-unit_name_defined_in_xml_file)
  2. Obtaining an instanceof ‘EntityManager
  3. Persist the Entity
    • Begin EntityTransaction
    • Invoke persist()
    • Commit the transaction


EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmployeeService");
EntityManager em = emf.createEntityManager();
EntityTransaction et = em.getTransaction();
if(et==null) {
    //ERROR. Cannot obtain transaction
et.begin(); //begin transaction
    Employee emp = new Employee();
    em.persist(emp); //persist the data
et.commit(); //ending transaction

When we call em.persist(), data is sent to the database and it is stored in DB Buffer. It is not yet updated in the table. When we call em.commit(), the data gets updated in the table.

Finding an Entity

To find an entity, we need not begin a transaction. We can find it using EntityManager.
EntityManager.find(EntityClassName.class, PrimaryKey)


Employee empl = em.find(Employee.class, 101);

Here 101 is the empId(primary key) to be searched.

If table doesn’t exist or record is not present, it returns null.

Updating an Entity

Once we find an entity, the entity object in the heap memory gets a connection to the table. So,if we modify the object, the table also gets updated.
An object which is connected to a table(relation) is called ‘Managed Object‘. If we update the Managed Object, the table also gets updated.


EntityTransaction et = em.getTransaction();
    Employee empl = em.find(Employee.class, 101);
    //empl is now a Managed Object. We can now modify it
    if(empl!=null) {

Removing an Entity

This is similar to update. Once we find the object we call EntityManager.remove(entityObject)


EntityTransaction et = em.getTransaction();
    Employee empl = em.find(Employee.class, 101);
    //empl is now a Managed Object. We can now modify it
    if(empl!=null) {

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s