Showing posts with label eclipseLink. Show all posts
Showing posts with label eclipseLink. Show all posts

Saturday, December 31, 2016

Eclipselink Cache Coordination with JBoss and JMS

Agenda:
  1. Creating JPA project and generating entities.
  2. Configuring persistence properties and JPA project
  3. Testing the JPA project
  4. Update database when entity changed.
  5. Setting up load balance database connection.
  6. Setting Cache Coordination in Eclipselink

Cache Coordination enables EclipseLink to synchronize data changes in a distributed environment. for more information about cache coordination in EclipseLink, click here.

Following are the steps to enable Cache Coordination in JBoss AS7 / JBoss EAP6.
1. create JMS topic

2. creaet JMS connection factory

3. add the cache coordination properties into persistence.xml
<property name="eclipselink.cache.coordination.protocol" value="jms" />

<property name="eclipselink.cache.coordination.jms.topic" value="jms/EclipseLinkTopic" />

<property name="eclipselink.cache.coordination.jms.factory" value="jms/EclipseLinkTopicConnectionFactory" />

<property name="eclipselink.cache.coordination.jms.reuse-topic-publisher" value="true" />

<property name="eclipselink.cache.coordination.jms.host" value="remote://HOST:PORT" />

4. in eclipseLink module.xml, add the following into <dependencies />
<module name="javax.jms.api"/>

5. in hornetq module.xml, add the following into <dependencies />
<module name="org.eclipse.persistence"/>



Done!!

Tuesday, August 11, 2015

Synchronize Class List problem when integrating JBoss with eclipseLink

The problem when integrating eclipseLink with JBoss 7.1
the attribute below in persistence.xml is not functioning
<exclude-unlisted-classes>false</exclude-unlisted-classes>

the workaround is to list all the entity class in persistence.xml.
but this is troublesome to developers, every time entities changed. the developers need to re-sync the entity classes again.

To avoid this troublesome step, we need to add additional module into JBoss AS7 to handle this eclipseLink integration.

The official document from JBoss is here.
And steps below are the simplified version of the official document.

1. Download the precompile jar from my drive.
2. Extract and copy the jar to JBoss modules folder
3. add the following line into standalone.xml
    <system-properties>

        <property name="eclipselink.archive.factory"

            value="id.au.ringerc.as7.eclipselinkintegration.JBossArchiveFactoryImpl"/>

    </system-properties>

4. set persistence.xml to exclude classes and remove all synchronized classes.

<exclude-unlisted-classes>false</exclude-unlisted-classes>

p/s: the eclipseLink jar must be 2.4.2 or above


Done!!

Wednesday, August 6, 2014

Soft Delete with JPA and EclipseLink

Assuming I have the following table.

And my entity
@Entity
@Table(name="ITEM")
public class Item {
    private Long itemId;
    private String itemName;
    private Integer itemStatus;

    // getter and setter
}

for AUDITING purpose, hard delete is not encouraged.
thus, when deleting a record, the ITEM_STAUS changes to 2 (deleted).

and I have the following status code
0 - active
1 - inactive
2 - deleted

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!!

Wednesday, January 1, 2014

EclipseLink Static weaving with Ant

<?xml version="1.0"?>
<!DOCTYPE project>

<project name="staticWeaving" default="weaving">  

<target name="define.task"
description="New task definition for EclipseLink static weaving" >
<taskdef
name="weave"
classname="org.eclipse.persistence.tools.weaving.jpa.StaticWeaveAntTask">
<classpath refid="project.class.path" />
</taskdef>
</target>

<target name="weaving" description="perform weaving." depends="define.task">
<weave
source="original-jpa.jar"
target="weaved-jpa.jar"
persistenceinfo="${jpaProjectHome}\classes">
<classpath refid="project.class.path" />
</weave>
</target>

<path id="project.class.path">
   <pathelement path="eclipselink.jar/"/>
  <pathelement path="hibernate-jpa-2.0-api-1.0.1.Final.jar"/>
</path>

</project>

Monday, April 29, 2013

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)

Saturday, December 15, 2012

How to generate entities from tables with Eclipse JPA Tools


The purpose of this post is to show detail steps on how to setting up JPA project and generate entities for the JPA Project

Required Software/Tool
1. Eclipse
2. JDBC jar file for the target database

LinkWithin

Related Posts Plugin for WordPress, Blogger...