Nhibernate/Hibernate、查找表和对象设计
我有两张桌子。包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的目标是(如您所说)避免两次查询,您可以使用单个 HQL 语句检索数据:
...如 hibernate 文档。这应该只执行一个 SQL select 语句并检索所有内容,而不需要任何映射更改。
这是一个常规的 HQL 查询,执行方式如下:
有关 hibernate 查询语言的更多信息,请参阅 页面。
If your goal is (as you said) to avoid two queries, you can retrieve the data using a single HQL statement:
...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:
More information is available on the hibernate query language page.