Skip to content

Instantly share code, notes, and snippets.

Created January 25, 2015 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/3acd784ff2719bc4e1d9 to your computer and use it in GitHub Desktop.
Save anonymous/3acd784ff2719bc4e1d9 to your computer and use it in GitHub Desktop.
using System;
using NHibernate;
using NHibernate.Cfg;
namespace HassanFaghihi.StatesInstitution.BusinessTier.Util.Hibernate
{
public class HibernateUtil
{
private HibernateUtil(){}
private static readonly ISessionFactory SessionFactory = BuildSessionFactory();
private static readonly ISession Session = GetOpenSession();
private static ISession GetOpenSession()
{
return SessionFactory.OpenSession();
}
private static ISessionFactory BuildSessionFactory()
{
try
{
var config = new Configuration();
config.Configure("hibernate.cfg.xml");
return config.BuildSessionFactory();
}
catch (Exception ex)
{
Console.WriteLine("Initial SessionFactory creation failed." + ex);
throw new TypeInitializationException(typeof (ISessionFactory).FullName, ex);
}
}
public static void PreInitialize()
{
GetSessionFactory();
}
internal static ISessionFactory GetSessionFactory()
{
return SessionFactory;
}
internal static ISession GetSession()
{
return Session;
}
public static void Shutdown()
{
GetSessionFactory().Close();
}
}
}
using System;
using System.Collections;
using HassanFaghihi.StatesInstitution.BusinessTier.Util.Hibernate;
using NHibernate;
namespace HassanFaghihi.StatesInstitution.BusinessTier.Dao
{
/**
* Created with IntelliJ IDEA.
* User: Hassan
* Date: 1/9/15
* Time: 2:15 AM
* To change this template use File | Settings | File Templates.
*/
internal class BaseDao
{
public void Save(Object obj)
{
//ISession session = HibernateUtil.GetSessionFactory().OpenSession();
ISession session = HibernateUtil.GetSession();
session.BeginTransaction();
session.Save(obj);
session.Transaction.Commit();
}
public void Update(Object obj)
{
//ISession session = HibernateUtil.GetSessionFactory().OpenSession();
ISession session = HibernateUtil.GetSession();
session.BeginTransaction();
session.Merge(obj);
session.Transaction.Commit();
}
public void Delete(Object obj)
{
//ISession session = HibernateUtil.GetSessionFactory().OpenSession();
ISession session = HibernateUtil.GetSession();
session.BeginTransaction();
session.Delete(obj);
session.Transaction.Commit();
}
public void SaveOrUpdate(Object obj)
{
//ISession session = HibernateUtil.GetSessionFactory().OpenSession();
ISession session = HibernateUtil.GetSession();
session.BeginTransaction();
session.SaveOrUpdate(obj);
session.Transaction.Commit();
}
public Object Get(Type t, object id)
{
//ISession session = HibernateUtil.GetSessionFactory().OpenSession();
ISession session = HibernateUtil.GetSession();
session.BeginTransaction();
Object obj = session.Get(t, id);
session.Transaction.Commit();
return obj;
}
public IList Get(Type type, String where, String orderby, int limit)
{
//ISession session = HibernateUtil.GetSessionFactory().OpenSession();
ISession session = HibernateUtil.GetSession();
session.BeginTransaction();
String query = "from " + type.Name;
if (where.Length > 0)
query += " where " + where;
if (orderby.Length > 0)
query += " order by " + orderby;
Console.WriteLine("BaseDao-Query: " + query);
//ArrayList<User> list = (ArrayList<User>)session.createQuery("from User").list();
IList list = session.CreateQuery(query).SetMaxResults(limit).List();
session.Transaction.Commit();
return list;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment