Skip to content

Commit

Permalink
Modificando ciertas cosas
Browse files Browse the repository at this point in the history
  • Loading branch information
yogonza524 committed Mar 7, 2016
1 parent 91fc220 commit ac8c2e8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 90 deletions.
43 changes: 0 additions & 43 deletions src/main/java/com/core/controller/HibernateUtil.java

This file was deleted.

63 changes: 16 additions & 47 deletions src/main/java/com/core/controller/Kimera.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class Kimera {
*
*/
protected static SessionFactory sf;

public Kimera(SessionFactory sessionFactory) {
Kimera.sf = sessionFactory;
}

/**
* List all object in a table
Expand All @@ -40,7 +44,6 @@ public class Kimera {
* @return List of entity object for table T. Object receiver must be the type List<type>
*/
public <T> T all(Class type){
sf = HibernateUtil.getSessionFactory();
Session s = sf.openSession();
List<T> l = s.createCriteria(type).list();
for(Object o : l){
Expand All @@ -56,23 +59,20 @@ public <T> T all(Class type){
* @param id represents the id for the table. Can be an Integer, String, etc. Depends the Table design.
* @return Entity object width the fields initialized with Hibernate
*/
public <T> T byId(String key,Object id, Class type){
sf = HibernateUtil.getSessionFactory();
public <T> T entityById(String key,Object id, Class type){
Session s = sf.openSession();
T result = (T)s.createCriteria(type).add(Restrictions.eq(key, id)).setMaxResults(1).uniqueResult();
Hibernate.initialize(result);
s.close();
//result = this.initialize(result);
return result;
}

/**
* Delete obj in DataBase
* @param obj
* @return true if obj was removed successfuly. False in otherwise.
*/
public boolean remove(Object obj){
sf = HibernateUtil.getSessionFactory();
boolean resp = false;
try {
Session s = sf.openSession();
Expand All @@ -86,14 +86,12 @@ public boolean remove(Object obj){
}
return resp;
}

/**
* Persist obj in DataBase
* @param obj
* @return true if obj was added suyccessfuly- False in otherwise.
*/
public boolean add(Object obj){
sf = HibernateUtil.getSessionFactory();
boolean resp = false;
try {
Session s = sf.openSession();
Expand All @@ -114,7 +112,6 @@ public boolean add(Object obj){
* @return true if updated obj successfuly, false in otherwise
*/
public boolean update(Object obj){
sf = HibernateUtil.getSessionFactory();
boolean resp = false;
try {
Session s = sf.openSession();
Expand All @@ -138,7 +135,6 @@ public boolean update(Object obj){
* @return Last object with the key param if exists. Otherwise returns null.
*/
public <T> T getLast(String key, Class type){
sf = HibernateUtil.getSessionFactory();
T result = null;
try {
Session s = sf.openSession();
Expand All @@ -151,8 +147,7 @@ public <T> T getLast(String key, Class type){
return result;
}

public <T> T byRestrictions(List<Criterion> restrictions, Class type){
sf = HibernateUtil.getSessionFactory();
public <T> T entityByRestrictions(List<Criterion> restrictions, Class type){
Session s = sf.openSession();
Criteria cri = s.createCriteria(type);
Iterator i = restrictions.iterator();
Expand All @@ -167,7 +162,6 @@ public <T> T byRestrictions(List<Criterion> restrictions, Class type){
}

public <T> T listByRestrictions(List<Criterion> restrictions, Class type){
sf = HibernateUtil.getSessionFactory();
Session s = sf.openSession();
Criteria cri = s.createCriteria(type);
Iterator i = restrictions.iterator();
Expand Down Expand Up @@ -202,8 +196,7 @@ public <T> T initialize(Object entity){
return (T) entity;
}

public <T> T withRestrictions(Map<String,Object> restrictions, Class type){
sf = HibernateUtil.getSessionFactory();
public <T> T listWithRestrictions(Map<String,Object> restrictions, Class type){
Session s = sf.openSession();
Criteria cri = s.createCriteria(type);
for(Map.Entry<String,Object> entry : restrictions.entrySet()){
Expand All @@ -215,8 +208,7 @@ public <T> T withRestrictions(Map<String,Object> restrictions, Class type){
return (T) output;
}

public <T> T withParams(Map<String,Object> restrictions,Class type){
sf = HibernateUtil.getSessionFactory();
public <T> T entityWithParams(Map<String,Object> restrictions,Class type){
Session s = sf.openSession();
Criteria cri = s.createCriteria(type);
for(Map.Entry<String,Object> entry : restrictions.entrySet()){
Expand All @@ -228,8 +220,7 @@ public <T> T withParams(Map<String,Object> restrictions,Class type){
return output;
}

public <T> T withRestrictionsLike(Map<String,Object> restrictions, Class type){
sf = HibernateUtil.getSessionFactory();
public <T> T entityWithRestrictionsLike(Map<String,Object> restrictions, Class type){
Session s = sf.openSession();
Criteria cri = s.createCriteria(type);
for(Map.Entry<String,Object> entry : restrictions.entrySet()){
Expand All @@ -241,8 +232,7 @@ public <T> T withRestrictionsLike(Map<String,Object> restrictions, Class type){
return (T) output;
}

public <T> T withOneRestriction(String key, Object value, Class type){
sf = HibernateUtil.getSessionFactory();
public <T> T entityWithOneRestriction(String key, Object value, Class type){
Session s = sf.openSession();
Criteria cri = s.createCriteria(type);
cri.add(Restrictions.eq(key, value));
Expand All @@ -252,8 +242,8 @@ public <T> T withOneRestriction(String key, Object value, Class type){
return (T) output;
}

public <T> T byIdLike(String key, String value, Class type){
sf = HibernateUtil.getSessionFactory();
public <T> T entityByIdLike(String key, String value, Class type){

Session s = sf.openSession();
Criteria cri = s.createCriteria(type);
cri.add(Restrictions.like(key,"%" + value + "%"));
Expand All @@ -263,8 +253,7 @@ public <T> T byIdLike(String key, String value, Class type){
return (T) output;
}

public <T> T allOrderBy(String key, OrderBy order, Class type){
sf = HibernateUtil.getSessionFactory();
public <T> T listOrderBy(String key, OrderBy order, Class type){
Session s = sf.openSession();
Criteria c = s.createCriteria(type);
c.addOrder(order.equals(OrderBy.ASC)? Order.asc(key): Order.desc(key));
Expand All @@ -274,8 +263,7 @@ public <T> T allOrderBy(String key, OrderBy order, Class type){
return (T) output;
}

public <T> T withRestrictionsOrderBy(Map<String,Object> restrictions,String key, OrderBy order, Class type){
sf = HibernateUtil.getSessionFactory();
public <T> T entityWithRestrictionsOrderBy(Map<String,Object> restrictions,String key, OrderBy order, Class type){
Session s = sf.openSession();
Criteria cri = s.createCriteria(type);
for(Map.Entry<String,Object> entry : restrictions.entrySet()){
Expand All @@ -288,8 +276,7 @@ public <T> T withRestrictionsOrderBy(Map<String,Object> restrictions,String key,
return (T) output;
}

public <T> T withRestrictions(List<Criterion> restrictions, Class type){
sf = HibernateUtil.getSessionFactory();
public <T> T entityWithRestrictions(List<Criterion> restrictions, Class type){
Session s = sf.openSession();
Criteria cri = s.createCriteria(type);
Iterator i = restrictions.iterator();
Expand All @@ -304,7 +291,6 @@ public <T> T withRestrictions(List<Criterion> restrictions, Class type){
}

public List callProcedure(String query, Map<String,Object> params){
sf = HibernateUtil.getSessionFactory();
List result = null;
try {
Session s = sf.openSession();
Expand All @@ -326,8 +312,7 @@ public List callProcedure(String query, Map<String,Object> params){
return result;
}

public <T> T betweenDates(String field, Date first, Date second, Class type){
sf = HibernateUtil.getSessionFactory();
public <T> T entityBetweenDates(String field, Date first, Date second, Class type){
List<T> result = null;
try {
Session s = sf.openSession();
Expand All @@ -338,20 +323,4 @@ public <T> T betweenDates(String field, Date first, Date second, Class type){
}
return (T) result;
}
public <T> T listWithRestrictionsAndOrder(List<Criterion> restrictions, Order o, Class type){
sf = HibernateUtil.getSessionFactory();
List<T> result = null;
try {
Session s = sf.openSession();
Criteria c = s.createCriteria(type);
for(Criterion r : restrictions){
c.add(r);
}
result = c.addOrder(o).list();
s.close();
} catch (Exception e) {
e.printStackTrace();
}
return (T) result;
}
}
Empty file.
Empty file.

0 comments on commit ac8c2e8

Please sign in to comment.