Tuesday, February 2, 2010
Differance between hibernate session find() vs get(),load();
get() and load() returns >>>>>>>>>> object
but find() returns >>>>>>>>> List
cascade Attribute in Hibernate .hbm.xml
1) cascade="none", the default, tells Hibernate to ignore the association.
2) cascade="save-update" tells Hibernate to navigate the association when the
transaction is committed and when an object is passed to save() or
update() and save newly instantiated transient instances and persist changes to
detached instances.
3) cascade="delete" tells Hibernate to navigate the association and delete persistent
instances when an object is passed to delete().
4) cascade="all" means to cascade both save-update and delete, as well as
calls to evict and lock.
5) cascade="all-delete-orphan" means the same as cascade="all" but, in addition,
Hibernate deletes any persistent entity instance that has been removed
(dereferenced) from the association (for example, from a collection).
6) cascade="delete-orphan" Hibernate will delete any persistent entity
instance that has been removed (dereferenced) from the association (for
example, from a collection).
Advantage of Hibernate session load() vs get()
thrown.
The load() method never returns null.
The get() method returns
null if the object can’t be found.
The get() method will return a FULL initialized object if nothing is on the session cache, that means several DB hits depending on your mappings.
While the load() method will return a proxy (or the instance if already initialized), allowing lazy initialization and thus better performance
Choosing between get() and load() is easy: If you’re certain the persistent
object exists, and nonexistence would be considered exceptional, load() is a
good option. If you aren’t certain there is a persistent instance with the given
identifier, use get() and test the return value to see if it’s null.
Monday, December 28, 2009
AOP in Spring Example
/**
* Created by IntelliJ IDEA.
* User: Gayan-Laptop
* Date: Dec 22, 2009
* Time: 1:49:12 AM
* To change this template use File | Settings | File Templates.
*/
public interface Adder {
public int add(int a, int b);
}
=====================================================
package aop;
/**
* Created by IntelliJ IDEA.
* User: Gayan-Laptop
* Date: Dec 22, 2009
* Time: 1:51:13 AM
* To change this template use File | Settings | File Templates.
*/
public class AdderImpl implements Adder {
public int add(int a, int b) {
return a + b;
}
}
===============================================================
package aop;/**
* Created by IntelliJ IDEA.
* User: Gayan-Laptop
* Date: Dec 22, 2009
* Time: 1:53:20 AM
* To change this template use File | Settings | File Templates.
*/
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class LogAfterReturningAdvice implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args,
Object target) throws Throwable {
System.out.println("After Normal Return from Method (here you can remove attribute in session )");
}
}
=============================================================
package aop;/**
* Created by IntelliJ IDEA.
* User: Gayan-Laptop
* Date: Dec 22, 2009
* Time: 1:53:53 AM
* To change this template use File | Settings | File Templates.
*/
import org.springframework.aop.ThrowsAdvice;
import java.lang.reflect.Method;
public class LogAfterThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Method method, Object[] args, Object target,
Exception exception) {
System.out.println("Exception is thrown on method " + method.getName());
}
}
======================================================
package aop;/**
* Created by IntelliJ IDEA.
* User: Gayan-Laptop
* Date: Dec 22, 2009
* Time: 1:54:23 AM
* To change this template use File | Settings | File Templates.
*/
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class LogAroundAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("LogAroundAdvice invoke method (here you can check method Arguments validation) ");
Object arguments[] = methodInvocation.getArguments();
int number1 = ((Integer) arguments[0]).intValue();
int number2 = ((Integer) arguments[1]).intValue();
if (number1 == 0 && number2 == 0) {
throw new Exception("Dont know how to add 0 and 0!!!");
}
if(number2==0){
throw new Exception("Dont know how to divide by 0 !!!");
}
return methodInvocation.proceed();
}
}
==========================================================
package aop;/**
* Created by IntelliJ IDEA.
* User: Gayan-Laptop
* Date: Dec 22, 2009
* Time: 1:52:11 AM
* To change this template use File | Settings | File Templates.
*/
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class LogBeforeCallAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) {
System.out.println("Before Calling the Method (here you can check user sessions) ");
}
}
=======================================================
package aop;
/**
* Created by IntelliJ IDEA.
* User: Gayan-Laptop
* Date: Dec 22, 2009
* Time: 2:18:15 AM
* To change this template use File | Settings | File Templates.
*/
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
public class AdderTest {
public static void main(String args[]) {
try {
Resource resource = new FileSystemResource("./aop-test.xml");
BeanFactory factory = new XmlBeanFactory(resource);
try{
Adder adder = (Adder) factory.getBean("adder");
System.out.println("------------- TEST CASE 1 ---------------");
int result = adder.add(10, 10);
System.out.println("Result = " + result);
System.out.println("------------- TEST CASE 2 ---------------");
result = adder.add(100, 10);
System.out.println("Result = " + result);
System.out.println("------------- TEST CASE 3 ---------------");
result = adder.add(101, 110);
System.out.println("Result = " + result);
System.out.println("------------- TEST CASE 4 ---------------");
result = adder.add(0, 0);
System.out.println("Result = " + result);
}catch(Exception e){
e.printStackTrace();
}
try{
Divider divider = (Divider) factory.getBean("divider");
System.out.println("------------- TEST CASE 5 ---------------");
int result2 = divider.divide(6,3);
System.out.println("Result = " + result2);
System.out.println("------------- TEST CASE 6 ---------------");
result2 = divider.divide(12,3);
System.out.println("Result = " + result2);
System.out.println("------------- TEST CASE 7 ---------------");
result2 = divider.divide(16,3);
System.out.println("Result = " + result2);
System.out.println("------------- TEST CASE 9 ---------------");
result2 = divider.divide(6,0);
System.out.println("Result = " + result2);
}catch(Exception e){
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
================================================================
aop-test.xml file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- Advices -->
<bean id="beforeCall" class="aop.LogBeforeCallAdvice"/>
<bean id="afterCall" class="aop.LogAfterReturningAdvice"/>
<bean id="throwCall" class="aop.LogAfterThrowsAdvice"/>
<bean id="aroundCall" class="aop.LogAroundAdvice"/>
<!-- Implementation Class -->
<bean id="adderImpl" class="aop.AdderImpl"/>
<bean id="dividerImpl" class="aop.DividerImpl"/>
<!-- Proxy Implementation Class -->
<bean id="adder" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>aop.Adder</value>
</property>
<property name="interceptorNames">
<list>
<value>beforeCall</value>
<value>afterCall</value>
<value>throwCall</value>
<value>aroundCall</value>
</list>
</property>
<property name="target">
<ref bean="adderImpl"/>
</property>
</bean>
<bean id="divider" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>aop.Divider</value>
</property>
<property name="interceptorNames">
<list>
<value>beforeCall</value>
<value>afterCall</value>
<value>throwCall</value>
<value>aroundCall</value>
</list>
</property>
<property name="target">
<ref bean="dividerImpl"/>
</property>
</bean>
</beans>
Monday, December 21, 2009
jdom Example
<?xml version="1.0" encoding="UTF-8"?>
<database>
<main id="jdbcconnecter">
<driver id="jdbc">oracle.jdbc.driver.OracleDriver</driver>
<url id="java mysql">jdbc:oracle:thin:@XXXXXXXX:XXX</url>
<username uname="defult">XXXX</username>
<password password="defult">XXX</password>
</main>
</database>
public void setConnectionData(){
try{
String filename ="dataconnect.xml";
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(filename));
Element root = doc.getRootElement();
List servlets = root.getChildren("main");
Iterator i = servlets.iterator();
while (i.hasNext()) {
Element servlet = (Element) i.next();
driver= servlet.getChild("driver").getTextTrim();
url=servlet.getChild("url").getTextTrim();
username=servlet.getChild("username").getTextTrim();
password=servlet.getChild("password").getTextTrim();
}//end while
}catch(Exception e){
e.printStackTrace();
}
}
Wednesday, December 16, 2009
Ant Script to compile,deploy,test EJB Appication
<project name="jblog" default="ear">
<property environment="env"/>
<property name="jboss.home" value="${env.JBOSS_HOME}"/>
<property name="jboss.server.dir" value="${jboss.home}/server/default"/>
<property name="jboss.deploy.dir" value="${jboss.server.dir}/deploy"/>
<property name="source.dir" value="src"/>
<property name="classes.dir" value="classes"/>
<property name="docs.dir" value="docs"/>
<property name="docs.test.dir" value="${docs.dir}/test"/>
<property name="docs.api.dir" value="${docs.dir}/api"/>
<property name="app.ear" value="${ant.project.name}.ear"/>
<property name="app.war" value="${ant.project.name}.war"/>
<property name="app.ejb.jar" value="${ant.project.name}-ejb.jar"/>
<property name="app.client.jar" value="${ant.project.name}-client.jar"/>
<property name="app.test.jar" value="${ant.project.name}-test.jar"/>
<property name="test.args" value=""/>
<property name="test.include" value="**/*Test.class"/>
<property name="test.exclude" value=""/>
<property name="db.url" value="jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1111:sid"/>
<property name="db.user" value="ehosbk"/>
<property name="db.password" value="ehosbk"/>
<property name="db.driver" value="oracle.jdbc.driver.OracleDriver"/>
<!-- compile classpath -->
<path id="compile.class.path">
<pathelement location="${env.J2EE_HOME}/lib/j2ee.jar"/>
<pathelement location="${env.JUNIT_HOME}/junit-4.4.jar"/>
</path>
<!-- test classpath -->
<path id="test.class.path">
<pathelement location="."/>
<pathelement location="${app.test.jar}"/>
<pathelement location="${app.client.jar}"/>
<pathelement location="${jboss.home}/client/jbossall-client.jar"/>
<pathelement location="${jboss.home}/server/default/lib/classes12.jar"/>
<path refid="compile.class.path"/>
</path>
<!-- all -->
<target name="all" depends="clean,ear,deploy,test"/>
<!-- compile -->
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${source.dir}" destdir="${classes.dir}"
classpathref="compile.class.path" debug="yes"/>
<copy todir="${classes.dir}">
<fileset dir="${source.dir}">
<include name="META-INF/**"/>
</fileset>
</copy>
</target>
<!-- jar -->
<target name="jar" depends="compile">
<jar jarfile="${app.ejb.jar}">
<fileset dir="${classes.dir}">
<include name="META-INF/ejb-jar.xml"/>
<include name="META-INF/jboss.xml"/>
<include name="META-INF/jbosscmp-jdbc.xml"/>
<include name="**/*.class"/>
<exclude name="**/test/**"/>
</fileset>
</jar>
<jar jarfile="${app.client.jar}">
<fileset dir="${classes.dir}">
<include name="**/Author.class"/>
<include name="**/Story.class"/>
<include name="**/Blog.class"/>
<include name="**/*Home.class"/>
</fileset>
</jar>
<jar jarfile="${app.test.jar}">
<fileset dir="${classes.dir}">
<include name="**/test/**"/>
</fileset>
</jar>
</target>
<!-- war -->
<target name="war">
<jar jarfile="${app.war}">
<fileset dir="${source.dir}">
<include name="WEB-INF/web.xml"/>
<include name="*.jsp"/>
</fileset>
</jar>
</target>
<!-- ear -->
<target name="ear" depends="jar,war">
<jar jarfile="${app.ear}">
<fileset dir="${classes.dir}">
<include name="META-INF/application.xml"/>
<include name="${app.ejb.jar}"/>
</fileset>
<fileset dir=".">
<include name="${app.ejb.jar}"/>
<include name="${app.war}"/>
</fileset>
</jar>
</target>
<!-- deploy -->
<target name="deploy">
<copy file="${app.ear}" todir="${jboss.deploy.dir}"/>
</target>
<!-- undeploy -->
<target name="undeploy">
<delete file="${jboss.deploy.dir}/${app.ear}" quiet="true"/>
</target>
<!-- test -->
<target name="test">
<!-- Make docs. dir. -->
<mkdir dir="${docs.test.dir}"/>
<!-- Run test. -->
<junit fork="true" printsummary="on" showoutput="true"
failureproperty="test.failures" errorproperty="test.errors">
<sysproperty key="db.url" value="${db.url}"/>
<sysproperty key="db.user" value="${db.user}"/>
<sysproperty key="db.password" value="${db.password}"/>
<sysproperty key="db.driver" value="${db.driver}"/>
<jvmarg line="${test.args}"/>
<classpath refid="test.class.path"/>
<formatter type="xml"/>
<batchtest todir="${docs.test.dir}">
<fileset dir="${classes.dir}">
<include name="${test.include}"/>
<exclude name="${test.exclude}"/>
</fileset>
</batchtest>
</junit>
<!-- Run report. -->
<junitreport todir="${docs.test.dir}">
<fileset dir="${docs.test.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${docs.test.dir}"/>
</junitreport>
<!-- Fail if failures or errors. -->
<fail if="test.failures">Test failures.</fail>
<fail if="test.errors">Test errors.</fail>
</target>
<!-- clean -->
<target name="clean">
<delete includeEmptyDirs="true" quiet="true">
<fileset dir=".">
<include name="*.jar"/>
<include name="*.war"/>
<include name="*.ear"/>
<include name="junit*.properties"/>
</fileset>
<fileset dir="${classes.dir}"/>
<fileset dir="${docs.dir}"/>
</delete>
</target>
</project>
Tuesday, November 10, 2009
do bulk insert in oracle - example
where s.TXN_DATE between to_date('01/01/2008','dd/mm/yyyy')
and to_date('01/06/2008','dd/mm/yyyy')
)