NHibernate "Flushing the Session" question.

70 views
Skip to first unread message

mynkow

unread,
Jul 13, 2011, 7:55:50 AM7/13/11
to nhu...@googlegroups.com
Hi

I have an entity Culture with a child collection of value objects CultureResource. There is a concept of import/export for culture resources. When I do import a resource (key/value) is deleted and a resource is inserted (samekey/newvalue). As I know there is a specific order when a session is flushed:
So, what happens is the insert is before the delete and I got error from the db for uniqueness. One of the solutions was to call session.Flush() right after the delete, but the Import/Export object does not know anything about persistence, so not an option.

here is my mapping:
Table("culture");
            Cache.ReadWrite();
 
            Id(c => c.Id, "culture_id");
            Map(c => c.CultureID, "lcid");
            Map(c => c.ShortName, "abbr");
            Map(c => c.FullName, "name");
            HasMany<LocalizationResource>(Reveal.Member<Culture>("_resourcesCollection"))
                .Access.CamelCaseField()
                .Cascade.AllDeleteOrphan()
                .Inverse()
                .Not.LazyLoad()
                .Fetch.Join()
                .Cache.ReadWrite().Region("resources");
Any ideas how to force delete before insert?
Any other suggestions?

Best regards

belva...@googlemail.com

unread,
Jul 13, 2011, 9:17:11 AM7/13/11
to nhu...@googlegroups.com
What exactly are you doing in your code?

Am schrieb mynkow <myn...@gmail.com>:

> Hi
>
> I have an entity Culture with a child collection of value objects CultureResource. There is a concept of import/export for culture resources. When I do import a resource (key/value) is deleted and a resource is inserted (samekey/newvalue). As I know there is a specific order when a session is flushed:
>
> The SQL statements are issued in the following order:all entity insertions in the same order the corresponding objects were saved using Session.save()all entity updatesall collection deletionsall collection element deletions, updates and insertionsall collection insertionsall entity deletions in the same order the corresponding objects were deleted usingSession.delete()

>
> So, what happens is the insert is before the delete and I got error from the db for uniqueness. One of the solutions was to call session.Flush() right after the delete, but the Import/Export object does not know anything about persistence, so not an option.
>
>
> here is my mapping:
> Table("culture");
>             Cache.ReadWrite();
>
>             Id(c => c.Id, "culture_id");
>             Map(c => c.CultureID, "lcid");
>             Map(c => c.ShortName, "abbr");
>             Map(c => c.FullName, "name");
>             HasManyLocalizationResource>(Reveal.MemberCulture>("_resourcesCollection"))

>                 .Access.CamelCaseField()
>                 .Cascade.AllDeleteOrphan()
>                 .Inverse()
>                 .Not.LazyLoad()
>                 .Fetch.Join()
>                 .Cache.ReadWrite().Region("resources");
> Any ideas how to force delete before insert?
> Any other suggestions?
>
>
> Best regards
>
>
>
>
>
> --
>
> 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/-/wO4TC513naUJ.
>
> 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.
>
>
>
>

mynkow

unread,
Jul 13, 2011, 10:09:19 AM7/13/11
to nhu...@googlegroups.com
var culture = Session.Get(123);
LanguageSerivce.Import(culture, content);
session.SaveOrUpdate(culture);

belva...@googlemail.com

unread,
Jul 13, 2011, 10:21:20 AM7/13/11
to nhu...@googlegroups.com
And LanguageServie.Import only will do Resources.Clear(), new Resource(), AddResource() etc. and no Session specific things?

Am schrieb mynkow <myn...@gmail.com>:

> var culture = Session.Get(123);LanguageSerivce.Import(culture, content);
> session.SaveOrUpdate(culture);
>
>
>
>
>
> --
>
> 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/-/wkKU6zgBshUJ.

mynkow

unread,
Jul 13, 2011, 10:25:25 AM7/13/11
to nhu...@googlegroups.com
Other stuff too, but nothing about session.

belva...@googlemail.com

unread,
Jul 13, 2011, 10:59:56 AM7/13/11
to nhu...@googlegroups.com
This is strange. I would think it is because of the same ID so Equals gives true for the deleted and the new entity, but thats just me assuming.
Why do you not update the existing Resource Entity if there already is one?

Am schrieb mynkow <myn...@gmail.com>:

> Other stuff too, but nothing about 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/-/R1DIod8bVPoJ.

mynkow

unread,
Jul 13, 2011, 11:05:34 AM7/13/11
to nhu...@googlegroups.com
Because it is not an entity. It is a value object

mynkow

unread,
Jul 13, 2011, 11:07:03 AM7/13/11
to nhu...@googlegroups.com
about your assuming, I have some constraints in the DB for uniqueness, doesn't matter if it is ID or something else. I want deletes before inserts.

belva...@googlemail.com

unread,
Jul 13, 2011, 11:17:53 AM7/13/11
to nhu...@googlegroups.com
I meant that i think that the reason for your behaviour is that the deleted and the new entity have the same ID. But I don't know exactly if this is correct.
Maybe you should clear your collection and Save the culture entity before calling the ImportService. Just to see if this fixes the problem.

Am schrieb mynkow <myn...@gmail.com>:

> about your assuming, I have some constraints in the DB for uniqueness, doesn't matter if it is ID or something else. I want deletes before inserts.
>
>
>
>
> --
>
> 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/-/-bAiUE-_AsUJ.

mynkow

unread,
Jul 13, 2011, 11:23:38 AM7/13/11
to nhu...@googlegroups.com
well, I cannot clear the collection from business perspective. And the you are right about the error that there is a record with the same id. And that is why I want to do delete first and then insert.


the same problem, but his solution do not work for me. Now I am looking for other solutions for example forcing deletes before inserts somehow.

John Davidson

unread,
Jul 13, 2011, 11:41:29 AM7/13/11
to nhu...@googlegroups.com
It may not be possible to do what you want as a business unit. If you do an explicit flush after the delete and before the insert then it would behave as you describe, but it will also probably delete any collection associated with that ID.

I think you would have an identical problem if you were to work just in sql statements - and if you do that would mean you need to redesign how this process should function.

John Davidson

--
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/-/g8ASjJRw5hoJ.

mynkow

unread,
Jul 13, 2011, 12:03:48 PM7/13/11
to nhu...@googlegroups.com
true
Reply all
Reply to author
Forward
0 new messages