tags:

views:

27

answers:

1

Consider following 2 classes:

public class TypeAGood{
  public virtual int Id{get;set;}
  public virtual string Description{get;set;}
  public virtual Type Type{get;set;}  
}

public virtual class Type{
  public virtual int Id{get;set;}
  public virtual string Name{get;set;}
}

mappings:

 public class TypeMap : ClassMap<Type>
    {
        public TypeMap()
        {
            Table("Type");
            Id(x => x.Id);
            Map(x => x.Name);
        }
    }

 public class TypeAGoodMap : ClassMap<TypeAGood>
 {
        public TypeAGoodMap()
        {
            Table("Good");
            Id(x => x.Id);
            Map(x => x.Description);
            References(x => x.Type)
               .Fetch.Join()
               .Column("TypeId")
               .ForeignKey("Id");
        }
 }

Type could have different values like a,b,c.
How can I change the TypeAGoodMap to only map the goods which have Type a?
Thanks,
Al

A: 

Sounds like you're looking for a filter described here. Unfortunately this is not supported in FluentNhibernate yet. But this is what it looks like in hbm:

<filter name="typeAfilter" condition=":typeA = TypeId"/>

and then in the query do something like this:

ISession session = ...;
session.EnableFilter("typeAfilter").SetParameter("typeA ", "a");
IList<TypeAGood> results = session.CreateCriteria(typeof(TypeAGood))
         .List<TypeAGood>();
Chris Conway
Thanks Chris for your reply.Your solution seems to be what I need.Since this is not available in fnh,do you think it might be possible using discriminator/subclass to solve this?
Silvia
It may be possible, depending on your domain model. Is there a TypeBGood and TypeCGood? If so, do they inherit from IGood? In this scenario, Types would have to have a Good (either a, b or c) in the database and you would discriminate on that column as to whether you have a TypeAGood, TypeBGood, etc.What's wrong with the hbm solution? I've got plenty of mixed items in my mappings, about half are fluent and half are hbm. I love the fluent, but until it can match what's available in hbm I'm forced to do both and it's not so bad.
Chris Conway