Monday, April 29, 2013

JPA Boolean Conversion

In some cases, we might want to store Boolean value in database as "Y/N" or "T/F" instead of "1/0".
This is to increase human readability and better support across different databases because different databases could store Boolean value in different data types.

JPA does support the "Y/N" or "T/F" as Boolean value with the help of @ObjectTypeConverter annotation.
We just need to annotate the entity class with this annotation.
@Entity
@ObjectTypeConverter(...)
public class MyEntity {
    ............................
    ............................
}

Example below shows the converter to convert "Y/N" to Boolean (true/false).
All 5 attributes are occupied, now what we need to do is simply provide values to all the attributes.
@ObjectTypeConverter(
    name = "YNBooleanConverter",
    dataType = String.class,
    objectType = Boolean.class,
    defaultObjectValue = "false",
    conversionValues = {
        @ConversionValue(dataValue = "Y", objectValue = "true"),
        @ConversionValue(dataValue = "N", objectValue = "false") })


Done!!

How to update database schema when JPA entity changes

  4. Update database when entity changed.
  5. Setting up load balance database connection.
  6. Setting Cache Coordination in Eclipselink

It is always changes on database tables' fields during development.
For normal approach, we probably update the database schema first and follow by the entity (both are manual process).

Now, there could be alternative by JPA provider (either Hibernate or EclipseLink).
When the JPA provider detected change(s) on entities, it will update database schema to compliance with the working copy of entities.

What we need to do is only configure to JPA provider in persistence.xml

Saturday, April 27, 2013

How to test a newly setup JPA project

  3. Testing the JPA project 

The purpose of this post is to show how to test a JPA project whether it is properly setup.
Besides, this solution can be used to test a newly created/generated entity as well.

The concept is quite simple, simply create a java class with main(String[] args), in the main method, create database connection and then execute a select statement.
If successfully select records from database, that's mean the JPA project is properly setup.

Steps:
1. Create a new package called test in the project.
2. Create a new class called JpaTest in the test package.
3. Write the following codes in the main(String[] args) to test.
    factory = Persistence.createEntityManagerFactory("HelloJPA");
    EntityManager em = factory.createEntityManager();
    Query q = em.createQuery("select a from Ofuser a");
    List<Ofuser> users = q.getResultList();   

    System.out.println("Number of users: " + users.size());
    if(users.size() > 0) {
        for (Ofuser user : users) {
            System.out.println(user);
        }       
    }

Thursday, April 25, 2013

How to configure JPA project


This post shows how to configure the JPA project about its' persistence properties after entities successfully generated in a JPA project.

Before we start to configure our JPA project, first we must decide which JPA implementation to use.
There are two(2) popular JPA implementations, which are EclipseLink and Hibernate.

Once the preferred JPA implementation installed, then we can start to configure the persistence properties.

Steps:

1. Install JPA implementation library

1.1 EclipseLink

      a) download the required EclipseLink library, and the version that I used in this tutorial is 2.4.1

      b) unzip the archive

      c) copy the javax.persistence_2.0.4.v201112161009.jar and eclipselink.jar to a folder (eg, c:\eclipselink-lib)

PrimeFaces 3.4, 3.5 Compatible Browsers

For all PrimeFaces fans, below is the list of browser and it's version where PrimeFaces supported and tested on
IE 8, 9, 10, FF 13+, Safari 5, 6, Chrome 17+

and here is the list of PrimeFaces 3.3.1 components tested on different version of browsers.

LinkWithin

Related Posts Plugin for WordPress, Blogger...