These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Mapping a foreign key to an association table, not Entity
PostPosted: Wed Sep 27, 2006 10:07 am 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
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?

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.