如何获取使用Castle的ActiveRecord查询多个实体的强类型集合?

时间:2009-03-25 06:03:22

标签: nhibernate hql castle-activerecord

我正在尝试获取一组特定的数据,同时将4个不同的实体连接在一起。我所做的是设置一个DTO来试图让这个工作:

public class LatestThread
{
    private readonly string comment;
    private readonly DateTime posted;
    private readonly string userName;
    private readonly int reputation;
    private readonly int threadId;
    private readonly string topic;
    private readonly int userId;
    private readonly string avatar;

    public LatestThread(string comment, DateTime posted, string userName, int reputation, int threadId, string topic, int userId, string avatar)
    {
        this.comment = comment;
        this.avatar = avatar;
        this.userId = userId;
        this.topic = topic;
        this.threadId = threadId;
        this.reputation = reputation;
        this.userName = userName;
        this.posted = posted;
    }

    public string Comment
    {
        get { return comment; }
    }

    public DateTime Posted
    {
        get { return posted; }
    }

    public string UserName
    {
        get { return userName; }
    }

    public int Reputation
    {
        get { return reputation; }
    }

    public int ThreadId
    {
        get { return threadId; }
    }

    public string Topic
    {
        get { return topic; }
    }

    public int UserId
    {
        get { return userId; }
    }

    public string Avatar
    {
        get { return avatar; }
    }
}

现在我想我可以像这样使用SimpleQuery:

string hql = string.Format("select new LatestThread(m.Comment, m.Posted, u.UserName, u.Reputation, t.Id, t.Topic, u.Id, u.Avatar) from Thread as t inner join Message as m on t.Id = m.ThreadId inner join User as u on u.Id = m.PostedById inner join Activity as a on a.Id = t.ActivityId where a.Lineage like '{0}%' order by t.LastPosted desc", activityLineage);

return repository.SimpleQuery(0,10,hql);

我的存储库方法如下:

    public virtual IList<T> SimpleQuery<T>(int firstResult, int maxResults, string hql, params object[] parameters)
    {
        var query = new SimpleQuery<T>(hql, parameters);
        query.SetQueryRange(firstResult, maxResults);
        return query.Execute();
    }

现在要求我将[ActiveRecord]放在LatestThread类的顶部。当我这样做时,它需要一个主键,而这似乎是错误的路径。

我还读取了一些位,这些位引用了不属于DTO的类的Import属性。在所有的例子中,虽然它只是两个实体加入,而不是我拥有的4个实体。我需要将导入添加到所有4吗?或者有什么可以告诉AR这是一个只读的DTO课程吗?或者我这样做是错的,并且有一种非常简单的方法可以做我想做的事。

TIA!

2 个答案:

答案 0 :(得分:2)

将Import属性添加到新的Thread类

[Import(typeof(LatestThread), "LatestThread")]
[ActiveRecord("Thread")]
public class Thread : ActiveRecordBase<Thread> { /* blah blah */ }

然后,查询魔法发生:)

string hql = string.Format("select new LatestThread(m.Comment, m.Posted, u.UserName, u.Reputation, t.Id, t.Topic, u.Id, u.Avatar) from Thread as t inner join Message as m on t.Id = m.ThreadId inner join User as u on u.Id = m.PostedById inner join Activity as a on a.Id = t.ActivityId where a.Lineage like '{0}%' order by t.LastPosted desc", activityLineage);

SimpleQuery<LatestThread> query = new  SimpleQuery<LatestThread>(typeof(Thread), hql );  
LatestThread[] results = query.Execute()

来源:http://www.kenegozi.com/Blog/2006/10/08/projection-using-activerecords-importattribute-and-hqls-select-new-clause.aspx

答案 1 :(得分:1)

您无法查询未映射的类型([ActiveRecord]属性的作用)。 AFAIK你不能让NHibernate通过HQL创建一个新的任意对象实例(如果有人不知道的话我会得到纠正)。

您最好的选择是进行投影查询,然后使用方法将返回的元组映射到您的类型的实例中。

我的回答here显示了如何进行投影查询并将其映射到匿名类型;你想做的事情并没有太大的不同。然后,您可以在类型特定的存储库或通用存储库的强类型扩展方法中执行此方法。