Greetings all,
I'm new to NHibernate and currently seeking some help here. The problem is that I have been trying to bind a datagrid from the method ListBlogsAndRecentItems() as listed below.
Code:
public IList ListBlogsAndRecentItems()
{
IList result = null;
using (ISession session = _sessions.OpenSession())
using (ITransaction tx = session.BeginTransaction())
{
IQuery q = session.CreateQuery(
"select blog.Id, blog.Name, blogItem.Text from Blog as blog " +
"join fetch blog.Items as blogItem " +
"where blogItem.DateTime > :minDate"
);
DateTime date = DateTime.Now.AddMonths(-1);
q.SetDateTime("minDate", date);
result = q.List();
tx.Commit();
}
return result;
}
When I have the datagrid autogenerate the columns I get this as the result in my datagrid:
Code:
Rank IsReadOnly IsSynchronized IsFixedSize Length LongLength
1 False False True 3 3
I basically want to return a resultset comparable to this SQL statement:
Code:
select [blog_id], [Name], [text] from blog_items, blogs where blogs.blog_id = blog_items.blog_id
I have searched the fourms and basically read all topics regarding datagrid binding with NHibernate and have not been able to solve my problem. I've also read the NHibernate documentation on HQL, and Collection mapping with no success in resolving this issue. I'm at my wits end here and would appreciate some help. So far I like what I have seen and would like to try to do more with it so I can justify using it at work. Eventually I would like to use Template Columns instead of having the datagrid columns autogenerate. Thanks again for any help
Hibernate version: 1.0.2.0
Name and version of the database you are using:Microsoft SQL 2000
ASP.NET Framwork:1.1
Mapping documents:Blogs Mappings
Code:
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
assembly="BlogSample" namespace="BlogSample.AppDomain">
<class
name="Blog"
table="BLOGS"
lazy="true">
<id
name="Id"
column="BLOG_ID">
<generator class="native"/>
</id>
<property
name="Name"
column="NAME"
not-null="true"
unique="true"/>
<bag
name="Items"
inverse="true"
lazy="true"
order-by="DATE_TIME"
cascade="all"
fetch="join">
<key column="BLOG_ID"/>
<one-to-many class="BlogItem"/>
</bag>
</class>
</hibernate-mapping>
BlogItem mappings
Code:
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
assembly="BlogSample" namespace="BlogSample.AppDomain">
<class
name="BlogItem"
table="BLOG_ITEMS"
dynamic-update="true">
<id
name="Id"
column="BLOG_ITEM_ID">
<generator class="native"/>
</id>
<property
name="Title"
column="TITLE"
not-null="true"/>
<property
name="Text"
column="TEXT"
not-null="true"/>
<property
name="DateTime"
column="DATE_TIME"
not-null="true"/>
<many-to-one
name="Blog"
column="BLOG_ID"
not-null="true"
/>
</class>
</hibernate-mapping>
Blog.cs
Code:
using System;
using System.Collections;
namespace BlogSample.AppDomain
{
/// <summary>
/// Summary description for Blog.
/// </summary>
public class Blog
{
private long _id;
private string _name;
private IList _items;
public virtual long Id
{
get { return _id; }
set { _id = value; }
}
public virtual IList Items
{
get { return _items; }
set { _items = value; }
}
public virtual string Name
{
get { return _name; }
set { _name = value; }
}
}
}
BlogItem.cs
Code:
using System;
namespace BlogSample.AppDomain
{
/// <summary>
/// Summary description for BlogItem.
/// </summary>
public class BlogItem
{
private long _id;
private DateTime _dateTime;
private string _text;
private string _title;
private Blog _blog;
public virtual Blog Blog
{
get { return _blog; }
set { _blog = value; }
}
public virtual DateTime DateTime
{
get { return _dateTime; }
set { _dateTime = value; }
}
public virtual long Id
{
get { return _id; }
set { _id = value; }
}
public virtual string Text
{
get { return _text; }
set { _text = value; }
}
public virtual string Title
{
get { return _title; }
set { _title = value; }
}
}
}