Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

NHibernate Set() equivalent or Join on collection #526

Answered by bend
bend asked this question in Q&A
Discussion options

I have the following use case:

public class Object {
  long Id
   ...
  ISet<Tags> tags
}
        
public class Tag : IEquatable<Tag> {
  string Label;
}

Object is an Aggregate Root and Tag a Value Object.
Both are stored in 2 different tables:

CREATE TABLE incident(id bigint, ...)
CREATE Table tag (object_id bigint References object(id), label varchar,...)

I'm trying to create the ClassMap using FluentNhibernate, which works well for object but I couldn't find a way to map it with Tag

public ObjectsMapping()
        {
            Id(x => x.Id).GeneratedBy.Assigned();
            Version(x => x.ObjectVersion).Column("object_version");         
            HasMany(x => x.Tags).Inverse().Cascade.All().KeyColumn("object_id");
        }

public TagsMapping()
        {
            CompositeId().KeyProperty(x => x.Label).KeyProperty(x => x.CreationTimestamp);
            Map(x => x.Label);
            Map(x => x.CreationTimestamp);
        }

Any idea how to map that an entity that has a oneToMany relation with a ValueObject from another table ?

Basically I'm looking for an equivalient of Set() in NHibernate

It's like doing a Join on a collection where the collection does not have any identifier (basically the Pk of Tags is the combination of all the fields)

I've tried:
HasMany(x => x.Tags).KeyColumn("object_id").AsSet();

However Nhibernate when adding a Tag to the object is doing an Update instead of an insert which fails

Thank you

You must be logged in to vote

ok I found the solution:
HasMany(x => x.Tags).Component(x => { x.Map(k => k.Label); }).Table("tag");

Replies: 2 comments

Comment options

ok I found the solution:
HasMany(x => x.Tags).Component(x => { x.Map(k => k.Label); }).Table("tag");

You must be logged in to vote
0 replies
Answer selected by hazzik
Comment options

Note that if it's really a value object, you shouldn't really have a class mapping for it. You should only have the component mapping.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #525 on February 04, 2022 18:57.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.