Wednesday, October 28, 2009

Homer Simpson on Hibernate: DAO! (Part 2)

Let's pick it up where we left off; writing code for the implementation part of our dao.

To get started, we need to get the tapestry-hibernate lib to make it easy for us to get hibernate and tapestry talking. We are going to use maven for this.

Now open you pom.xml the project file folder and add this:

 <dependency>  
       <groupId>org.apache.tapestry</groupId>  
       <artifactId>tapestry-hibernate</artifactId>  
       <version>5.1.0.5</version>  
 </dependency>  

This should be in the dependencies part of the pom file. Once you save this, it will automatically get the files for you. Remember to connect to the internet. You gotta love Maven.

We got our library, now we have to actually write the implementation code - we start with creating an impl package within the dao package and creating two Java classes namely: AddressDAOimpl and LoginDAOimpl. This Java classes implement their respective DAO interfaces.

Here is part of that AddressDAOimple.java source code.
 import org.apache.tapestry5.ioc.annotations.Inject;  
 import org.hibernate.Session;  
 /**  
  *  
  * @author killertilapia  
  */  
 public class AddressDAOimpl implements AddressDAO {  
   @Inject  
   private Session session;  
   public void add(Address newAddress) {  
     session.save(newAddress);  
   }  
   public void delete(Address address) {  
     session.delete(address);  
   }  
   public List<Address> retrieveAll() {  
     return session.createCriteria(Address.class).list();  
   }  
   public void update(Address address) {  
     session.saveOrUpdate(address);  
   }  
 }  

LoginDAOimpl contains the same idea.

It's quite simple really since Hibernate hides almost all(if not all) of the database/sql/jbdc code from us. All we really need is that session object that we injected into the class. That session object is our link to the database - think of it as an instantace of hibernate.cfg.xml.

We got the database part working now all we need is for someway to make it available to the entire web application. Enter Inversion of Control or IOC. We simply expose this thing as a service and then we can inject it into any part of the web application where we need it just like that session object earlier.

Open AppModule.java file. It should be in the services package. We first bind the DAO interfaces with their implementation file. Then we "give advice" to it using HibernateTransactionAdviser interface. This way the advice method is configured to match against any service whose id ends with "DAO", such as "PersonDAO". The advisor scans the service interface and identifies any methods with the @CommitAfter annotation. Got all that?

Here's the bind:

 public static void bind(ServiceBinder binder)  
 {  
     binder.bind(AddressDAO.class, AddressDAOimpl.class);  
     binder.bind(LoginDAO.class, LoginDAOimpl.class);  
 }  

The advisor part:
 @Match("*DAO")  
 public static void adviseTransactions(HibernateTransactionAdvisor advisor, MethodAdviceReceiver receiver) {  
     advisor.addTransactionCommitAdvice(receiver);  
 }  

And here's the @CommitAfter annotation in the DAOs:

 public interface AddressDAO {  
   @CommitAfter  
   public void add(Address newAddress);  
   @CommitAfter  
   public List<Address> retrieveAll();  
   @CommitAfter  
   public void update(Address address);  
   @CommitAfter  
   public void delete(Address address);  
 }  

See...was that so bad?

Anyway, I am putting the code into a subversion repository so you can get your bloody claws into it. Again, if you don't know what a subversion repo educate yourself. You'll probably need a kenai.com. account. Make one, its free anyway and it integrates with Netbeans6.7 quite nicely.