Storing a simple relation without an explicit identifier

So, I have two tables, name them User and Item. I want every user to be able to mark (for example, "watch" things in a Google universe) on any element.

Now, from a set-theoretical perspective, it makes sense to keep this as a simple connection between users and elements. Call this new table UserItemFlags. The table should have two columns; one with foreign keys in the User table, the other with foreign keys in the Item table. If the user flag U points to element I, this is then represented by the presence of a row (U, I)in the UserItemFlags table.

Now my problem with expressing this in Fluent-NHibernate is this: I can't figure out what to do with the mapping Id. Ideally, I would not have an identifier, because there is nothing more than the presence or absence of this connection between the User and the Element. A natural consequence of this model is the inability to duplicate rows in a table. This is the desired function.

using FluentNHibernate.Mapping;

public class UserItemFlagsMapping : ClassMap<UserItemFlags>
{
    public UserItemFlagsMapping()
    {
        // Aaaa! I must have an Id!
        References(x => x.User).Not.Nullable();
        References(x => x.Item).Not.Nullable();
    }
}

I would be happy for any pointers in the right direction, even if they require me to abandon Fluent for this particular case.

+3
source share
2 answers

What does it mean you do not have an identifier? You have a composite identifier with user id and item id.


Edit: The answer in this comment is:

Id() . CompositeId(). - : CompositeId(). KeyReference (x = > x.User, "user_id" ). KeyReference (x = > x.Item, "item_id"; , , t .

+2

Fluent NHibernate, NHibernate , .

Many-to-many . , :

User.Items Item.Users

UserItemFlags.

. : http://marekblotny.blogspot.com/2009/02/fluent-nhbernate-and-collections.html

+1

Source: https://habr.com/ru/post/1786631/


All Articles