ManyToOne Breaking Changes between NH 2.1 and 3.2

49 views
Skip to first unread message

sternr

unread,
Nov 29, 2011, 11:35:07 AM11/29/11
to nhu...@googlegroups.com
EntityA has a ManyToOne relationship to EntityB which uses the property-ref attribute.
In addition, the relationship is also mapped as cascade="none" and readonly.

When I try and save a new instance of EntityA:

EntityA a = new EntityA();
a.EntityB = new EntityB() { propertyRef = 33 };
session.Save(a);

I get "object is an unsaved transient instance - save the transient instance before merging".

In NHibernate 2.1 this worked as expected.

What am I missing?


Asher Newcomer

unread,
Nov 29, 2011, 11:42:07 AM11/29/11
to nhu...@googlegroups.com
If cascade="none", why do you expect it to save EntityB for you?



--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/hsLkjIfdUYQJ.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.

Edgar Muniz Berlinck

unread,
Nov 29, 2011, 11:41:28 AM11/29/11
to nhu...@googlegroups.com
Try this:

Open one transaction (ITransaction)

do what you are doing

Commit the transction. 

Close the session.

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/hsLkjIfdUYQJ.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.



--
Edgar Muniz Berlinck
http://edgarberlinck.wordpress.com/
http://about.me/edgarberlinck

Follow me on Twitter: http://twitter.com/@edgarberlinck

"Você pode passar a vida inteira chorando, ou pode passar a vida inteira vendendo lenços. A escolha é sua, e de mais ninguém."

sternr

unread,
Dec 1, 2011, 10:53:07 AM12/1/11
to nhu...@googlegroups.com
I dont want EntityB to be saved, I just want the value of propertyRef column in EntityA's table to be saved!

Oskar Berggren

unread,
Dec 1, 2011, 11:07:32 AM12/1/11
to nhu...@googlegroups.com
2011/12/1 sternr <ste...@gmail.com>:

> I dont want EntityB to be saved, I just want the value of propertyRef column
> in EntityA's table to be saved!
>

So you already have EntityB in the database and what you are trying to
do is similar to

a.EntityB = session.Load<EntityB>(bId);
session.Save(a);

except that you want (NHibernate) to find the EntityB instance by the
value of the property used as property-ref, instead of by primary key.
Is this a correct understanding?


/Oskar

sternr

unread,
Dec 1, 2011, 11:25:34 AM12/1/11
to nhu...@googlegroups.com
Basically yes.
Though I dont really need (NHibernate) to find the EntityB instance - the relationship between the entities is only by the property-ref property, so basically I dont need any additional data.

Ricardo Peres

unread,
Dec 2, 2011, 9:09:28 AM12/2/11
to nhusers
You can do it with stateless sessions.

RP

Reply all
Reply to author
Forward
0 new messages