Today I started on hibernate and this is about the basic stuff. More like a hello world. I just finished re-writting my LoginDao to use hibernate framework. This webpage has helped me to do this :
www.vaannila.com/spring/spring-hibernate-integration-1.html
Heres what I have done. This time instead of spending time on working with the classic hibernate which I had learned 2 years ago and have forgotten many details about, I decided to take a chance and plunge into annotations. Thanks to that webpage, it did not cause much problems.
As usual, for hibernate to work, we need a session factory. Confirming with spring, I defined it in the spring context as : 
<bean id="akappsessionfactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="akappdbdatasource" />
		<property name="packagesToScan" value="com.akapp.domain" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory
				</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">create</prop>
			</props>
		</property>
	</bean>
The data source was already defined using JNDI as mentioned in one of my previous posts. The class to be used here is the AnnotationSessionFactoryBean. I have set the datasource. The property hibernateProperties is used to configure various parameters of hiibernate. HSQLDialect helps me to use queries written in HQL, the hibernate query language. This language is similar to SQL. But rather uses entity names specified in the mapping data. 
One important thing to note regarding weblogic is that the property hibernate.query.factory_class MUST BE set as shown above for HQL to work. 
The packagesToScan property is used by hibernate to scan packages for domain objects. Domain classes are nothing but POJOs to be used by hibernate to carry data between your application and the database.  The classic hibernate would expect domain classes and mapping files. In the annotation version, the mapping is specified within the domain classes itself. Heres the domain class that I have used : 
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package com.akapp.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
 *
 * @author Anoop.Kammaran
 */
@Entity(name="LOGININFO")
public class LoginInfo {
    @Id
    @Column(name="ID")
	String user;
    @Column(name="PASSWD")
    String passwd;
public LoginInfo() {}
    public LoginInfo(String user, String passwd) {
        this.user   = user;
        this.passwd = passwd;
    }
    public String getPasswd() {
        return passwd;
    }
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }
}
Now the new Dao method is as follows :
@Override
	public LoginInfo getCredentialsFromDBHibernate(String user) {
		try {
			return (LoginInfo) hibernateTemplate.find(
					"from LOGININFO as info where info.user=?", user).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new LoginInfo();
	}
As you can see, I have used hibernateTemplate, which is an object of the HibernateTemplate class. I have defined the spring bean for this object as :
<bean id="akapphibernatetemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="akappsessionfactory" />
	</bean>
I have used this bean to inject into my Dao class as :
<bean name="loginDaoImpl" class="com.akapp.dao.impl.LoginDaoImpl">
		<property name="hibernateTemplate" ref="akapphibernatetemplate" />
	</bean>
Thats pretty much it. This is my very first work with hibernate with annotations from scratch. I’l be continuing towards more advanced areas of hibernate, namely JTA, etc. Lets wait and see 😉

