i got problem with load and evict in session of hibernate here are the codes.
public virtual void ClearData(T obj)
{
using (ISession ses = SessionManager.OpenSession())
{
ses.Evict(obj);
}
}
public virtual T Load<T>(object id)
{
using (ISession ses = SessionManager.OpenSession())
{
return (T)ses.Load(typeof(T), id);
}
}
calling it with
Firmy fir = new Firmy();
fir.ClearData(fir);
var yol = fir.Load<Firmy>(6);
Response.Write("<br/><br/><br/> TEST get");
Response.Write(yol.NazwaFirmy);
Response.Write("<br/><br/><br/> TEST EVI");
fir.ClearData(yol);
Response.Write(yol.NazwaFirmy);
and here is session menager
public class SessionManager
{
#region Class Member Declarations
private static readonly ISessionFactory _sessionFactory;
private static readonly Configuration _configuration;
#endregion
static SessionManager()
{
_configuration = new Configuration();
_configuration.Configure();
_configuration.AddAssembly(typeof(SessionManager).Assembly);
_sessionFactory = _configuration.BuildSessionFactory();
}
public static ISession OpenSession()
{
return _sessionFactory.OpenSession();
}
#region Class Property Declarations
public static ISessionFactory SessionFactory
{
get { return _sessionFactory; }
}
#endregion
}
I wanna to load some data of fir with load function and then clear data with cleardata but idk how to do this was based on some tutroial.
Where do you exaclty get the error?
A couple of things that are not ok:
You’re calling evict (first time) for an object that is not related to any nhibernate session (you just created it). Evict is for detaching an object from a session, but only make sense if you loaded the object with that session.
You create a session on every opearation and that’s not the recommended way to go. You load an object with one session and then you try to evict it on a different session and that’s not possible.