• 0

[C#] NHibernate


Question

Hi,

I can't seem to get my code to work with NHibernate properly, what I am trying to do is Inherit my Database Entities into my classes, so i have it like this:

class DbEntity
{
public virtual int UserId { get; set; }
public virtual int Something { get; set; }
}

and

class SomeClass : DbEntity
{
// some code here
}

but in nHibernate if i do say session.Save(SomeClass); it doesn't work? it says there is no persister for "SomeClass" it seems to not understand that "DbEntity" which is Inherited is what needs to be used here, I have classes which include data I want to retrieve, save and update from my database using Hibernate but these classes are Inherited like deep down, but why won't Hibernate accept "SomeClass" and just cast it to DbEntity and work out what it needs??

Thanks!

EDIT: When i mean't .Save(SomeClass); i mean if i do like

var blah = new SomeClass();
session.Save(SomeClass);

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

this or this could be of help?

I don't think those explain what I wanted to do. It looks like I have to infact Map both DbEntity and SomeClass classes or nHibernate doesn't have a clue when i do .Save(SomeClass); because it moans about a persister, I can see now why it sort of does this, but this means I need duplicate mappings now, not much of an issue though really, im sure there are better ways to do what I want to do but I may aswell just map all the classes I wish to send to Hibernate, not sure why Hibernate doesn't pickup the base class, but I guess this may conflict with other things, like maybe for instance SomeClass included more fields but also Inherits the fields of DbEntity.

Thanks anyway, if someone does know what I want to do and can point me in another direction then feel free :p

Matt.

Link to comment
Share on other sites

  • 0

Hi,

I can't seem to get my code to work with NHibernate properly, what I am trying to do is Inherit my Database Entities into my classes, so i have it like this:

class DbEntity
{
public virtual int UserId { get; set; }
public virtual int Something { get; set; }
}

and

class SomeClass : DbEntity
{
// some code here
}

but in nHibernate if i do say session.Save(SomeClass); it doesn't work? it says there is no persister for "SomeClass" it seems to not understand that "DbEntity" which is Inherited is what needs to be used here, I have classes which include data I want to retrieve, save and update from my database using Hibernate but these classes are Inherited like deep down, but why won't Hibernate accept "SomeClass" and just cast it to DbEntity and work out what it needs??

Thanks!

EDIT: When i mean't .Save(SomeClass); i mean if i do like

var blah = new SomeClass();
session.Save(SomeClass);

NHibernate accepts base classes without issues. You don't need a mapper for the base class.

var blah = new SomeClass();
session.Save(SomeClass);

Is this your actual code? If so, you're sending a type to the Save method.

Are you using XML map files, or Fluent NHibernate?

Link to comment
Share on other sites

This topic is now closed to further replies.