Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate Mapping with No Id

Tags:

nhibernate

Is it possible to do a mapping in NHibernate without using an Id?

What I'm trying to do is, call a stored procedure using

session.CreateSqlQuery( myQuery ).AddEntity( typeof( MyEntity ) );

The procedure is an aggregate and there is no Id field being returned, but I would still like to take advantage of NHibernate mapping the data returned into an entity. The only thing I have come up with so far is having the procedure add a field "O as Id" or something, and have the entity have a fake Id field.

like image 228
Josh Close Avatar asked Jun 15 '09 02:06

Josh Close


2 Answers

Your entity must be "unique" in some way, even if it doesn't have an Id field.

Decide which properties, when taken together, must be unique, and use a composite key over them...

<composite-id>
  <key-property name="Property1" column="Column1"/>
  <key-property name="Property2" column="Column2"/>
</composite-id>
like image 148
Andy McCluggage Avatar answered Oct 12 '22 10:10

Andy McCluggage


I found this really isn't possible. The data returned from the stored procedure has to have an Id also. It worked to just create a dummy column with an Id, and a dummy property on the object, but I just decided to convert the data returned by NHibernate to an object by hand.

Basically, you're not supposed to use stored procedures with NHibernate. It IS an ORM afterall. In my case, I had no choice but to use the procedure.

like image 40
Josh Close Avatar answered Oct 12 '22 10:10

Josh Close