0

I have two classes:

class User {
    public int Id { get;set; }
    public string Name { get; set; }
}

class VerifiedUser : User {
    public ICollection<Verified> { get; set; }
}

I would like NHibernate to treat VerifiedUser and User as the same table but keep them separate to, so.

Session.Query<User>() //would return a User
Session.Query<VerifiedUser>() //would return a VerifiedUser

Is this possible or is it unsupported?

Jake Rote
  • 2,177
  • 3
  • 16
  • 41
  • Why would you want to treat them as the same table? They are clearly not the same entity. – Maria Ines Parnisari Nov 05 '15 at 01:36
  • http://ayende.com/blog/3941/nhibernate-mapping-inheritance This might help... specify the "abstract" table and it's sub tables in the hbm. – Yaw Nov 06 '15 at 12:34
  • What is the difference between the two entities? They get persisted from the same table but say with a different `where` clause? Please clarify/edit your question. – kayess Dec 04 '15 at 10:48

1 Answers1

0

You will need to implement the table-per-hierarchy strategy with Fluent Nhiberate in mapping classes. These are like overrides for the AutoMapping feature (if used) of FNH, otherwise mapping classes are de facto and you will be used to them.

Something like:

public class UserMappingOverride : IAutoMappingOverride<User>
 {
    public void Override(AutoMapping<User> mapping)
    {                  
        mapping.DiscriminateSubClassesOnColumn("IsVerified").Not.Nullable();
    }
 }

public class VerifiedUserClassMap : SubclassMap<VerfiedUser>
{
    public VerifiedUserClassMap()
    {
        DiscriminatorValue("Yes");
    }
}

And to answer your question, yes as far as I remember nothing to do here: Session.QueryOver<VerifiedUser>() as NHibernate will add on the where clause for the discriminator

jenson-button-event
  • 18,101
  • 11
  • 89
  • 155