Monday, September 2, 2013
Creating Finders method and Custom Queries
Thursday, August 15, 2013
Running a Debug in Eclipse: (Also useful for Remote Java application)
Running a Debug in Eclipse:
(Also useful for Remote Java application)
We just need to connect the sourcecode in eclipse to your eclipse debug (that will automatically detect a running tomcat on your machine) .Fill the name of the debug in which you want to run
Resume
button),that will take you to the
break point from there debug each line with F6.If you want to go to next break point press F8.
Tuesday, August 6, 2013
Some interesting Eclipse Short-cuts
Monday, July 29, 2013
Maven commands needed for a Portlet with service layer
Frequently Needed Maven commands in Liferay :
Tuesday, October 26, 2010
Hibernate Integration with Spring
Prerequisite Steps
1) Create java class (bean) Employee
2) Create Database and table employee
3) Create hbm.xml
4) make all mappings
3.4) Creating the Spring Configuration File
This section deals with configuring the various information needed for the Spring Framework. In Spring, all the business objects are configured in Xml file and the configured business objects are called Spring Beans. These Spring Beans are maintained by the IOC which is given to the Client Application upon request. Let us define a data source as follows,
spring-hibernate.xml
Refer : http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-3.html
The above bean defines a data-source of type 'org.apache.commons.dbcp.BasicDataSource'
. More importantly, it defines the various connection properties that are needed for accessing the database. For accessing the MySql database, we need MySql database driver which can be downloaded from http://dev.mysql.com/downloads/connector/j/5.1.html. The first property called driverClassName
should point to the class name of the MySql Database Driver. The second property url represents the URL string which is needed to connect to the MySql Database. The third and the fourth properties represent the database username
and the password
needed to open up a database session.
Now, let us define the second Spring Bean which is the SessionFactoryBean
. If you would have programmed in Hibernate, you will realize that SessionFactoryBean
is responsible for creating Session
objects through which Transaction
and Data accessing is done. Now the same SessionFactoryBean
has to be configured in Spring's way as follows,
Refer :
http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-3.html
To make the SessionFactoryBean
to get properly configured, we have given two mandatory information. One is the data-source information which contains the details for accessing the database. This we have configured already in the previous step and have referred it here using the 'ref'
attribute in the 'property'
tag. The second one is a list of Mapping files which contains the mapping information between the database tables and the Java class names. We have defined one such mapping file in section 2 and have referenced the same here with the 'list'
tag.
The 3rd important Spring Bean is the Hibernate Template. It provides a wrapper for low-level data accessing and manipulation. Precisely, it contains methods for inserting/delting/updating/finding data in the database. For the Hibernate Template to get configured, the only argument is the SessionFactoryBean
object as represented in the following section,
Refer : http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-3.html
The final Bean definition is the Dao class which is the client facing class. Since this class has to be defined in the Application level, it can contain any number of methods for wrapping data access to the Client. Since we know that it is the Hibernate Template class that interacts with the database, it will be ideal a refer an instance of Hibernate Template to the Dao class.
Refer : http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-3.html
Reference Link : http://www.javabeat.net/articles/
42-integrating-spring-framework-with-hibernate-orm-framewo-1.html
Tuesday, July 27, 2010
Lazy Initilization in Hibernate
when loading an object that has a collection, hibernate loads collection elements ONLY when actually you ask for them; so this improves performance.
2) lazy initialization improves performance by delaying the fetch from the database until the returned object is actually queried for the data...it works similar to write behind caching on your hard disk ;-) .
3)
when you use hibernate you can make lazy=true or lazy=false.
if you mentioned lazy=true
and you call from a company bean like
Collection x = company.getAllEmployees();
x will be empty;
if u mentioned lazy=false
x will contain the data as output of the getter funtion.
So if you put lazy=true then you need to write HQL query getting these kind of collections. else if lazy = false u can get the collection at the time when u fetch company object.
So taking performance(time cost and memory cost) as criteria the its better to make lazy=true.
Tuesday, June 29, 2010
Basic Spring AOP Tutorial
Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects.
AOP concepts:
• Aspect: a modularization of a concern that cuts across multiple classes. Transaction management is a good example of a crosscutting concern in J2EE applications. In my words: a trigger which can affect the multiple classes a one point….
• Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution. In my words: a locus of points where execution will happen.
• Advice: action taken by an aspect at a particular join point. Different types of advice include “around,” “before” and “after” advice. (Advice types are discussed below.) In my words : the action to be taken at the point.
• Pointcut: a predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default. In my words a criteria used to locate point.
• Introduction: declaring additional methods or fields on behalf of a type. Spring AOP allows you to introduce new interfaces (and a corresponding implementation) to any advised object. For example, you could use an introduction to make a bean implement an IsModified interface, to simplify caching. (An introduction is known as an inter-type declaration in the AspectJ community.)
• Target object: object being advised by one or more aspects. Also referred to as the advised object. Since Spring AOP is implemented using runtime proxies, this object will always be a proxied object.
• AOP proxy: an object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on). In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.
Consider the example:
Lets declare an interface:
public interface Foo {
Foo getFoo(String fooName,int age);
void getAfter();
void getBefore(String myName);
}
A class implementing the interface:
public class DefaultFooService implements FooService {
public Foo getFoo(String name, int age) {
return new Foo(name, age);
}
public void getAfter() {}
public void getBefore(String myName) {}
}
Till here we have simple java implementation.
Now let see come AOP concepts in picture.
Before - Now I want that before the getBefore() method is called I want to log message saying what is the parameter passed.
After - Also I want that once any method in the interface is called a message should be logged after it.
I have a class which will be called to satisfy the above criteria.
public class SimpleProfiler {
public void afterMethod() throws Throwable {
System.out.println(“After the method call”);
}
public void beforeMethod(String myName){
System.out.println(“My name is “+myName);
}
}
The afterMethod() will log message after any method is called and beforeMethod() will log message before getBefore() is called.
To configure this we will used xmlThis is how I configure my spring.xml.
<beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns:aop=“http://www.springframework.org/schema/aop”
xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd”>
<!– this is the object that will be proxied by Spring’s AOP infrastructure –>
1 <bean id=“fooService” class=“DefaultFooService”/>
2
3 <!– this is the actual advice itself –>
4 <bean id=“profiler” class=“SimpleProfiler”/>
5
6 <aop:config>
7 <aop:aspect ref=”profiler”>
14 <aop:pointcut id=“aopafterMethod”
expression=“execution(* FooService.*(..))”/>
15 <aop:after pointcut-ref=“aopafterMethod”
method=“afterMethod”/>
16 <aop:pointcut id=“aopBefore”
expression=“execution(* FooService.getBefore(String)) and args(myName)”/>
17 <aop:before pointcut-ref=“aopBefore”
method=“beforeMethod”/>
</aop:aspect>
</aop:config>
</beans>
Let see how we have configure the AOP .
Similarly for beforeMethod we define in Line 16,17.
In above example we have
Aspect – SimpleProfiler.class
Point-cut – aopafterMethod,aopBefore
Advice <aop:before> <aop:after>
Now I am ready to run my main class and class methods of FooService.
public class Boo {
public static void main(final String[] args) throws Exception {
BeanFactory ctx = new ClassPathXmlApplicationContext("spring.xml");
FooService foo = (FooService) ctx.getBean("fooService");
foo.getFoo("Pengo", 12);
foo.getAfter();
foo.getBefore("Manish");
}
}
OutPut is
After the method call ( log messagefor getAfter method )
My name is Manish (log message for getBefore)
After the method call (log message for getAfter method)