Hello,
I am testing the v3.0 Alpha 2 release in regards to the new property
lazy loading capability. In general, I have property that is a BLOB
in the database (the property is Body). I don't want the BLOB content
loaded in the object that gets returned from the query. This is what
I have defined:
Class
--------
public class RptSource {
public RptSource() { }
public virtual long SourceId { get; set; }
public virtual string Name { get; set; }
public virtual string BodyFormat { get; set; }
public virtual byte[] Body { get; set; }
}
Mapping file (notice the lazy="true" on ther Body property)
-----------------
<hibernate-mapping assembly="IcebergMain"
namespace="IcebergMain.Domain" xmlns="urn:nhibernate-mapping-2.2">
<class name="RptSource" table="RPT_SOURCE" lazy="true" >
<id name="SourceId" type="Int64" column="SOURCE_ID">
<generator class="sequence">
<param name="sequence">RPT_SOURCE_SEQ</param>
</generator>
</id>
<property name="Name" column="NAME" />
<property name="BodyFormat" column="BODY_FORMAT" />
<property name="Body" column="BODY" lazy="true"/>
</class>
</hibernate-mapping>
Query (notice the call to the NHibernateHelper class to get a session)
---------
public RptSource GetSourceById(long sourceid) {
using (ISession session = NHibernateHelper.OpenSession()) {
RptSource src =
session.CreateCriteria(typeof(RptSource)).Add(Restrictions.Eq("SourceId",
sourceid)).UniqueResult<RptSource>();
return src;
}
}
NHibernateHelper Class
----------------------------------
class NHibernateHelper {
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory {
get {
if (_sessionFactory == null) {
var configuration = new Configuration();
configuration.Configure();
// Only make AddAssembly call once in our case.
This should load all of the .hbm.xml mapping files.
configuration.AddAssembly(typeof
(RptSourceGroup).Assembly);
_sessionFactory =
configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession() { return
SessionFactory.OpenSession(); }
}
So, when the query is run, I do in fact get a result that does not
include the BODY. All is well at this point. This is working as I
understood it would. The problem is when I try to get the Body (BLOB)
from the object. In a previous article I found that described the
property lazy loading, it indicated that the lazy loaded property
(Body in this case) would be retrieved only after it is explicitly
aske for. This would be, for example, when the code did something
like:
byte[] barr = src.Body;
When I do this, however, I get the error:
Initializing[Iceberg.Domain.RptSource#]-session is not connected.
entity-name:'Iceberg.Domain.RptSource' property:'Body'
It seems that the session used for the query was destroyed after the
query and by the time I asked for the Body, there was no session to
make the connection. My question then is how would I create and use
a new session when I want to get the blob (ie, what is a valid
strategy for doing this type of thing)? Would I do something like:
using (ISession session = NHibernateHelper.OpenSession()) {
byte[] barr = src.Body;
}
Any thoughts would be greatly appreciated.
Thanks - Peter
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.