Home > Hibernate, Java, Programación, snippets > Generic CRUD on Hibernate

Generic CRUD on Hibernate

November 30, 2011 Leave a comment Go to comments

Util to help manage CRUD on Hibernate


package grekz.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ModelUtil {
	@SuppressWarnings("unchecked")
	public static T findById(Class c, int id) {
		Session session = (Session) HibernateUtil.getSessionfactory()
				.openSession();
		T row = null;
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			row = (T) session.get(c, id);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return row;
	}

	@SuppressWarnings("unchecked")
	public static T findById(Class c, String id) {
		Session session = HibernateUtil.getSessionfactory().openSession();
		T row = null;
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			row = (T) session.get(c, id);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return row;
	}

	public static boolean saveMappedObject(T objToSave) {
		Session session = HibernateUtil.getSessionfactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.save(objToSave);
			transaction.commit();
			return true;
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	public static boolean updateDetachedObject(T objToUpdate) {
		Session session = HibernateUtil.getSessionfactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.merge(objToUpdate);
			transaction.commit();
			return true;
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	public static boolean deleteDetachedObject(T objToDelete) {
		Session session = HibernateUtil.getSessionfactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.delete(objToDelete);
			transaction.commit();
			return true;
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}
}


public class HibernateUtil {
	private static final SessionFactory sessionFactory;
	static {
		try {
			Configuration config = new Configuration().configure();
			sessionFactory = config.buildSessionFactory();
		} catch (Throwable t) {
			System.err.println("No se pudo crear");
			throw new ExceptionInInitializerError(t);
		}
	}

	/** * @return the sessionfactory */
	public static SessionFactory getSessionfactory() {
		return sessionFactory;
	}
}

Y una forma distinta más limpia aquí

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: