Hibernate version: 1.0.2
I'm having a problem with the order of my inserts. This is caused because I have not conveyed to NHibernate that a constraint exists in the database, so it issues a few commands in the wrong order and gets an exception.
Here's what I have:
EntityA has a collection of EntityB (many to many)
EntityC has a reference to EntityA and EntityB (one to one on both)
So you can imagine that the database looks like this:
Code:
[b]EntityA[/b]
---------
EntityA_id
//other columns here
[b]EntityB[/b]
----------
EntityB_id
//other columns here
[b]EntityA_EntityB[/b]
-----------------
EntityA_id
EntityB_id
[b]EntityC[b]
-----------------
EntityA_id
EntityB_id
//other columns here
So the table EntityA_EntityB is how we represent a many-to-many between the two entities.
The problem is that the 2 fields from the EntityC table are not foreign keyed to the EntityA and EntityB tables, respectively.... they are foreign keys to the EntityA_EntityB join table. This is my problem. I cannot seem to convey this information in my mapping file.
My test looks something like this:
Code:
EntityA a = new EntityA("123");
EntityB b = new EntityB("456");
a.EntityBs.Add(b);
EntityC c = new EntityC(a, b);
session.SaveOrUpdate(b);
NHibernate issues the commands in this order:
INSERT ON EntityB for b
INSERT ON EntityA for a
INSERT ON EntityC for c
//ERROR
INSERT ON EntityA_EntityB for a/b
At this point the only thing I can come up with is to map EntityA_EntityB as an actual entity with no properties, and this seems kind of ugly. Is there another way to inform NHibernate that there is a database constraint on that table and that it should do those last two INSERTS in reverse order?