Monday, April 15, 2013

Hibernate 4 configuration with spring framework


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
       

<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
   <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="test.model" />
    <property name="hibernateProperties">
        <props>
            vprop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
  <prop key="hibernate.format_sql">false</prop>
  <prop key="hibernate.use_sql_comments">true</prop>
  <prop key="hibernate.connection.release_mode">auto</prop>
  < rop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</prop>
  <prop key="show_sql">false
  <prop key="format_sql">true
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
   
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>


package com.marcura.dadesk.dis.dao.model;
import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/*
 *@Author Sandeep
 */
@Entity
@Table(name="TBL_LOGGING_MASTER")
public class LoggingMaster implements Serializable{


@Id
@Column(name = "TX_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE,  generator = "master_seq")
@SequenceGenerator(name = "master_seq", sequenceName = "LOGGING_MASTER_SEQ")
private long txID;
@Column(name ="PRINCIPAL_ID", nullable=false )
private long principalId;

public LoggingMaster(long principalId){
this.principalId = principalId;
}
public LoggingMaster(){}

public long getTxID() {
return txID;
}

public void setTxID(long txID) {
this.txID = txID;
}

public long getPrincipalId() {
return principalId;
}

public void setPrincipalId(long principalId) {
this.principalId = principalId;
}

@Override
public String toString() {
return "LoggingMaster [txID=" + txID + ", principalId=" + principalId + "]";
}
}


/*
 *@Author Sandeep
 *@date Feb 14, 2013
 */
package com.marcura.dadesk.dis.services.impl;

import java.util.List;

import org.hibernate.Session;

import com.marcura.dadesk.dis.dao.HibernateDAO;
import com.marcura.dadesk.dis.dao.model.LoggingMaster;
import com.marcura.dadesk.dis.services.HibernateService;

public class HibernateServiceImpl implements HibernateService {
private HibernateDAO hibernateDAO;
@Override
public void update(Object entity) {
// TODO Auto-generated method stub
hibernateDAO.update(entity);
}

@Override
public void save(Object entity) {
// TODO Auto-generated method stub
hibernateDAO.save(entity);
}

@Override
public void saveOrUpdate(Object entity) {
// TODO Auto-generated method stub
hibernateDAO.saveOrUpdate(entity);
}

@Override
public void delete(Object entity) {
// TODO Auto-generated method stub
hibernateDAO.delete(entity);
}

public HibernateDAO getHibernateDAO() {
return hibernateDAO;
}

public void setHibernateDAO(HibernateDAO hibernateDAO) {
this.hibernateDAO = hibernateDAO;
}

@Override
public Session getCurrentSession() {
// TODO Auto-generated method stub
return this.hibernateDAO.getCurrentSession();
}


@Override
public LoggingMaster load(long txId) {
// TODO Auto-generated method stub
return this.hibernateDAO.load(txId);
}

}

/*
*@Author Sandeep
*@date Feb 14, 2013
*/
package com.marcura.dadesk.dis.services;

import java.util.List;

import org.hibernate.Session;

import com.marcura.dadesk.dis.dao.model.LoggingDetails;
import com.marcura.dadesk.dis.dao.model.LoggingMaster;
public interface HibernateService {
void update(Object entity);
void save(Object entity);
void saveOrUpdate(Object entity);
void delete(Object entity);
Session getCurrentSession();
LoggingMaster load(long txId);

}