Monday, February 8, 2010

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


No comments:

Post a Comment