Dear all,
I'm experiencing a problem using the foreign primary key generator with a one-to-one association.
Let me try to describe the situation.
- I have two classes: A and B
- A's id is generated using the hi-lo algorithm.
- B's id is generated using the 'foreign generator'
- there is a one to one association that goes from B to A, constrained set to true
my unit test looks like this:
Code:
A a = new A();
[transaction]
HibernateTemplate.SaveOrUpDate(a);
[transaction closed]
[...]
B b = new B();
b.A = a
[transaction]
HibernateTemplate.SaveOrUpDate(b);
[transaction closed]
I'm expecting b to be saved in the database with b.Id set to a.Id.
Instead, a new instance of A, aa, is created and saved in the database (with aa.Id = a.Id + 1).
At the end of my unit test, i delete b, then a, but aa remains.
I had a look at the source code of NHibernate.Id.ForeignGenerator:
Code:
try
{
id = ForeignKeys.GetEntityIdentifierIfNotUnsaved(type.GetAssociatedEntityName(), associatedObject, sessionImplementor);
}
catch (TransientObjectException)
{
id = session.Save(associatedObject);
}
Apparently, a TransientObjectException is raised and Save(a) is called.
My object a is not transient, it should be detached.
I'm using SQL2005, NHibernate 1.2 and Spring 1.1.2
Can you help me with this?
Thank you.
Best regards,
Clement
Code: