Fluent nHibernate Join is doing insert into joined table
Asked Answered
E

1

6

I am trying to use join to pull in a single property from another table, which doesn't have a mapping. My problem is that when I create a new instance of my mapped entity and save it I get an error about trying to insert into my unmapped table (it's trying to insert null into a not null column). I thought using .ReadOnly() would stop nhibernate from trying to insert into my unmapped table but that doesn't seem to work.

My mapping looks like this:

        // Join _UnMapped table with Mapped table to get the property
        Join("_UnMapped", x =>
            {
                x.Fetch.Join();
                x.KeyColumn("UnMappedFK");
                x.Map(y => y.Property, "Property")
                    .Not.Nullable()
                    .ReadOnly();
            });

I have thought about creating a view and mapping to that to get this property, but if I can I would rather do it through a mapping. Any help (or an explanation on how join is supposed to work) would be greatly appreciated!

Enthymeme answered 26/7, 2011 at 21:39 Comment(0)
S
8

Use x.Inverse();.

Here is some documentation about join.

Streamline answered 27/7, 2011 at 5:51 Comment(2)
As it turns out setting inverse worked after resolving some other issues I had. I don't entirely understand why though. I thought was just a flag to tell nhibernate which entity in the relationship owns the FK. in this case my mapped table is the one that has the FK (i.e. inverse = false right?). Your link doesn't work for me so unfortunately that doesn't help me.Enthymeme
@Ben Don't know why the link doesn't work for you, it works fine for me. But "inverse" is just a bad naming here. From the documentation: inverse (optional - defaults to false): If enabled, Hibernate will not try to insert or update the properties defined by this join.Streamline

© 2022 - 2024 — McMap. All rights reserved.