Luiz
Luiz

Reputation: 542

Nhibernate bag element not persisting

I have a Class that has two bags. One bag is with a collection of a class and it works, the other is a collection of long values and this one is not persisted. I've searched all the web for this and my mappings appears to be OK.

In my mapping I have this:

  <class name="Event" table="Events">
    <id name="Id" type="Int32">
        <generator class="native" />
    </id>
    <property name="Name" />
    <property name="Owner" />
    <many-to-one name="DeliveryAddress" column="DeliveryAddressId" cascade="save-update, persist" />
    <many-to-one name="EventAddress" column="EventAddressId" cascade="save-update, persist" />
    <bag name="Friends" table="Event_Friends" lazy="false" inverse="true" cascade="save-update, persist" fetch="join">
      <key column="EventId" />
      <element column="Friend" type="Int64" />
    </bag>
    <bag name="Products" table="Event_Products" lazy="false" inverse="true" cascade="all,delete-orphan" fetch="join">
      <key column="EventId" />
      <one-to-many class="Product" />
    </bag>
  </class>

When I call SabeOrUpdate in my session NHibernate create both adresses, create the event and all the products, but the friends list is not saved. After the save I issue an Get, and the select on the database is correct. I don't know what else can be.

My model for this mapping is this:

public class Event
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Int64 Owner { get; set; }
    public virtual Address DeliveryAddress { get; set; }
    public virtual Address EventAddress { get; set; }
    public virtual ICollection<Int64> Friends { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}

and my database looks like this:

Events
-------------------------------
Id                int     identity
Name              varchar
Owner             long
DeliveryAddressId int
EventAddressId    int

Address
-------------------------------
Id                int
-- Code Abbreviated --

Event_Products
-------------------------------
Id                int
EventId           int
-- Code Abbreviated --

Event_Friends
-------------------------------
EventId           int
Friend            long

Upvotes: 1

Views: 1149

Answers (1)

hazzik
hazzik

Reputation: 13364

You should change inverse to false for your collection of longs

<bag name="Friends" table="Event_Friends" lazy="false" inverse="false" cascade="save-update, persist" fetch="join">
  <key column="EventId" />
  <element column="Friend" type="Int64" />
</bag>

Upvotes: 2

Related Questions