Description: Put TopLink work together with Spring Framework
IDE: Eclipse
JARs:
class12.jar
toplink.jar
spring.jar
commons-logging-1.1.1.jar
xercesImpl.jar
xml-apis.jar
serializer.jar
xsltc.jar
xalan.jar
xmlparserv2.jar
Spring Configuration
<bean id="toplinkSessionFactoryInitialiser" class="com.somellc.toplink.TopLinkSessionFactoryInitialiser"/><bean id="toplinkSessionCreation" factory-bean="toplinkSessionFactoryInitialiser" factory-method="createSession"> <constructor-arg type="java.lang.String" value="localSession"/></bean><bean id="toplinkSessionBean" class="com.somellc.toplink.TopLinkSessionBean"> <property name="sessionObject" ref="toplinkSessionCreation"/></bean>
<bean id="toplinkSessionObject" factory-bean="toplinkSessionBean" factory-method="getSessionObject"/><bean id="toplinkSessionEventManager" factory-bean="toplinkSessionBean" factory-method="getSessionEventManager"/><bean id="toplinkSessionProject" factory-bean="toplinkSessionBean" factory-method="getSessionProject"/>
<bean id="readAllQuerySimpleExpression" class="com.somellc.spring_toplink3.ReadAllQuerySimpleExpression"> <property name="mySessionObject" ref="toplinkSessionObject"/></bean>
Session.xml
<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; <session xsi:type="database-session"> <name>localSession</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>hr</user-name> <password>hr</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:@wshi777:1521:orcl</connection-url> </login> </session></toplink-sessions>
IOCManager
public Object getBean(String fn, String beanId) {
Object obj;
try {
filename = fn;
context = new ClassPathXmlApplicationContext(fn);
BeanFactory factory = (BeanFactory) context;
obj = factory.getBean(beanId);
return obj;
}
catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getStackTrace());
return null;
}
}
public ClassPathXmlApplicationContext getContext(String fn) {
try {
filename = fn;
context = new ClassPathXmlApplicationContext(fn);
return context;
}
catch (Exception e) {
return null;
}
}
public DatabaseSession createSession(String sessionName) {
return (DatabaseSession)SessionManager.getManager().getSession(sessionName);
}
TopLinkSessionBean.java
public class TopLinkSessionBean {
protected static DatabaseSession sessionObject;
// @Required
public void setSessionObject(DatabaseSession sessionObj) {
this.sessionObject = sessionObj;
}
public DatabaseSession getSessionObject() {
return this.sessionObject;
}
public SessionEventManager getSessionEventManager() {
return (SessionEventManager)this.sessionObject.getEventManager();
}
public Project getSessionProject() {
return (Project)this.sessionObject.getProject();
}
}
ReadAllQuerySimpleExpression.java
// @Required
public void setMySessionObject(DatabaseSession sessionObject) {
this.mySessionObject = sessionObject;
}
/**
* Demonstration code using the Employee Demo classes.
* @param args java.lang.String[]
*/
public static void main(String[] args) {
IOCManager iocManager = IOCManager.getIOCManager();
ReadAllQuerySimpleExpression readAllQuerySimpleExpression = (ReadAllQuerySimpleExpression)iocManager.getBean("toplink3-context.xml", "readAllQuerySimpleExpression");
readAllQuerySimpleExpression.runExample();
}
public void runExample() {
log("\n\n\n###START: " + this.getClass().getName());
try {
Expression exp = new ExpressionBuilder().get("firstName").like("J%");
Vector employees = mySessionObject.readAllObjects(Employee.class, exp);
log("\tRead " + employees.size() + " employees with firstName like J%:");
log(employees);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
mySessionObject.logout();
log("\n###END***: " + this.getClass().getName());
}
}