开发者

HQL Query - Castle Active Record - Outer Joins

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.


check this out:

select d 
from Domain d left join d.DomainData ddata
where ddata.Feature is null OR ddata.Feature.Id <> :featureId

notice that i'm calling IS NULL on the mapped entity Feature 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