0

我正在尝试获取一组特定的数据,同时将 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);

返回 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 个。我需要将 Import 添加到所有 4 个吗?或者有什么可以告诉 AR 它是一个只读的 DTO 类?或者我做这一切都错了,有一种非常简单的方法可以做我想做的事情。

蒂亚!

4

2 回答 2

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

于 2009-03-25T09:11:35.227 回答
1

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

你最好的办法是做一个投影查询,然后有一个方法将返回的元组映射到你的类型的实例中。

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

于 2009-03-25T08:54:43.677 回答