Skip to content

Instantly share code, notes, and snippets.

@cbmeeks
Last active September 25, 2015 04:57
Show Gist options
  • Save cbmeeks/866389 to your computer and use it in GitHub Desktop.
Save cbmeeks/866389 to your computer and use it in GitHub Desktop.
/*
* Name: AbstractRepository
* Purpose: To abstract out the Repository interface so that each implementation (ex: UserRepository)
* doesn't have to implement every method.
*
*/
package com.example.dao;
import com.example.struts.utilities.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
*
* @author Cecil.Meeks
*/
public abstract class AbstractRepository<T> implements Repository<T> {
String errorMessage = "Not supported yet";
String className = "";
List<Like> likeList;
public void SetClassName(String className) {
this.className = className;
this.likeList = new ArrayList<Like>();
}
public String GetClassName() {
return this.className;
}
// Session factory and sessions
public SessionFactory getSessionFactory() {
SessionFactory factory = HibernateUtil.getSessionFactory();
return factory;
}
public Session getSessionFromFactory() {
return getSessionFactory().getCurrentSession();
}
// Query building
public AbstractRepository<T> like(String column, String value) {
if (likeList != null) {
likeList.add(new Like(column, value));
}
return this;
}
public List<T> select() {
List<T> objects = new ArrayList<T>();
String hql = "from " + className + " " + className.toLowerCase();
try {
Session session = getSessionFromFactory();
Transaction trans = session.getTransaction();
trans.begin();
// Build LIKE
hql += BuildLIKEString(hql);
// Build query
Query query = session.createQuery(hql);
// Set parameters
if (likeList.size() > 0) {
for (Like like : likeList) {
query.setParameter(like.getColumnName(), like.getValue());
}
}
objects = query.list();
trans.commit();
} catch (Exception ex) {
for(StackTraceElement elem : ex.getStackTrace()){
System.out.println("StackTrace Element (Exception): " + elem.toString());
System.out.println("StackTrace Element (Class Name): " + elem.getClassName());
System.out.println("StackTrace Element (Method Name): " + elem.getMethodName());
System.out.println("StackTrace Element (Line Number): " + elem.getLineNumber());
}
}
return objects;
}
private String BuildLIKEString(String queryString) {
String qs = "";
boolean first = true;
if (likeList.size() > 0) {
for (Like like : likeList) {
if (first) {
first = false;
qs += " where ";
} else {
qs += " and ";
}
qs += className.toLowerCase() + "." + like.getColumnName() + " like :" + like.getColumnName();
}
}
return qs;
}
// Fetching shortcuts
public Object first() {
throw new UnsupportedOperationException(errorMessage);
}
public List<T> findAll() {
throw new UnsupportedOperationException(errorMessage);
}
// Insert / Updates
public int insert(Object dataBean) {
Integer ret = 0;
Session session = getSessionFromFactory();
session.beginTransaction();
Transaction trans = session.getTransaction();
try {
session.save(dataBean);
trans.commit();
} catch (Exception ex) {
trans.rollback();
ret = -1;
}
return ret;
}
public int update(Object dataBean) {
Integer ret = 0;
Session session = getSessionFromFactory();
session.beginTransaction();
Transaction trans = session.getTransaction();
try {
session.update(dataBean);
trans.commit();
} catch (Exception ex) {
trans.rollback();
ret = -1;
}
return ret;
}
public int delete(Object dataBean) {
Integer ret = 0;
Session session = getSessionFromFactory();
session.beginTransaction();
Transaction trans = session.getTransaction();
try {
session.delete(dataBean);
trans.commit();
} catch (Exception ex) {
trans.rollback();
ret = -1;
}
return ret;
}
}
StackTrace Element (Exception): $Proxy0.createQuery(Unknown Source)
StackTrace Element (Class Name): $Proxy0
StackTrace Element (Method Name): createQuery
StackTrace Element (Line Number): -1
StackTrace Element (Exception): com.example.dao.AbstractRepository.select(AbstractRepository.java:71)
StackTrace Element (Class Name): com.example.dao.AbstractRepository
StackTrace Element (Method Name): select
StackTrace Element (Line Number): 71
StackTrace Element (Exception): com.example.models.TestSites.main(TestSites.java:52)
StackTrace Element (Class Name): com.example.models.TestSites
StackTrace Element (Method Name): main
StackTrace Element (Line Number): 52
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.example.models;
import java.util.UUID;
/**
*
* @author Cecil.Meeks
*/
public class State {
private String stateKey;
private String stateCode;
private String name;
private String countryCodeKey;
private String accountingStateCode;
public State() {
}
// ---------------------------------------------------------------------------------------------------------
// ID (Key)
// ---------------------------------------------------------------------------------------------------------
public String getStateKey() {
return stateKey;
}
private void setStateKey(String stateKey) {
this.stateKey = stateKey;
}
// ---------------------------------------------------------------------------------------------------------
// StateCode
// ---------------------------------------------------------------------------------------------------------
public String getStateCode() {
return stateCode;
}
public void setStateCode(String stateCode) {
this.stateCode = stateCode;
}
// ---------------------------------------------------------------------------------------------------------
// Name
// ---------------------------------------------------------------------------------------------------------
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// ---------------------------------------------------------------------------------------------------------
// CountryCodeKey
// ---------------------------------------------------------------------------------------------------------
public String getCountryCodeKey() {
return countryCodeKey;
}
public UUID getCountryCodeKeyAsUUID() {
return UUID.fromString( countryCodeKey );
}
public void setCountryCodeKey(String countryCodeKey) {
this.countryCodeKey = countryCodeKey;
}
// ---------------------------------------------------------------------------------------------------------
// AccountingStateCode
// ---------------------------------------------------------------------------------------------------------
public String getAccountingStateCode() {
return accountingStateCode;
}
public void setAccountingStateCode(String accountingStateCode) {
this.accountingStateCode = accountingStateCode;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.example.models;
import com.example.dao.repositories.*;
import java.util.List;
import java.util.UUID;
/**
*
* @author Cecil.Meeks
*/
public class TestSites {
public static void main(String[] args) {
StateRepository stateRepo = new StateRepository();
// Example 4
// Search for states using LIKE
// List<State> states = stateRepo.like("StateCode", "T%").select()
List<State> states = stateRepo.like("StateCode", "%TN").select();
System.out.println("Count: " + states.size() );
for(State state : states){
System.out.println("State: " + state.getName());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment