Nhibernate does not save a collection of strings
I am not new to nhibernate, but I can't get it to save/update a collection of strings.
I have an entity and this entity has 3 collections of strings (Iesi.ISet<string>
).
I make changes to the string collections on the entity, and call Session.Save
, Session.Update
, Session.SaveOrUpdate
, nothing works.
This is all in an nhibernate transcation, the transaction is being commited. I can see it all happening in NHProf, no errors, no exceptions. Nhibernate simply ignores my collections.
It updates the parent entity fine.
Yes, cascade is set. to save-update, Ive also tried all-delete-orphan, which is actually what i want.
The collections on the entity are a Iese.Iset<string>
. They are initialized in the constructor of the entity using new Iesi.Collections.Generic.HasheSet<string>()
.
I will paste in the mapping file:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NameRemovedV2.Web" namespace="NameRemovedV2.Web.Models">
<class name="NameRemovedUser" table="NameRemovedUsers">
<id name="NameRemovedUserID">
<generator class="hilo"></generator>
</id>
<many-to-one cascade="save-update" class="NameRemovedSite" column="NameRemovedSiteID" name="Site" not-null="true" foreign-key="FK_NameRemovedSites_NameRemovedUsers"></many-to-one>
<property name="Username" not-null="true" unique="true" unique-key="UX_NameRemovedUsers"></property>
<property name="FirstName" not-null="false"></property>
<property name="LastName" not-null="false"></property>
<property name="EmailAddress" not-null="false">开发者_开发百科;</property>
<property name="DealerCode" not-null="false"></property>
<property name="RegistrationDate" not-null="true"></property>
<set cascade="save-update" inverse="true" name="LeftSide" table="LeftSides">
<key column="NameRemovedUserID" not-null="true" foreign-key="FK_NameRemovedUsers_LeftSides"></key>
<element column="ElementID" not-null="true"></element>
</set>
<set cascade="save-update" inverse="true" name="RightSide" table="RightSides">
<key column="NameRemovedUserID" not-null="true" foreign-key="FK_NameRemovedUsers_RightSides" ></key>
<element column="ElementID" not-null="true"></element>
</set>
<set generic="true" cascade="save-update" inverse="true" name="Hide" table="Hides">
<key column="NameRemovedUserID" not-null="true" foreign-key="FK_NameRemovedUsers_Hides" ></key>
<element column="ElementID" type="String" not-null="true"></element>
</set>
</class>
</hibernate-mapping>
I think the problem is the inverse="true"
attribute in your set mappings. This informs NHibernate that the other (one) side of the relationship will be examined for changes when the session is flushed. With a set of strings there is no way to associate a string to the parent except by adding it to the collection. Try removing the inverse="true"
attribute or setting it to false (the default).
精彩评论