Breeze + NHibernate Many-To-One Relation, avoid having to specify relation keys

痞子三分冷 提交于 2019-12-12 20:12:58

问题


The following many to one mapping is working (taken from NorthBreeze):

public partial class UserRole
{
    public virtual long ID { get; set; }
    public virtual long UserId { get; set; }
    public virtual long RoleId { get; set; }

    public virtual User User { get; set; }
    public virtual Role Role { get; set; }
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Models.NorthwindIB.NH" assembly="Model_NorthwindIB_NH">
  <class name="UserRole" table="`UserRole`" dynamic-update="true" optimistic-lock="dirty">
    <id name="ID" column="`ID`" type="long" unsaved-value="0">
      <generator class="native" />
    </id>
    <many-to-one name="User" column="`UserId`" class="User" />
    <many-to-one name="Role" column="`RoleId`" class="Role" />
    <property name="UserId" type="long" not-null="true" insert="false" update="false" />
    <property name="RoleId" type="long" not-null="true" insert="false" update="false" />
  </class>
</hibernate-mapping>

How can I make this kind of many to one working (without UserId and RoleId properties specified in the model):

public partial class UserRole
{
    public virtual long ID { get; set; }

    public virtual User User { get; set; }
    public virtual Role Role { get; set; }
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Models.NorthwindIB.NH" assembly="Model_NorthwindIB_NH">
  <class name="UserRole" table="`UserRole`" dynamic-update="true" optimistic-lock="dirty">
    <id name="ID" column="`ID`" type="long" unsaved-value="0">
      <generator class="native" />
    </id>
    <many-to-one name="User" column="`UserId`" class="User" not-null="true" />
    <many-to-one name="Role" column="`RoleId`" class="Role" not-null="true" />
  </class>
</hibernate-mapping>

Error thrown: Could not find matching fk for property Models.NorthwindIB.NH.UserRole.User

Could this be somehow handled in the future versions of BreezeJs, or is there currently a way of doing it without having to specify these keys in the model itself? Or is there more to it than what i'm seeing?

EDIT (unofficial solution): Thank you for guiding us in the right direction. As this was something we wanted taken care of, we made some modifications ourselves based on what Steve Schmitt said.

Here's a link to the changes so you can see how we solved this issue, so it can be of help to anybody else. (please note that this code only works for NHibernate)

https://github.com/maca88/Breeze/commit/7a80c35cf0b20b5cffdef6d2eddeccd1bdeb3735


回答1:


No, but we're planning to fix that. The problem, as you and Rippo have learned, is that the FKs are required on the client for Breeze to work. They are also required to re-connect the entities on the server during the SaveChanges processing.

Our idea (as yet untried) is to:

  1. Create necessary foreign key properties in the metadata where they don't exist in the real model. These would be marked as "synthetic" somehow ('$' prefix, special property, etc).
  2. During the JSON serialization process, populate the synthetic foreign keys from the related entities or NH proxies.
  3. During the JSON deserialization process (when saving), carry the synthetic foreign key information along with the entity, so it can be used to re-establish relationships or create NH proxies.

Not a trivial exercise. We realize that it's important for developers who have existing N/Hibernate models, and don't want to modify them to use Breeze.




回答2:


I have downloaded NorthBreeze and took a quick look. Breeze has a notion of a FK map and I suspect the FK's are needed to link entities.

According to the docs...

Breeze associations require foreign keys.

This means that all foreign keys needs to be exposed in breeze so as to allow for navigation/retrieving related entities. In your example it most likely allows breeze to list users and their associated roles.

<property name="UserId" ... insert="false" update="false" />
<property name="RoleId" ... insert="false" update="false" />

The NHibernate mapping for these foreign keys disables these keys from changing directly. Therefore I would leave the mapping as is as it is not going to cause any noticeable impact on the code-base.



来源:https://stackoverflow.com/questions/22146668/breeze-nhibernate-many-to-one-relation-avoid-having-to-specify-relation-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!