2

I'm trying to do the following, but it's complaining that the "classes referenced by 'extends' were not found". I think I need to having a mapping for each concrete type of Component but I can't specify the Attributes.Class twice..

The code is as follows:

[NHibernate.Mapping.Attributes.Class(Table = "Components", Abstract = true,
    NameType = typeof (Component<ContentItem>))]
public abstract class Component<T> : IComponent<T> where T : ContentItem
{
    ...
}

[NHibernate.Mapping.Attributes.JoinedSubclass(Table = "ComponentA", ExtendsType = typeof(Component<ItemA>))]
public class ComponentA : Component<ItemA>
{
    ...
}

[NHibernate.Mapping.Attributes.JoinedSubclass(Table = "ComponentB", ExtendsType = typeof(Component<ItemB>))]
public class ComponentB : Component<ItemB>
{
    ...
}

Where ItemA and ItemB inherit from ContentItem and are all mapped.

1 Answer 1

2

You can't map an open generic type like this, i.e. one that has an unspecified type parameter <T>. It just doesn't work.

Ayende discusses this in more detail on his blog.

Sign up to request clarification or add additional context in comments.

3 Comments

If I follow that article and hard-code the information in the Component`1.hbm.xml file, then it appears to work fine. I just don't know how to do it using the Mapping.Attributes annotation. Annoyingly, T isn't actually mapped in the classes because it's not persisted by the database in this instance.
So it sounds like the persistent property of Component<T> do not actually depend on T. If this is the case, separate them into a ComponentBase (which is mapped), have Component<T> derive from ComponentBase (which is not mapped) and then have your Component<ItemA> class mapped as a joined-subclass of ComponentBase.
Yes, that's what I ended up doing in the end. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.