mercredi 28 octobre 2015

How to insert entity with value collection using a stateless session?

I have the following (simplified) entity that I need to bulk-insert:

public class BookmarkEntity
{
    public virtual Guid Id { get; set; }

    public virtual ISet<string> Tags { get; set; }
}

And the Tags property is configured (via FluentNHibernate):

public class BookmarkEntityMappingOverride : IAutoMappingOverride<BookmarkEntity>
{
    public void Override(AutoMapping<BookmarkEntity> mapping)
    {
        mapping.HasMany(x => x.Tags)
               .AsSet().Element("Value")
               .Cascade.Delete()
               .Not.LazyLoad();
    }
}

This results in these tables (using SQLite):

CREATE TABLE "BookmarkEntity" (
    Id UNIQUEIDENTIFIER not null,
    primary key (Id)
)

CREATE TABLE Tags (
    BookmarkEntity_id UNIQUEIDENTIFIER not null,
    Value TEXT,
    constraint FK9061CD2928F7F2F9 foreign key (BookmarkEntity_id) references "BookmarkEntity"
)

I would like to use a stateless session to reduce memory consumption and improve the speed a little. I realize that because stateless sessions do not perform any cascading, I must insert the Tags collection myself. I tried the following:

void InsertIntoStatelessSession(BookmarkEntity entity, IStatelessSession statelessSession)
{
    statelessSession.Insert(entity);
    if (entity.Tags != null)
    {
        foreach (string tag in entity.Tags)
            statelessSession.Insert("Tags", tag);
    }
}

But this fails with an exception:

NHibernate.MappingException : No persister for: Tags

So, how can I insert the bookmark entity and its set of tags?

Aucun commentaire:

Enregistrer un commentaire