Hibernate annotation example with Spring - Java @ Desk

Sunday, October 13, 2013

Hibernate annotation example with Spring

Hibernate annotation example with Spring

The web application to be created using Struts Spring and Hibernate can be configured in 6 easy steps. This configuration is an example to configure hibernate using .hbm files using the HibernateDaoSupport as shown below:
1) Configure the datasource - This is configured in the spring application context xml file. Configure the bean for datasource using class org.springframework.jdbc.datasource.DriverManagerDataSource. The class can be found in jar spring-jdbc-2.0.7.jar
Load Database Configuration from Properties file

 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
 <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value>  
 </property>  
 <property name="url"><value>jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]</value>  
 </property>  
 <property name="username"><value>[USERNAME]</value></property>  
    <property name="password"><value>[PASSWORD]</value></property>  
 </bean>
Load Database Configuration from Properties file
2) Configure the SessionFactory of hibernate - This is configured in the spring application context xml file. Configure the bean for SessionFactory using class org.springframework.orm.hibernate.LocalSessionFactoryBean. The class can be found in jar spring-hibernate.jar

<!-- Hibernate SessionFactory -->  
 <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">  
 <property name="dataSource"><ref local="dataSource"/></property>  
 <property name="annotatedClasses">  
 <list>  
 <value>com.javacodeimpl.hibernate.entity.Person</value>  
 </list>  
 </property>  
 <property name="hibernateProperties">  
 <props>  
 <prop key="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</prop>  
 </props>  
 </property>  
 </bean>   
3) Create the Person hibernate entity - In this step, create the Person hibernate entity annotated class.


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
 
@Entity
@Table(name = "Person")
public class Stock implements java.io.Serializable {
 
 private Integer id;
 private String name;

 @Id
 @GeneratedValue(strategy = IDENTITY)
 @Column(name = "ID", unique = true, nullable = false)
 public Integer getId() {
  return this.stockId;
 }
 
 public void setId(Integer id) {
  this.id = id;
 }
 
 @Column(name = "NAME"nullable = false)
 public String getName() {
  return this.name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
}

4) Configure the DAO class - This configuration is done in the application context xml file. It is required to create the bean for the DAO class in which the database operations will be performed.

 <bean id="personDAO" class="com.javacodeimpl.dao.hibernate.PersonDAOHibernate">  
 <property name="sessionFactory"><ref local="sessionFactory"/></property>  
 </bean>
5) Create the java DAO class - The java class in which the DB operations are performed. Create methods to get, load, save, update the Person class object with the DB table Person.

public class PersonDAOHibernate extends HibernateDaoSupport implements PersonDAO {
 public List getPersons() {
  return getHibernateTemplate().find("from Person");
 } 
 public User getPerson(Long id) {
  return (Person) getHibernateTemplate().get(Person.class, id);
 } 
 public void saveUser(Person person) {
  getHibernateTemplate().saveOrUpdate(person); 
 }
getHibernateTemplate() - Return the HibernateTemplate for DAO, pre-initialized with the SessionFactory or set explicitly.






No comments:

Post a Comment