Correct NHibernate mapping for a specific scenario (one-to-many / one-to-one)

I had the following structure:

User has many books in his history

which was transferred to the next

class User { ICollection<Book> History }       // C#
User -> UserHistory (UserId, BookId) -> Book   // DB

Now I want to add a date to the story by creating the following class structure:

class User { ICollection<Read> History }
class Read { Book Book, Date At }

and saving the db schema almost unchanged

User -> UserHistory (UserId, BookId, At) -> Book

I want to match Readwith the UserHistoryquestions:

  • What should be used as idin the display Read? UserHistoryprimary key (UserId, BookId). Do I need idNH to work?
  • UserHistory -> Bookseems to be the case one-to-one. How to specify the name of the column BookIdin UserHistorythis case? I do not see the attribute of the column on one-to-one(and there is a reason why I should be explicit about the column name).
+3
2

UserHistory " " . UserHistory/Read - , . - ReadId ( UserHistoryId) UserHistory.

UserId BookId, ? , At , . , .

UserHistory to Book --, . (, , ). -- .

+2

1: , , ( , )

2: " → , , .

, :

<class name="User">

  <bag name="History" table="UserHistory">
    <key name="UserId">
    <composite-element class="Read">
      <property name="At" />
      <many-to-one name="Book" column="BookId" />
    </composite-element>
  </bag>

. one-to-one. , , . many-to-one, .

+1

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


All Articles