-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Session and objects
PostPosted: Fri May 04, 2007 8:35 am 
Newbie

Joined: Wed May 02, 2007 12:56 pm
Posts: 17
I´ve got the following object

class Inc{
public int Id{
set{
}
get{
}
}
public Aplicacion Aplicacion{
set{
}
get{
}
}
}

public Aplicacion {
public int Id{
}
public string Nombre{
}
}

When i want to insert a new Aplicacion into Inc I just do:
Aplicacion ap = new Aplicacion();
ap.Id=9;
inc.Aplicacion = ap;
sesion.SaveOrUpdate(inc);

The problem is that at once I want to show the Nombre of Aplicacion but it´s not possible because i haven´t inserted it but i know that is in the DB. How can i do to show the Nombre? May I Initialize Aplicacion?

Best regards,

Gato


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 10:08 am 
Newbie

Joined: Wed Dec 20, 2006 12:34 pm
Posts: 13
Can you supply mapping files and be more clear as to the behavior of the nombre field. Is it database generated? Is there a many-to-one relation?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 06, 2007 3:18 pm 
Newbie

Joined: Wed May 02, 2007 12:56 pm
Posts: 17
It´s a1 to n relation. I´ve been trying to destroy dthe session for that object but i cannot.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 10:19 am 
Newbie

Joined: Wed May 02, 2007 12:56 pm
Posts: 17
I attach the mappings files:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" auto-import="true">
<class name="com.empresa.db.dto.Incidencia,Logica" table="Incidencia" lazy="false" >
<id name="Id" column="id" type="System.Int32" unsaved-value="0">
<generator class="native" />
</id>

<bag name="Acciones" table="Accion" inverse="true" lazy="true" order-by="fecha" cascade="all-delete-orphan">
<key column="IdIncidencia"/>
<one-to-many class="com.empresa.db.dto.Accion,Logica"/>
</bag>

</class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" auto-import="true">
<class name="com.empresa.db.dto.Accion, Logica" table="ACCION" lazy="false" >
<id name="Id" column="idAccion" type="System.Int32" unsaved-value="0">
<generator class="native" />
</id>

<many-to-one name="Estado" class="com.empresa.db.dto.Estado,Logica" column="idEstado"/>
<many-to-one name="Incidencia" class="com.empresa.db.dto.Incidencia, Logica" column ="idIncidencia"/>
</class>
</hibernate-mapping>




<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" auto-import="true">
<class name="com.empresa.db.dto.Estado, Logica" table="ESTADO_INCIDENCIA" lazy="false" >
<id name="IdEstado" column="id" type="System.Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Descripcion" column="descripcion"/>
<property name="Nombre" column ="nombre"/>
</class>
</hibernate-mapping>

The code is:

accion.Estado.Id = ESTADOS.NUEVA;
incidencia.Acciones.Add(accion);

I saved in the DB, but i would like to show immediately the Descripcion of the Estado that is always stored in the database so i do :
incidencia.Accion:Estado.Description i get a null because the property is not filled. What can i do? If i do a get or load the data is get from the cache. I have tried the clean the sesion without success.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 10:40 am 
Newbie

Joined: Wed Dec 20, 2006 12:34 pm
Posts: 13
borjagat wrote:
I attach the mappings files:

The code is:

accion.Estado.Id = ESTADOS.NUEVA;
incidencia.Acciones.Add(accion);

I saved in the DB, but i would like to show immediately the Descripcion of the Estado that is always stored in the database so i do :
incidencia.Accion:Estado.Description i get a null because the property is not filled. What can i do? If i do a get or load the data is get from the cache. I have tried the clean the sesion without success.


Try session.refresh(incidencia)...

If this doesn't work please include all code between session.open and session.close.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 1:05 pm 
Newbie

Joined: Wed May 02, 2007 12:56 pm
Posts: 17
I´ve tried and not success. For opening the sessión and for clossing the session i use this code.
using System;
using System.Web;
/*using Com.Benday.BugTracker.DataAccess;*/
using NHibernate;
using NHibernate.Cfg;

namespace com.tinsa.utils.nhibernate
{
/// <summary>
/// Summary description for NHibernateHttpModule.
/// </summary>
public class NHibernateHttpModule : IHttpModule
{
// this is only used if not running in HttpModule mode
protected static ISessionFactory m_factory;

// this is only used if not running in HttpModule mode
private static ISession m_session;

private static readonly string KEY_NHIBERNATE_FACTORY = "NHibernateSessionFactory";
private static readonly string KEY_NHIBERNATE_SESSION = "NHibernateSession";


public void Init(HttpApplication context)
{
log4net.Config.XmlConfigurator.Configure();
context.EndRequest += new EventHandler(context_EndRequest);
}

public void Dispose()
{
if (m_session != null)
{
m_session.Close();
m_session.Dispose();
}

if (m_factory != null)
{
m_factory.Close();
}
}

private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;

context.Items[KEY_NHIBERNATE_SESSION] = CreateSession();
}

private void context_EndRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;

ISession session = context.Items[KEY_NHIBERNATE_SESSION] as ISession;
if (session != null)
{
try
{
session.Flush();
session.Close();
}
catch { }
}

context.Items[KEY_NHIBERNATE_SESSION] = null;
}

