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.
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.
    name = "YNBooleanConverter",
    dataType = String.class,
    objectType = Boolean.class,
    defaultObjectValue = "false",
    conversionValues = {
        @ConversionValue(dataValue = "Y", objectValue = "true"),
        @ConversionValue(dataValue = "N", objectValue = "false") })


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.

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) {

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.


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.

Sunday, April 21, 2013

How to share project(s) into CVS with Eclipse

<< Back to CVS tutorial home

This article is part of the CVS Team Synchronization with Eclipse.
The objective of this article is to show how to share a new project into CVS with Eclipse.

1. Start Eclipse
2. R-click on the Eclipse that is going to share into CVS.
3. Team > Share Project...
4. Select CVS as Repository Type > Next
Choosing repository type

How to setup CVS client with Eclipse

<< Back to CVS tutorial home

This article shows how to connect to CVS server with Eclipse as the CVS client where CVS client already pre-installed in the Eclipse bundled, we just need to configure and use it.

1. Start Eclipse
2. Window > Open Perspective > Other... > CVS Repository Exploring > OK

3. Click on the Add CVS Repository icon.
CVS Repository dialog

Saturday, April 13, 2013

How to Export and Import project archive in Eclipse

In Eclipse, we can backup our project by exporting a project in workspace to a Archive file.
The Archive file can then import back to our or our teammates workspace.
Following are the steps How to export a project as an archive file, then import the archive to another workspace.

A) Export project as archive
1. R-click on selected project > Export... > General > Archive File > Next
2. Choose archive file location > Finish.
Archive file dialog

Thursday, April 11, 2013

How to create Credit Card Validator with Facelets

This post shows how to build a custom JSF regex validator to validate Credit Card number with Facelets.

There are different types of Credit Cards, the regex used to validate different types of Credit Card are vary with the types of Credit Card.
Visa ^4[0-9]{12}(?:[0-9]{3})?$
Master Card ^5[1-5][0-9]{14}$
American Express ^3[47][0-9]{13}$
Diners Club ^3(?:0[0-5]|[68][0-9])[0-9]{11}$

For more information about Credit Card Regex, please refer here.
so now let's start to build the Credit Card validator.

Tuesday, April 9, 2013

Easy JSF Email validator with Facelets

This post shows how to build a custom JSF regex validator to validate email address with Facelets.
Building email validator with Facelets is very easy if compare to building email validator with JSF component classes.

1. create the validateEmail.xhtml in folder <WebContent>/views/validator
    Regex here could be changed to any desired format.

        pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$" />


Sunday, April 7, 2013

How to setup PrimeFaces project with JBoss Tools

This post show how to quickly setup a PrimeFaces project with the help of JBoss Tools.

1. JBoss Tools Eclipse plugin has been installed.

Related PrimeFaces installation:
  - How to install PrimeFaces Extension
  - How to upgrade to PrimeFaces 4

1. Start Eclipse
2. File > New > Other... > JBoss Tools Web > JSF > JSF Project. Next
3. in the Create JSF Project dialog,
    a) fill in desired Project Name, 
    b) select JSF 2.0 for JSF Environment,
    c) select JSFKickStartWithoutLibs for Template (to quickly start the application).
Create JSF Project dialog

Saturday, April 6, 2013

How to upgrade Primefaces to latest version in Liferay

When we are creating PrimeFaces Portlet in Liferay, the version of PrimeFaces used is actually depends on the Liferay Plugin SDK.

With Liferay Plugin SDK 6.1.1, the version of PrimeFaces used is 3.3
version 3.3 PrimeFaces used

Anyway, we still can upgrade the PrimeFaces version with few simple steps.
This is important to make sure we get the latest update from PrimeFaces.


Related Posts Plugin for WordPress, Blogger...