posts - 88 , comments - 3 , trackbacks - 0

Using Object-relational mapping (ORM) with Salesforce Entities

Object-relational mapping (ORM) technique can bridge your logical business model with your physical storage model.Hibernate is a popular object-relational mapping library for Java, providing a framework for mapping object-oriented domain models to a traditional relational database.

Note: Please see the article Using LINQ with QuickBooks to see the ORM approach using Microsoft .NET.

The CData Salesforce JDBC Driver supports full integration with Hibernate.In this tutorial, we will generate an object-relational mapping of your Salesforce repository using the CData JDBC Driver for Salesforce with Hibernate. A similar approach is applicable to all our JDBC Drivers.

Though Eclipse is the IDE of choice for this article, CData drivers work in any product that supports the Java Runtime Environment. If you prefer NetBeans as your IDE, see our article here. If you prefer IntelliJ IDEA, see here.

STEP 1: Download the Prerequisite Software
You will need three pieces of software to follow this tutorial: Eclipse, the CData Salesforce Driver, and Hibernate. Here are their corresponding download links:

Note: These instructions are intended to work specifically with Hibernate version 4.1.2. Our Salesforce driver works with other versions, but these instructions may vary release to release.

STEP 2: Install the Hibernate Plug-In for Eclipse

  • In Eclipse, navigate to Help -> Install New Software.
  • Click the Add button, enter "Hibernate" in the Name field, and enter "http://download.jboss.org/jbosstools/updates/stable/" in the Location field. Click OK.
  • After all the content loads, expand the All JBoss Tools 3.2.x option, choose Hibernate Tools, and click Next.

STEP 3: Start A New Project

  • Go to the File menu and create new project. Select Java Project as your project type and click Next. Enter a project name and click Finish.
  • Right-click on your project and select Properties. Select Java Build Path and then select the Libraries tab.
  • Using the Add External JARs... command, you need to add several libraries to your project.
    1. From your CData Salesforce download, the rssbus.jdbc.salesforce.jar library.
    2. From your Hibernate download, all of the .jar files in the /dist/lib/required and /dist/lib/provided directories.

STEP 4: Add a Hibernate Configuration File

  • Right-click on our new project and select New -> Other -> Hibernate.
  • Select Hibernate Configuration File (cfg.xml) and hit Next.
  • Select src as the parent folder and click Next.
  • Input the following values for each text field:
    1. Database dialect: SQL Server
    2. Driver class:rssbus.jdbc.salesforce.SalesforceDriver
    3. Connection URL: jdbc:salesforce:User=Your User name; Password=Your password; Access Token=Your Access Token

STEP 5: Connecting Hibernate to Salesforce We must now connect our Hibernate instance to Salesforce.

  • Switch to the Hibernate Configurations perspective: Window -> Open Perspective -> Hibernate.
  • Right-click on the Hibernate Configurations panel and select Add Configuration...
  • Click the Browse... button and choose the project we're working with.
  • For the Configuration file field, click Setup, click Use Existing..., and select the location of the hibernate.cfg.xml file (inside src folder in this demo).
  • Click OK to dismiss the Edit Configuration dialog
  • Expand the Database node of our new created Hibernate Configurations file. If you can see the tables of the database, it means the connection is successfully established.

STEP 6: Reverse Engineer Salesforce

  • Switch back to the Package Explorer.
  • Right-click on our project, select New -> Other, and select Hibernate -> Hibernate Reverse Engineering File (reveng.xml). Click Next.
  • Select src as the parent folder and click Next.
  • In the Console configuration dropdown menu, select the Hibernate configuration file we created in the previous step and click Refresh. Please note that it may take a few minutes to load all the meta data of more than a hundred tables in Salesforce. Alternatively, you can use the cache of metadata. See how to do that at here.
  • Expand the node and choose the tables you want to reverse engineer. Click Finish when you are done.

STEP 7: Configure Hibernate to Run

  • From the menu bar, select Run -> Hibernate Code Generation -> Hibernate Code Generation Configurations.
  • In the Console configuration dropdown menu, select our Hibernate configuration file we created in Step 5. Click Browse... by Output directory and select src.
  • Enable the Reverse engineer from JDBC Connection checkbox. Click the Setup button, click Use Existing..., and select the location of the hibernate.reveng.xml file (inside src folder in this demo).
  • In the Exporters tab, check Domain code (.java) and Hibernate XML Mappings (hbm.xml).
  • Click Run.

One or more plain old Java objects (POJO) are created based on the reverse engineering setting in the previous step.

STEP 8: Insert Mapping Tags For each mapping we have generated, we will need to create a mapping tag in hibernate.cfg.xml to point Hibernate to our mapping resource. Open hibernate.cfg.xml and insert the mapping tags like below the comment as so:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">
CData.SalesForce.SalesForceDriver
</property>
<property name="hibernate.connection.url">
jdbc:salesforce:User=Your User Name; Password=Your password; Access Token= Your Access Token;
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<!--Add the XML Mapping tags here-->
<mapping resource="Account.hbm.xml" />
</session-factory>
</hibernate-configuration>

STEP 9: Et Voila! Now the setup of Hibernate is done. Using the entity you created from last step, you can now perform Select, Update, Delete, and Insert commands. For example:

import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
 
public class App {
public static void main(final String[] args) {
Session session =  new
Configuration().configure().buildSessionFactory().openSession();
String SELECT = "FROM Account";
Query q = session.createQuery(SELECT);
List<Account> resultList = (List<Account>) q.list();
 
for(Account s: resultList){
System.out.println(s.getAccountNumber());
System.out.println(s.getName());
}
}
}

Print | posted on Friday, April 27, 2012 6:11 AM | Filed Under [ salesforce cdata ORM ]

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: