Showing posts with label ORM. Show all posts
Showing posts with label ORM. Show all posts

Monday, February 8, 2010

Oracle ORM TopLink with Spring

Project: Spring_TopLink3
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;
}
}

TopLinkSessionFactoryInitialiser.java
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());
}
}

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());
}
}

Spring Hibernate Example - Credit Rating

Project: Spring_Credit_Rating
Description: Make Spring and Hibernate work together
IDE: Eclipse
JARs:
commons-logging-1.1.1.jar
spring.jar (2.5.6)
cglib-nodep-2.1.3.jar
asm-attrs-1.5.3.jar
antlr-2.7.6.jar
asm-1.5.3.jar
ojdbc14.jar
commons-collections-2.1.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
hibernate-3.2.5.ga.jar
jta.jar
xml-apis.jar
serializer.jar
xercesImpl.jar
xalan.jar
xsltc.jar

Spring Configuration
<bean id="createCreditCard" class="com.somellc.CreateCreditCardAccount"> <property name="creditRatingInterface"> <ref bean="creditRating" /> </property> <property name="creditLinkingInterface"> <ref bean="creditLinking" /> </property> <property name="emailInterface"> <ref bean="email" /> </property> </bean> <bean id="creditLinking" class="com.somellc.CreditLinking"> <property name="url"> <value>http://localhost/creditLinkService</value> </property> </bean> <bean id="creditRating" class="com.somellc.CreditRating"> </bean> <bean id="email" class="com.somellc.Email"> <property name="smtpHost"> <value>localhost</value> </property> <property name="fromEmail"> <value>mycompanyadmin@mycompanyadmin.com</value> </property> <property name="userId"> <value>myuserid</value> </property> <property name="password"> <value>mypassword</value> </property> </bean>
Hibernate Configuration
<hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@wshi7:1521:orcl</property> <property name="hibernate.connection.username">hr</property> <property name="hibernate.connection.password">hr</property> <property name="default_schema">hr</property> <property name="connection.autocommit">false</property> <property name="show_sql">true</property> <property name="use_outer_join">false</property> <property name="hibernate.cache.use_second_level_cache">false</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <property name="current_session_context_class">thread</property> <property name="hibernate.connection.pool_size">10</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Mapping files --> <mapping resource="Customer.hbm.xml"/>
</session-factory></hibernate-configuration>

Customer Table Configuration
<hibernate-mapping> <class name="com.somellc.NewCustomer" table="TAB_CUSTOMER"> <id name="customerId" type="long" column="CUSTOMER_ID" > <generator class="sequence"/> </id>
<property name="customerName" type="java.lang.String" column="CUSTOMER_NAME" update="true" insert="true"></property>
</class></hibernate-mapping>

Product Table Configuration
<hibernate-mapping> <class name="com.onstar.brm.begspring.Product" table="TAB_PROD"> <id name="productId" type="long" column="PROD_ID" > <generator class="sequence"/> </id>
<property name="productName" type="java.lang.String" column="PROD_NM" update="true" insert="true"></property> <property name="productDesc" type="java.lang.String" column="PROD_DESC" update="true" insert="true"></property>
</class></hibernate-mapping>


CreateCreditAccountClient.java
INewCustomer inewcustomer = new NewCustomer();inewcustomer.setCustomerName("Hibernate Example Customer");
IProductDAO iproductDAO1 = new ProductDAO(); iproductDAO1.createSession(); iproductDAO1.getProduct(63); //CreditCardAccountCreateCreditCardAccountInterface creditCardAccount = (CreateCreditCardAccountInterface)appContext.getBean("createCreditCard");creditCardAccount.createCreditCardAccount(icustomer);

CreateCreditCardAccount.java
public void createCreditCardAccount(ICustomer icustomer) throws Exception{
boolean crediRating = getCreditRatingInterface().getUserCreditHistoryInformation(icustomer);
icustomer.setCreditRating(crediRating);
//Good Rating
if(crediRating){
getCreditLinkingInterface().linkCreditBankAccount(icustomer);
}
getEmailInterface().sendEmail(icustomer);
}


CreateCreditCardAccountInterface.java
public interface CreateCreditCardAccountInterface {
public CreditLinkingInterface getCreditLinkingInterface();
public void setCreditLinkingInterface(
CreditLinkingInterface creditLinkingInterface);
public CreditRatingInterface getCreditRatingInterface();
public void setCreditRatingInterface(
CreditRatingInterface creditRatingInterface);
public EmailInterface getEmailInterface();
public void setEmailInterface(EmailInterface emailInterface);
public void createCreditCardAccount(ICustomer icustomer) throws Exception;
}


CreditLinking.java
public class CreditLinking implements CreditLinkingInterface {
private String url;
public void linkCreditBankAccount(ICustomer icustomer) throws Exception {
//Connect to URL
System.out.println("url to connect is" + url);
System.out.println("credit card linked for customer id " +icustomer.getCustomerId());
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}


CustomerDAO.java
public class CustomerDAO implements ICustomerDAO{
private Session session = null;
public void createSession(){
Configuration cfg = new Configuration()
.configure("hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
this.session = sf.getCurrentSession();
}
public INewCustomer create(INewCustomer inewCustomer) {
Transaction transaction = session.beginTransaction();
session.save(inewCustomer);
transaction.commit();
return inewCustomer;
}
}


Customer.java
public class Customer implements ICustomer {
// Existing Customer of bank
private String customerId;
// Customer Information
private String ssnId;
private String firstName;
private String lastName;
private String emailAddress;
private boolean creditRating;
// Address Information
private IAddress iAddress;
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public IAddress getIAddress() {
return iAddress;
}
public void setIAddress(IAddress address) {
iAddress = address;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSsnId() {
return ssnId;
}
public void setSsnId(String ssnId) {
this.ssnId = ssnId;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public boolean isCreditRating() {
return creditRating;
}
public void setCreditRating(boolean creditRating) {
this.creditRating = creditRating;
}
}