Prerequisites

The following are needed for creating the query application

  • caCORESDK generated data service as described in part1
  • JavaEE IDE (Eclipse was used in the following tutorial)

The code generation module of caCORESDK transforms the UML model into artifacts like Java Source, O/R mapping files etc. The people.war that was published to tomcat in Part 1 was one among the artifacts generated. The list of artifacts generated are:

  • Beans - For each object class defined in the UML model, caCORESDK generates a Java bean.The generated bean follows the same package structure as the folder structure in the object model. The generated Java beans are compiled and packaged in a JAR file. The JAR file is named project_name-beans.jar. So our example bean would be people-beans.jar located at caCORE/output/people/package/local-client/lib/people.jar
  • Hibernate files - The following hibernate files are packaged in a separate JAR file after the generation. The JAR file is named project_name-orm.jar (people-orm.jar at caCORE/output/people/package/local-client/lib/people-orm.jar)
    • Hibernate mapping files - For each object defined in the object model, the caCORE SDK generates a Hibernate mapping file (Object Relational mapping file) by reading tag values that maps object and attributes to tables and columns in the data model. In the case of inheritance in the object model, the mapping file is created for the root level class in the inheritance hierarchy. The generated files follow the same package structure as the folder structure in the object model.
    • Hibernate configuration file – A configuration file named hibernate.cfg.xml is generated for Hibernate, which contains a list of all the generated Hibernate mapping files in addition to the database connection settings.
    • EHCache configuration file – A cache configuration file for Hibernate.
  • XSD and XML mapping files
  • Web Services deployment descriptor file

We will mainly work with Beans and Hibernate mapping files to create a simple query for People application. To achieve this do the following:

NOTE: Its recommended to go through the Java Beans and Hibernate Tutorial before creating a simple query.

Create a New Project

  1. In Eclipse, File -> New -> Java Project
  2. Enter project name as PeopleLocalClient, Select Create project from existing source and browse to the directory caCORE/output/people/package/local-client, click Next and Finish

source:exp/sds/exp1/images/sds12/NewProject1.png

The New project with src, build, conf, and lib dirs will be visible in the Package Explorer on the left

Create a New Class

We will first create a class for opening the Hibernate session with the database

  1. Expand project PoepleLocalClient and navigate to src -> (default package)
  2. Select default package -> Right Click -> New -> Class -> Enter name as HibernateUtil -> Finish

source:exp/sds/exp1/images/sds12/NewClass.png

  1. Copy this java code and paste to the above created HibernateUtil.java
  2. Save project Ctrl-S

Now, create the class which forms a query and lists the contents of the single table data service People

  1. Expand project PoepleLocalClient and navigate to src -> (default package)
  2. Select default package -> Right Click -> New -> Class -> Enter name as PeopleClient -> Finish
  3. Copy this java code and paste to the above created PeopleClient.java
  4. Save project Ctrl-S

Modify Hibernate Configuration

The modifications below become necessary due to the fact that caCORESDK created default local client makes use of Aspect Oriented Programming (AOP) to create custom transaction interceptors to open a hibernate session and forward transactions to the database. The ApplicationService tier of the caCORESDK Runtime System is responsible for generating these custom transaction interceptors and makes use of the AOP Springframework API to do so.

  • Chapter 4 from caCORESDK programmer's guide gives a detailed explanation on the Runtime System
  • Of particular significance in Chapter 4 of caCORESDK programmer's guide, is the section titled Client Interface Tier. Sub-section of Client Interface Tier, Technical Challenges, explains the workings of ApplicationService tier with sequence diagrams.
  • This is also a good link in understanding AOP with Hibernate.

To create a custom query client with the Application Service API of caCORESDK Runtime System, the following modifications need not be applied. but to create a custom query client without Application Service API (instead use caCORESDK generated Java Beans and Hibernate mappings), its necessary to explicitly configure hibernate mappings file (hibernate.cfg.xml) to open a session for us.

To open a current session to the database with Hibernate, do the following:

  1. Open a terminal (can also do it Eclipse if you have an XML editor plugin installed) and browse to the caCORESDK generated artifact, local-client. Lets assume that your local caCORE installation is at /home/user/src/caCORE. Then you would've to navigate to the following location
    cd /home/user/src/caCORE/output/people/package/local-client/lib
    
  2. The hibernate mappings, generated by caCORESDK for the People data service will be packaged in the jar file people-orm.jar inside the local-client lib directory.

source:exp/sds/exp1/images/sds12/HibernateConfig.png

  1. Make a temp dir, extract the jar file
    mkdir temp
    
    cp people-orm.jar temp/.
    
    cd temp
    
    jar -xvf people-orm.jar
    
  2. Open hibernate.cfg.xml in a text editor and add the following lines within the session-factory element.
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
    
  3. Save and quit the text editor
  4. Check your above modifications with this hibernate properties file
  5. Package the jar file and copy to lib
    jar -cvf people-orm.jar *
    
    cp people-orm.jar ../lib/.
    
    cd ../
    
    rm -r temp
    

Modify Build Configuration

Add the newly created java query client to ant run target (can edit build.xml via Eclipse/terminal)

  1. In Eclipse, open build.xml (its located in the top-level project directory, PeopleLocalClient)
  2. On line 76 for the ant target run, replace the default TestClient with PeopleClient (the java class name for the query client you created above) i.e., line 76 should look like:
    <java classname="PeopleClient" fork="true">
    
  3. Save build.xml, Ctrl-S

Run the Application

  1. In Eclipse, navigate to the query client class PeopleClient.java. It will be located at src/default package.
  2. Select PeopleClient.java -> Right Click -> Run as -> Java Application

source:exp/sds/exp1/images/sds12/RunJavaApp.png

  1. The console should print the result of the run, i.e., list all the rows of the table Person, consisting of id, first name, and last name from the People data service

source:exp/sds/exp1/images/sds12/PplClientResult.png

Continue to caGrid-enable the data service using Introduce

References