Monday, February 8, 2010

Oracle ORM - TopLink

Project: Oracle-TopLink
Description: Demo how TopLink can be used as ORM on Oracle 11g
IDE: Eclipse
JARs:
commons-logging-1.1.1.jar
toplink.jar
xmlparserv2.jar
classes12.jar

sessions.xml
<?xml version="1.0" encoding="UTF-8"?><toplink-sessions version="10g Release 3 (10.1.3.0.0)" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt; <session xsi:type="database-session"> <name>TwoTierEmployee</name> <event-listener-classes/> <logging xsi:type="toplink-log"> <log-level>fine</log-level> </logging> <primary-project xsi:type="xml">EmployeeProject.xml</primary-project> <login xsi:type="database-login"> <platform-class>oracle.toplink.platform.database.oracle.Oracle10Platform</platform-class> <user-name>username</user-name> <password>password</password> <sequencing> <default-sequence xsi:type="table-sequence"> <name>Default</name> </default-sequence> </sequencing> <driver-class>oracle.jdbc.OracleDriver</driver-class> <connection-url>jdbc:oracle:thin:@wshi999:1521:orcl</connection-url> </login> </session></toplink-sessions>

ReadAllQuerySimpleExpressionExample.java
/**
* Purpose: To demonstrate some common functions through example.
*/
public class ReadAllQuerySimpleExpressionExample extends Example {
/**
* Example of how all Employee objects can be read using a simple expression.
*/
public void run() {
Expression exp = new ExpressionBuilder().get("firstName").like("J%");
Vector employees = getSession().readAllObjects(Employee.class, exp);
log("\tRead " + employees.size() + " employees with firstName like J%:");
log(employees);
}
/**
* Demonstration code using the Employee Demo classes.
* @param args java.lang.String[]
*/
public static void main(String[] args) {
new ReadAllQuerySimpleExpressionExample().runExample();
}
}


Example.java
public void login() {
databaseSession = (DatabaseSession)SessionManager.getManager().getSession("TwoTierEmployee");
}
public void logout() {
if (databaseSession != null) {
databaseSession.logout();
}
}

public void runExample() {
log("\n\n\n###START: " + this.getClass().getName());
try {
login();
//databaseSession.getIdentityMapAccessor().initializeIdentityMaps();
} catch (DatabaseException loginException) {
System.out.println();
System.out.println("ERROR: Could not log in to database. Make sure your database server is running");
loginException.printStackTrace();
return;
}
try {
run();
} catch (Exception exception) {
exception.printStackTrace();
} finally {
logout();
log("\n###END: " + this.getClass().getName());
}
}

No comments:

Post a Comment