protected static ISessionFactory CreateSessionFactory()
{
Configuration config;
ISessionFactory factory;

config = new Configuration();

if (config == null)
{
throw new InvalidOperationException("NHibernate configuration is null.");
}

config.Configure();

factory = config.BuildSessionFactory();

if (factory == null)
{
throw new InvalidOperationException("Call to Configuration.BuildSessionFactory() returned null.");
}
else
{
return factory;
}
}

public static ISessionFactory CurrentFactory
{
get
{
if (HttpContext.Current == null)
{
// running without an HttpContext (non-web mode)
// the nhibernate session is a singleton in the app domain
if (m_factory != null)
{
return m_factory;
}
else
{
m_factory = CreateSessionFactory();

return m_factory;
}
}
else
{
// running inside of an HttpContext (web mode)
// the nhibernate session is a singleton to the http request
HttpContext currentContext = HttpContext.Current;

ISessionFactory factory = currentContext.Application[KEY_NHIBERNATE_FACTORY] as ISessionFactory;

if (factory == null)
{
factory = CreateSessionFactory();
currentContext.Application[KEY_NHIBERNATE_FACTORY] = factory;
}

return factory;
}
}
}

public static ISession CreateSession()
{
ISessionFactory factory;
ISession session;

factory = NHibernateHttpModule.CurrentFactory;

if (factory == null)
{
throw new InvalidOperationException("Call to Configuration.BuildSessionFactory() returned null.");
}

session = factory.OpenSession();

if (session == null)
{
throw new InvalidOperationException("Call to factory.OpenSession() returned null.");
}

return session;
}

public static ISession CurrentSession
{
get
{

if (HttpContext.Current == null)
{
// running without an HttpContext (non-web mode)
// the nhibernate session is a singleton in the app domain
if (m_session != null)
{
return m_session;
}
else
{
m_session = CreateSession();

return m_session;
}
}
else
{
// running inside of an HttpContext (web mode)
// the nhibernate session is a singleton to the http request
HttpContext currentContext = HttpContext.Current;

ISession session = currentContext.Items[KEY_NHIBERNATE_SESSION] as ISession;

if (session == null)
{
session = CreateSession();
currentContext.Items[KEY_NHIBERNATE_SESSION] = session;
}

return session;
}
}
}

}

}



}


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 1:02 am 
Newbie

Joined: Wed Dec 20, 2006 12:34 pm
Posts: 13
What about where you actually call session.save(). What db are you using? I wish i could help but i need more information. Nothing is jumping out at me.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 3:13 am 
Newbie

Joined: Wed May 02, 2007 12:56 pm
Posts: 17
The database is SqlServer 2000.



public void insertarIncidencia(Incidencia incidencia)
{
string localizator = "[" + className + ":insertarIncidencia]:";
ITransaction tx = null;
try
{
ISession sesion = NHibernateHttpModule.CurrentSession;
tx = sesion.BeginTransaction();
sesion.SaveOrUpdate(incidencia);
tx.Commit();
return;
}
catch (Exception e)
{
if (Log.IsErrorEnabled())
Log.Error(localizator + "Error insertando la incidencia en la Base de datos." + e.Message, e);
tx.Rollback();
throw e;
}
}


public Incidencia getIncidencia(int idIncidencia)
{
string localizator = "[" + className + ":obtenerIncidencia]:";
try
{
ISession sesion = NHibernateHttpModule.CurrentSession;
Incidencia inc = (Incidencia)sesion.Load(typeof(Incidencia),idIncidencia);

//se obtiene los campos sin inicializar(los lazys)
NHibernateUtil.Initialize(inc.Imagenes);
NHibernateUtil.Initialize(inc.Acciones);
NHibernateUtil.Initialize(inc.Documentos);

return inc;
}
catch (Exception e)
{
if (Log.IsErrorEnabled())
Log.Error(localizator + " Error obteniendo la incidencia:" + e.Message, e);
throw e;
}
}
public Incidencia refreshIncidencia(Incidencia incidencia)
{
string localizator = "[" + className + ":refrescarIncidencia]:";
try
{
//int idIncidencia = incidencia.Id;
/*ISession sesion = NHibernateHttpModule.CurrentSession;
sesion.Refresh(incidencia.Acciones);*/
//TODO: refrescar la colección acciones
return obtenerIncidencia(incidencia.Id);
return incidencia;
//return obtenerIncidencia(idIncidencia);
}
catch (Exception e)
{
if (Log.IsErrorEnabled())
Log.Error(localizator + " Error obteniendo la incidencia:" + e.Message, e);
throw e;
}
}

The last function is comment because i cannot refresh the incidencia. I hae tried evict, refresh, clear,...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.