Created
September 19, 2017 20:00
-
-
Save thaylongs/6b4e8d4d5f29797b29b6dd41dc458932 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.dao.GenericDAO; | |
import br.com.dao.CriarConecao; | |
import br.com.hibernate.HibernateUtil; | |
import java.sql.Connection; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.hibernate.Criteria; | |
import org.hibernate.HibernateException; | |
import org.hibernate.Query; | |
import org.hibernate.Session; | |
import org.hibernate.Transaction; | |
/** | |
* | |
* @author Thaylon Guede Santos | |
* @email [email protected] | |
*/ | |
public class GenericDAO<T> { | |
Class obj_Class = null; | |
Session session = null; | |
Connection con = null; | |
Statement stmt = null; | |
Transaction tx = null; | |
public GenericDAO(Class obj_Class) { | |
this.obj_Class = obj_Class; | |
} | |
public void save(T entity) { | |
if (openSessionAndBeginTransaction()) { | |
session.save(entity); | |
commit(); | |
closeSession(); | |
} | |
} | |
public void delete(T entity) { | |
if (openSessionAndBeginTransaction()) { | |
session.delete(entity); | |
commit(); | |
closeSession(); | |
} | |
} | |
public void update(T entity) { | |
if (openSessionAndBeginTransaction()) { | |
session.update(entity); | |
commit(); | |
closeSession(); | |
} | |
} | |
public void save(ArrayList<T> entity) { | |
if (openSessionAndBeginTransaction()) { | |
entity.stream().forEach((a) -> { | |
session.save(a); | |
}); | |
commit(); | |
closeSession(); | |
} | |
} | |
public void delete(ArrayList<T> entity) { | |
if (openSessionAndBeginTransaction()) { | |
entity.stream().forEach((a) -> { | |
session.delete(a); | |
}); | |
commit(); | |
closeSession(); | |
} | |
} | |
public void update(ArrayList<T> entity) { | |
if (openSessionAndBeginTransaction()) { | |
entity.stream().forEach((a) -> { | |
session.update(a); | |
}); | |
commit(); | |
closeSession(); | |
} | |
} | |
public void update(List<T> entity) { | |
if (openSessionAndBeginTransaction()) { | |
entity.stream().forEach((a) -> { | |
session.update(a); | |
}); | |
commit(); | |
closeSession(); | |
} | |
} | |
public List<T> listAll() { | |
if (openSession()) { | |
try { | |
Criteria cri = session.createCriteria(obj_Class); | |
return cri.list(); | |
} catch (HibernateException e) { | |
System.out.println("Erro ao retornar lista: " + e.getMessage()); | |
} finally { | |
closeSession(); | |
} | |
closeSession(); | |
} | |
return null; | |
} | |
public List<T> listAll_SQL(String Sql) { | |
if (openSession()) { | |
session = HibernateUtil.getSessionFactory().openSession(); | |
try { | |
return (List<T>) session.createSQLQuery(Sql).addEntity(obj_Class).list(); | |
} catch (HibernateException e) { | |
System.out.println("Erro ao retornar lista: " + e.getMessage()); | |
} finally { | |
session.close(); | |
} | |
return null; | |
} | |
return null; | |
} | |
public List<T> listAll_HQL(String HQL) { | |
if (openSession()) { | |
try { | |
Query query = session.createQuery(HQL); | |
tx.commit(); | |
List<T> results = query.list(); | |
if (results == null || results.isEmpty()) { | |
return null; | |
} | |
return results; | |
} catch (HibernateException e) { | |
System.out.println("Erro ao execultar hql" + e.getMessage()); | |
} finally { | |
closeSession(); | |
} | |
} | |
return null; | |
} | |
private void closeSession() { | |
if (session != null && session.isOpen()) { | |
session.close(); | |
} | |
} | |
private boolean openSession() { | |
try { | |
session = HibernateUtil.getSessionFactory().openSession(); | |
return true; | |
} catch (HibernateException e) { | |
System.out.println("Erro ao abrir sessao: " + e.getMessage()); | |
} | |
return false; | |
} | |
private boolean openSessionAndBeginTransaction() { | |
try { | |
session = HibernateUtil.getSessionFactory().openSession(); | |
tx = session.beginTransaction(); | |
return true; | |
} catch (HibernateException e) { | |
System.out.println("Erro ao abrir sessao: " + e.getMessage()); | |
} | |
return false; | |
} | |
private void commit() { | |
tx.commit(); | |
} | |
public void execulte_pure_sql(String sql, int cont_atual, SQL_Pure_SQL r) { | |
sql+=";"; | |
try { | |
con = CriarConecao.getConecao(); | |
stmt = con.createStatement(); | |
final ResultSet rs = stmt.executeQuery(sql); | |
int cont = cont_atual - 1; | |
while (rs.next()) { | |
r.Result_set(rs, cont); | |
cont++; | |
} | |
} catch (ClassNotFoundException | SQLException e) { | |
System.err.println("Erro ao execultar sql: " + e.getMessage()); | |
} finally { | |
close_pure_sql(); | |
} | |
} | |
public int execulte_pure_sql(String sql, PURE_SQL_COUT r) { | |
System.out.println(sql + ";"); | |
try { | |
con = CriarConecao.getConecao(); | |
stmt = con.createStatement(); | |
final ResultSet rs = stmt.executeQuery(sql); | |
while (rs.next()) { | |
return r.cout(rs); | |
} | |
} catch (ClassNotFoundException | SQLException e) { | |
System.err.println("Erro ao execultar sql: " + e.getMessage()); | |
} finally { | |
close_pure_sql(); | |
} | |
return 0; | |
} | |
public void close_pure_sql() { | |
try { | |
if (con != null) { | |
con.close(); | |
} | |
if (stmt != null) { | |
stmt.close(); | |
} | |
} catch (SQLException e) { | |
System.err.println("Erro: " + e.getMessage()); | |
} | |
} | |
public interface PURE_SQL_COUT { | |
int cout(ResultSet rs); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.dao.GenericDAO; | |
import java.sql.ResultSet; | |
/** | |
* | |
* @author Thaylon Guede Santos | |
* @email [email protected] | |
*/ | |
@FunctionalInterface | |
public interface SQL_Pure_SQL { | |
public void Result_set(ResultSet rs,int index); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment