Tuesday, February 25, 2014

How to programmatically clear the JPA cache

When I used my JPA with EclipseLink as the implementation together with JBoss AS7.1.1.
I'm facing some issues.

One of the issue is the JPA cache not refresh by itself. 

Scenario:
When a new entity inserted into DB with JPA (EclipseLink).
and the new record is verified already inserted into database.

Then immediately I made a SELECT ALL query to this entity.
=> SELECT a FROM ENTITY a

But unfortunately, the newly inserted record is not appeared in the search result. 

to resolve this issue, I used the following line code the clear the JPA cached entities.


// the clear the entire JPA cache
entityManager.getEntityManagerFactory().getCache().evictAll();


// clear only selected entity from the JPA cache
entityManager.getEntityManagerFactory().getCache().evict(ENTITY.class);



Done!!

Saturday, February 22, 2014

Velocity without template(.vm) file

Velocity can be used without a .vm file.
The original template string could be stored in other places, eg. database, or coded in java class.

But there are slightly different to merge the template string with the passed in parameters.

Below is the sample code.
// template string coded in java class or retrieve from database
String templateString = "Using $project $name without vm file.";

// initialize Velocity
VelocityContext context = new VelocityContext();
context.put("name", "Velocity");
context.put("project", "Jakarta");

String output = new StringWriter();

// evaluate the template string and merge them together
Velocity.evaluate(context, output, "log or null", templateString);
System.out.println("output: " + output);

***to use Velocity with .vm file, we use template.merge( context, output )
***to use Velocity without .vm file, we use Velocity.evaluate( context, output, logTag, templateString )


Done!!

Wednesday, February 19, 2014

Case insensitive JPQL

There is no case insensitive comparison in SQL.
Thus, to write a case insensitive SQL, we have to first change the both parameter and value to same case.

The same concept is apply to JPQL as well.

below is the example of case insensitive JPQL.

String caseInsensitiveJPQL = " SELECT a FROM ENTITY a "

caseInsensitiveJPQL += " WHERE UPPER(a.PARAM1) = '" + value1.toUpperCase() "' ";


in the above example, I convert both PARAM1 and value1 to upper case.


Done!!

Sunday, February 9, 2014

How to attach source code for a third party jar file in Eclipse

This is to make the debugging easier in Eclipse by viewing the 3rd party source and also adding break point when in debug mode.

Steps
1. R-click on selected project > Properties

2. Go to source attachment
    Java Build Path > Libraries > choose jar to attach source > Edit

No   in xhtml

In html or jsp, we use " " to make a space,
But in xhtml, " " is not taking any effect.

To make a space in xhtml, should use  

for more information about Character Entities Reference, please go here.


Done!!

Monday, February 3, 2014

How to set up Velocity in Liferay?

To use Velocity in Liferay Portlet, we must fulfill the Velocity dependencies.
As Liferay already make sure of Velocity in layouts and themes, thus we just need to import Velocity's required jar into our customized Portlet.

This post is actually referring to Adding liferay libraries to customized portlet classpath and this is the extension of this post.

required jar files to use Velocity are
commons-lang.jar
oro.jar
velocity.jar


Done!!

LinkWithin

Related Posts Plugin for WordPress, Blogger...