dao
package com.booksxgames.dao;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.booksxgames.entity.Products;
import com.booksxgames.exception.UserException;
import ch.qos.logback.classic.Logger;
import javax.persistence.Entity;
@Transactional
@Repository
public class BaseDAO {
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(BaseDAO.class);
@Autowired
protected SessionFactory sessionFactory;
public <T> List<T> getList(String queryString, Map<String,Object> parameters){
List<T> retList = null;
Session session =null;
LOGGER.info("== Inside Get List =="+ queryString);
try {
session = sessionFactory.openSession();
Query query = session.createQuery(queryString);
if(parameters != null) {
for(Map.Entry<String, Object> entry : parameters.entrySet()){
String key = entry.getKey();
Object value = entry.getValue();
query.setParameter(key, value);
}
}
LOGGER.info("== Query string ==" + query.toString());
List<T> list = query.list();
LOGGER.info("== list size ==" + list.size());
retList = list;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if(session != null) {
session.clear();
session.close();
}
}
return retList;
}
public void saveOrUpdate(Object obj) {
Session session =null;
try {
session = sessionFactory.openSession();
Transaction txn = session.beginTransaction();
session.saveOrUpdate(obj);
txn.commit();
} catch (Exception ex) {
LOGGER.info("== BASEDAO Exception and throw ==");
throw ex;
} finally {
if(session != null) {
session.clear();
session.close();
}
}
}
public <T> T getFirst(String queryString, Map<String,Object> parameters) {
System.out.println();
List<T> list = getList(queryString, parameters,1) ;
//System.out.println(" getfirst list soze "+ list.toString());
if(list != null && !list.isEmpty()) {
System.out.println(" == Inside If condition ====");
return list.get(0);
}
System.out.println(" method finished");
return null;
}
private <T> List<T> getList(String queryString, Map<String, Object> parameters, int num) {
List<T> retList = null;
Session session =null;
try {
session = sessionFactory.openSession();
System.out.println(" before create query ");
Query query = session.createQuery(queryString);
if(parameters != null) {
for(Map.Entry<String, Object> entry : parameters.entrySet()){
String key = entry.getKey();
Object value = entry.getValue();
query.setParameter(key, value);
}
}
query.setMaxResults(num);
List<T> list = query.list();
System.out.println(" query result ==> " + list.size());
retList = list;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if(session != null) {
session.clear();
session.close();
}
}
return retList;
}
public void delete(Object obj) throws Exception {
Session session =null;
try {
session = sessionFactory.openSession();
Transaction txn = session.beginTransaction();
session.delete(obj);
txn.commit();
} catch (Exception ex) {
throw new Exception();
} finally {
if(session != null) {
session.clear();
session.close();
}
}
}
public void save(Object obj) throws Exception {
Session session =null;
try {
session = sessionFactory.openSession();
Transaction txn = session.beginTransaction();
session.save(obj);
txn.commit();
} catch (Exception ex) {
throw new Exception();
} finally {
if(session != null) {
session.clear();
session.close();
}
}
}
public void update(Object obj) throws Exception {
Session session =null;
try {
session = sessionFactory.openSession();
Transaction txn = session.beginTransaction();
session.update(obj);
txn.commit();
} catch (Exception ex) {
//throw new Exception();
ex.printStackTrace();
} finally {
if(session != null) {
session.clear();
session.close();
}
}
}
}
Comments
Post a Comment