azizkhani.net

I know that I know nothing

Spring and hibernate 4 integration

clock October 25, 2012 18:29 by author Administrator

add this dependancy

 

    <!-- Hibernate framework -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.4.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>
        <!-- end hibernate -->

 

and then change spring configuration like this

 



    <tx:annotation-driven transaction-manager="hibernateTransactionManager" />
    <bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${hibernate.connection.driver_class}" />
        <property name="url" value="${hibernate.connection.url}" />
        <property name="username" value="${hibernate.connection.username}" />
        <property name="password" value="${hibernate.connection.password}" />
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingLocations">
            <list>
                <value>/WEB-INF/classes/hibernate/**/*.hbm.xml</value>
            </list>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.connection.driver_class"> ${hibernate.connection.driver_class}</prop>
                <prop key="hibernate.connection.url"> ${hibernate.connection.url}</prop>
                <prop key="hibernate.connection.username"> ${hibernate.connection.username}</prop>
                <prop key="hibernate.connection.password"> ${hibernate.connection.password}</prop>
                <!-- <prop key="hibernate.connection.internal_logon"> ${hibernate.connection.internal_logon}</prop> -->
                <prop key="hibernate.dialect"> ${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql"> ${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto"> ${hibernate.hbm2ddl_auto}</prop>
                <!-- <prop key="hibernate.default_schema"> ${hibernate.default_schema}</prop> -->
            </props>
        </property>
    </bean>

    <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

 

and now

 

package org.azizkhani.dao;

import java.util.List;

import org.azizkhani.core.QueryResult;

public interface IGenericRepository<T> {
    public List<T> getAll();
    public List<T> getAll(String where, String order, int pageNumber,int pageSize);
    public QueryResult<T> getAllGrid(String where, String order, int pageNumber,int pageSize);
    public T loadByEntityId(int entityId);
    public T single(String where);
    public void add(T entity);
    public void delete(T entity);
    public void deleteByEntityId(int entityId);
    public void update(T entity);
    public int count();
}

 

implementation

 

package org.azizkhani.dao.impl.hibernate;

import java.util.List;

import org.azizkhani.common.utility.HQLUtility;
import org.azizkhani.core.QueryResult;
import org.azizkhani.dao.IGenericRepository;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public abstract class GenericRepository<T>  implements IGenericRepository<T> {
    protected Class<T> domainClass = getDomainClass();

    protected abstract Class<T> getDomainClass();
   
    @Autowired
    private SessionFactory sessionFactory;
   

    @Override
    public List<T> getAll() {
        Criteria criteria = getSession().createCriteria(domainClass.getName());
        return criteria.list();
    }

    public Session getSession() {
        try {
            return sessionFactory.getCurrentSession();
        } catch (Exception e) {
            System.out.println(e.getMessage().toString());
        }
        return sessionFactory.openSession();
    }

    public QueryResult<T> getAllGrid(String where, String order, int pageNumber, int pageSize) {
        Session session = getSession();
        StringBuffer jql = new StringBuffer("from " + domainClass.getName()
                + " e ");
        HQLUtility.toHQL(jql, where, order);
        Query query = session.createQuery(jql.toString());
        if (pageSize > 0)
        {
            query.setFirstResult(pageNumber * pageSize);
            query.setMaxResults(pageSize);
        }
        List<T> list = query.list();

        StringBuffer jqlCount = new StringBuffer("select count(*)  from "
                + domainClass.getName() + " e ");
        HQLUtility.toHQL(jqlCount, where, "");
        int count = ((Long) getSession().createQuery(jqlCount.toString())
                .uniqueResult()).intValue();

        return new QueryResult<T>(pageNumber, count, pageSize, list);
    }

    @Override
    public List<T> getAll(String where, String order, int pageNumber, int pageSize) {
        Session session = getSession();
        StringBuffer jql = new StringBuffer("from " + domainClass.getName()
                + " e ");
        HQLUtility.toHQL(jql, where, order);
        Query query = session.createQuery(jql.toString());
        if (pageSize > 0)
        {
            query.setFirstResult(pageNumber * pageSize);
            query.setMaxResults(pageSize);
        }
        return query.list();
    }
    @Transactional(readOnly=true)
    @Override
    public T loadByEntityId(int entityId) {
        return (T) getSession().load(domainClass, entityId);
    }

    @Override
    public T single(String where) {
        return null;
    }

    @Override
    public void add(T entity) {
        Session session = getSession();
        session.beginTransaction();
        session.save(entity);
        session.getTransaction().commit();
        session.close();
    }

    @Override
    public void delete(T entity) {
        getSession().delete(entity);
    }

    @Override
    public void deleteByEntityId(int entityId) {
        Session session = getSession();
        session.beginTransaction();
        Object obj = session.load(domainClass, entityId);
        session.delete(obj);
        session.getTransaction().commit();
        session.close();
    }

    @Override
    public void update(T entity) {
        Session session = getSession();
        session.beginTransaction();
        session.update(entity);
        session.getTransaction().commit();
        session.close();
    }

    @Override
    public int count() {
        int count = ((Long) getSession().createQuery(
                "select count(*) from " + domainClass.getName()).uniqueResult())
                .intValue();
        return count;
    }
}



 

 





file upload

clock September 24, 2012 21:24 by author Administrator

http://www.raistudies.com/spring/spring-mvc/file-upload-spring-mvc-annotation/

 

http://www.techiegyan.com/2009/04/20/limiting-upload-file-size-spring/

 

http://www.techiegyan.com/2008/06/09/ajax-file-upload-jquery-plugin/

 

http://stackoverflow.com/questions/4029583/using-spring-3-exceptionhandler-with-commons-fileupload-and-sizelimitexceededex

 

http://stackoverflow.com/questions/12076818/file-upload-with-spring-mvc

http://springsourcery.org/springSourcery/content/viewContent,1,32.html

 

http://www.codeproject.com/Articles/460142/ASP-NET-Multiple-File-Upload-With-Drag-Drop-and-Pr



About the author

 Welcome to this web site . This page has two purposes: Sharing information about my professional life such as articles, presentations, etc.
This website is also a place where I would like to share content I enjoy with the rest of the world. Feel free to take a look around, read my blog


Java,J2EE,Spring Framework,JQuery,

Hibernate,NoSql,Cloud,SOA,Rest WebService and Web Stack tech...

RecentPosts

Month List

Sign In