Thursday, August 20, 2009

Difference between hibernate's save,update and saveOrUpdate() methods

save - save method stores an object into the database. That means it insert an entry if the identifier doesn't exist, else it will throw error. If the primary key already present in the table, it cannot be inserted.

update - update method in the hibernate is used for updating the object using identifier. If the identifier is missing or doesn't exist, it will throw exception.

saveOrUpdate - This method calls save() or update() based on the operation. If the identifier exists, it will call update method else the save method will be called.

Note:

  if you use "unsaved-value="null" " property in .hbm xml this
SaveOrUpdate will not work


EX: <id name="accountID" column="ACCOUNT_ID" type="java.lang.String"
unsaved-value="null">

 

Sunday, August 16, 2009

Sending Email Using Apache Log4J

I mentioned that I was interested in using Log4j's SMTPAppender to e-mail me messages when errors occured in my webapp. I discovered how easy it is. Here's how to configure it.

Add the following configuration settings to Log4j.XML


<appender name="mail" class="org.apache.log4j.net.SMTPAppender">

<param name="SMTPHost" value="YourSMTPHostIP" />

<param name="From" value="FromEmailAddress" />

<param name="To" value="ToEmailAdressSeperatedByComma" />

<param name="Subject" value="Subject" />

<param name="BufferSize" value="1" />

<param name="threshold" value="error" />

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern"

value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />

</layout>

</appender>

Auto close database connection

Auto close database connection setting in Hibernate XML

 <property name="hibernate.transaction.auto_close_session">true</property>

Wednesday, August 12, 2009

Native SQL in Hibernate

public class NativeQueryExample {
public static void main(String[] args) {
Session session = null;
try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();

String sql =" select a.name as name, "+
" sum(a.amount) as total "+
" from Account a";
Query query = session.createSQLQuery(sql)
.addScalar("name",Hibernate.String)
.addScalar("total",Hibernate.DOUBLE);
Object [] data = (Object [])query.uniqueResult();
System.out.println("name : " + data[0]);
System.out.println("amount : " + data[1]);

session.close();

}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}

}
}
POJO Application Frameworks: Spring Vs. EJB 3.0

Albert Einstein once said, "Everything should be made as simple as possible, but not simpler." Indeed, the pursuit of scientific truth has been all about simplifying a theory's underlying assumptions so that we can deal with the issues that really matter. The same can be said for enterprise software development.

A key to simplifying enterprise software development is to provide an application framework that hides complexities (such as transaction, security, and persistence) away from the developers. A well-designed framework promotes code reuse, enhances developer productivity, and results in better software quality. However, the current EJB 2.1 framework in J2EE 1.4 is widely considered poorly designed and over-complicated. Unsatisfied with the EJB 2.1 framework, Java developers have experimented with a variety of other approaches for middleware services delivery. Most noticeably, the following two frameworks have generated a lot of developer interest and positive feedback. They are posed to become the frameworks of choice for future enterprise Java applications.

1). The Spring framework is a popular but non-standard open source framework. It is primarily developed by and controlled by Interface21 Inc. The architecture of the Spring framework is based upon the Dependency Injection (DI) design pattern. Spring can work standalone or with existing application servers and makes heavy use of XML configuration files. 

2).The EJB 3.0 framework is a standard framework defined by the Java Community Process (JCP) and supported by all major J2EE vendors. Open source and commercial implementations of pre-release EJB 3.0 specifications are already available from JBoss and Oracle. EJB 3.0 makes heavy use of Java annotations. 

Related Reading

These two frameworks share a common core design philosophy: they both aim to deliver middleware services to loosely coupled plain old Java objects (POJOs). The framework "wires" application services to the POJOs by intercepting the execution context or injecting service objects to the POJO at runtime. The POJOs themselves are not concerned about the "wiring" and have little dependency upon the framework. As a result, developers can focus on the business logic, and unit test their POJOs without the framework. In addition, since POJOs do not need to inherit from framework classes or implement framework interfaces, the developers have a high degree of flexibility to build inheritance structures and construct applications.

However, while sharing an overall philosophy, the two frameworks use very different approaches to deliver POJO services. While numerous books and articles have been published to compare either Spring or EJB 3.0 to the old EJB 2.1, no serious study has been done to compare Spring to EJB 3.0. In this article, I will examine some key differences behind the Spring and EJB 3.0 frameworks, and discuss their pros and cons. The topics covered in this article also apply to other lesser-known enterprise middleware frameworks, as they all converge on the "loosely coupled POJO" design. I hope this article will help you choose the best framework for your needs.