HQL 查询 - Castle Active Record - 外连接
我试图从特定表的数据库中获取数据,其中没有与另一个表的连接,或者有但不是正确的数据。
结构
我有一个域表。这一切所做的只是保存一个域名和一些其他杂项元数据。
我有一个功能表,其中只有一个 ID 列和一个用于表示该功能的值的列。例如,它可能看起来像这样:
1 | First Registered
2 | Expired On
3 | Hosted On
等等
。我有一个 DomainData 表。这保存了特征的值。列是这样的。
身份证 |域名 |功能 ID |值
本质上它保存该域的特征值,类似于键值列,以便它可以在不修改域表的表结构的情况下扩展。
现在,我需要做的是对所有没有功能 X 的域进行查询。
例如,这在 SQL 中有效:
SELECT D.*
FROM Domain AS D
LEFT OUTER JOIN DomainData AS Data ON Data.DomainId = D.Id
WHERE data.featureId IS NULL OR data.FeatureId != @FeatureId
在 SQL 中工作得很好,并返回所有需要的数据。所做的就是获取所有没有任何功能的域,或者它们确实有功能,但不是我需要的功能。
现在我使用 CastleActiveRecord 作为我的数据层,但我正在努力思考如何在 HQL 中编写它。现在花了一个小时,我要么什么也没得到,要么得到所有域,无论它们的功能 ID 是什么。不幸的是我已经删除了所有我尝试过的 HQL 语句。
我可以获得有关如何将上述语句重写为 HQL 的帮助吗?
旁注:在我的类中,我在 DomainData 类中有这个:
[BelongsTo("DomainId")]
public Domain Domain { get; set; }
[BelongsTo("FeatureId")]
public Feature Feature { get; set; }
在我的域类中有这个:
private IList<DomainData> featureData = new List<DomainData>();
[HasMany(typeof(DomainData), Table = "DomainData", ColumnKey = "DomainId")]
public IList<DomainData> FeatureData
{
get { return featureData; }
set { featureData = value; }
}
我认为这是正确的数据结构?但如果我错了请纠正我。这可能是我处理结构的方式而不是查询本身。
I am trying to grab data from the DB of a particular table, where there is either no join to a another table, or there is but it isn't the right Data.
Structure
I have a domains table. All this does is hold a domain name, and a few other misc metadata.
I have a features table, all this has is an ID column, and a column for a value of what that feature is. E.g. It could look like this :
1 | First Registered
2 | Expired On
3 | Hosted On
Etc.
I have a DomainData table. This holds the values for the features. The columns are something like this.
ID | DomainId | FeatureId | Value
Essentially it holds the value of the feature for that domain, something like a key value column so that it can be ever expanding without modifying the table structure of the Domain table.
Now, what I need to do is do a query for all domains that do not have feature X.
For example, this works in SQL :
SELECT D.*
FROM Domain AS D
LEFT OUTER JOIN DomainData AS Data ON Data.DomainId = D.Id
WHERE data.featureId IS NULL OR data.FeatureId != @FeatureId
That works fine in SQL, and returns all needed data. All that does is grab all domains that either have NO features at all, or they do have features, but not the ones I require.
Now I am using CastleActiveRecord as my datalayer, but I am struggling to as to how I can write this in HQL. Spent many an hour on it now, and I either get back nothing, Or I get back ALL domains, regardless of their feature ID. Unfortunately I have deleted all the HQL statements that I have tried.
Can I get some help on how I would rewrite the above statement to HQL?
Side Note : Inside my classes, I have this inside the DomainData Class :
[BelongsTo("DomainId")]
public Domain Domain { get; set; }
[BelongsTo("FeatureId")]
public Feature Feature { get; set; }
And this inside my Domain Class :
private IList<DomainData> featureData = new List<DomainData>();
[HasMany(typeof(DomainData), Table = "DomainData", ColumnKey = "DomainId")]
public IList<DomainData> FeatureData
{
get { return featureData; }
set { featureData = value; }
}
I think that is the correct data structure? But correct me if I am wrong. It could be how I am doing the structure rather than the query itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查一下:
请注意,我正在映射实体
Feature
上调用IS NULL
,但生成的查询将检查实际的 FK 列。作为评论,我发现奇怪的是您需要检查 featureid 是否为空,因为您还与我猜测不为空的 @FeatureId 变量进行比较。check this out:
notice that i'm calling
IS NULL
on the mapped entityFeature
yet the resulting query will check the actual FK column. As a comment i find it odd that you need to check for null for featureid since you also compare with the @FeatureId variable which i'm guessing is not null.