Nhibernate/Hibernate、查找表和对象设计

发布于 2024-08-24 22:55:35 字数 1484 浏览 5 评论 0原文

我有两张桌子。包含 CustomerID、InvoiceDate、Value、InvoiceTypeID 列(CustomerID 和 InvoiceDate 组成组合键)的发票以及包含 InvoiceTypeID 和 InvoiceTypeName 列的 InvoiceType。

我知道我可以创建我的对象,例如:

public class Invoice
{
    public virtual int CustomerID { get; set; }
    public virtual DateTime InvoiceDate { get; set; }
    public virtual decimal Value { get; set; }
    public virtual InvoiceType InvoiceType { get; set; }
}

public class InvoiceType
{
    public virtual InvoiceTypeID { get; set; }
    public virtual InvoiceTypeName { get; set; }
}

因此生成的 sql 看起来像:

SELECT CustomerID, InvoiceDate, Value, InvoiceTypeID FROM Invoice WHERE CustomerID = x AND InvoiceDate = y
SELECT InvoiceTypeID, InvoiceTypeName FROM InvoiceType WHERE InvoiceTypeID = z

但与其执行两个选择查询来检索数据,我宁愿有一个。我还想避免使用子对象进行简单的查找列表。所以我的对象看起来像:

public class Invoice
{
    public virtual int CustomerID { get; set; }
    public virtual DateTime InvoiceDate { get; set; }
    public virtual decimal Value { get; set; }
    public virtual InvoiceTypeID { get; set; }
    public virtual InvoiceTypeName { get; set; }
}

我的 sql 看起来像:

SELECT CustomerID, InvoiceDate, Value, InvoiceTypeID 
FROM Invoice INNER JOIN InvoiceType ON Invoice.InvoiceTypeID = InvoiceType.InvoiceTypeID
WHERE CustomerID = x AND InvoiceDate = y

我的问题是如何为此创建映射?

我尝试过使用加入,但是尝试使用 CustomerID 和 InvoiceDate 加入,我是否遗漏了一些明显的东西?

谢谢

I've got two tables. Invoice with columns CustomerID, InvoiceDate, Value, InvoiceTypeID (CustomerID and InvoiceDate make up a composite key) and InvoiceType with InvoiceTypeID and InvoiceTypeName columns.

I know I can create my objects like:

public class Invoice
{
    public virtual int CustomerID { get; set; }
    public virtual DateTime InvoiceDate { get; set; }
    public virtual decimal Value { get; set; }
    public virtual InvoiceType InvoiceType { get; set; }
}

public class InvoiceType
{
    public virtual InvoiceTypeID { get; set; }
    public virtual InvoiceTypeName { get; set; }
}

So the generated sql would look something like:

SELECT CustomerID, InvoiceDate, Value, InvoiceTypeID FROM Invoice WHERE CustomerID = x AND InvoiceDate = y
SELECT InvoiceTypeID, InvoiceTypeName FROM InvoiceType WHERE InvoiceTypeID = z

But rather that having two select queries executed to retrieve the data I would rather have one. I would also like to avoid using child object for simple lookup lists. So my object would look something like:

public class Invoice
{
    public virtual int CustomerID { get; set; }
    public virtual DateTime InvoiceDate { get; set; }
    public virtual decimal Value { get; set; }
    public virtual InvoiceTypeID { get; set; }
    public virtual InvoiceTypeName { get; set; }
}

And my sql would look something like:

SELECT CustomerID, InvoiceDate, Value, InvoiceTypeID 
FROM Invoice INNER JOIN InvoiceType ON Invoice.InvoiceTypeID = InvoiceType.InvoiceTypeID
WHERE CustomerID = x AND InvoiceDate = y

My question is how do I create the mapping for this?

I've tried using join but this tried to join using CustomerID and InvoiceDate, am I missing something obvious?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

归属感 2024-08-31 22:55:35

如果您的目标是(如您所说)避免两次查询,您可以使用单个 HQL 语句检索数据:

select i, it from Invoice i fetch join i.type it where ...

...如 hibernate 文档。这应该只执行一个 SQL select 语句并检索所有内容,而不需要任何映射更改。

这是一个常规的 HQL 查询,执行方式如下:

IQuery q = s.CreateQuery("select i, it from Invoice i fetch join i.type it where ...");
IList invoices = q.List();

有关 hibernate 查询语言的更多信息,请参阅 页面

If your goal is (as you said) to avoid two queries, you can retrieve the data using a single HQL statement:

select i, it from Invoice i fetch join i.type it where ...

...as documented in the hibernate docs. This should execute only one SQL select statement and retrieve everything without any mapping changes.

This is a regular HQL query and is executed as follows:

IQuery q = s.CreateQuery("select i, it from Invoice i fetch join i.type it where ...");
IList invoices = q.List();

More information is available on the hibernate query language page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文