连接子类问题的 NHibernate 子类

发布于 2024-08-09 22:56:46 字数 3320 浏览 9 评论 0原文

我有一个应用程序,它有一个核心程序集,其中包含我需要继承的基类。我需要将它们保存到数据库中,在阅读了 NHibernate 后决定使用它。

但是,我的新继承类之一有问题。我已经设置了子类映射,但是当我保存时,它既没有尝试保存它的任何基类属性,也没有尝试保存我在映射中分配的任何新属性!

我的类的布局如下:(来自一个小型演示应用程序)

核心程序集 数据项-> User

任何会接触数据库的东西都会继承 DataItem 类,因为它处理 id、修改日期等。

在我的测试中,我将用户设置为只有名字。

如果我保存一个新用户,它会很好用。但是当我从 user 继承,然后添加另一个名为 LastName 的属性并尝试保存这个新对象。它仅将 INSERT INTO t_User (id) VALUES(?) 的 sql 语句放在一起。它不会尝试保存名字或姓氏..尽管两者都已设置并已映射。

我的 nhibernate.config:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory name="DAL">
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="adonet.batch_size">16</property>
    <property name="current_session_context_class">web</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    <mapping assembly="DAL"/>
    <mapping assembly="NHibernateDemo"/>
  </session-factory>
</hibernate-configuration>

如您所见,我有 2 个程序集。我的 DAL 是我的核心,NHibernateDemo 是一个使用核心进行继承的 Web 应用程序。

我的核心 DataItem 映射:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DAL" namespace="DAL.Model">
  <class name="DataItem" table="t_DataItem" >
    <id name="Id">
      <generator class="native" />
    </id>
    <discriminator column="typeid" type="System.Int32"></discriminator>
    <property name="IsActive" column="isActive" not-null="true" />
    <property name="TypeId" column="typeId" not-null="true"></property>
    <many-to-one name="Parent" column="ParentId" class="DataItem"></many-to-one>
    <bag name="Children" cascade="all-delete-orphan">
      <key column="ParentId"></key>
      <one-to-many class="DataItem"/>
    </bag>
    <joined-subclass name="User" table="t_Users">
      <key column="id"></key>
      <property name="FirstName" column="firstName" not-null="true" ></property>
    </joined-subclass>
    <joined-subclass name="Email" table="t_Emails">
      <key column="emailid"></key>
      <property name="Address" column="Address"></property>
    </joined-subclass>
  </class>
</hibernate-mapping>

我继承的 NewUser 映射不起作用!:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateDemo" namespace="NHibernateDemo.Model">
  <subclass name="NewUser" extends="DAL.Model.User, DAL" discriminator-value="1">
    <property name="LastName" column="LastName"></property>
  </subclass>

为什么当我尝试保存我的类 NewUser 时,它不会尝试保存任何其他属性集,无论是来自它的基础属性还是新声明的属性特性?

我真的很感激对此的任何帮助或见解..我一定错过了一些非常简单的东西,但我只是看不到它。

谢谢,

迈克

I have an application that has a core assembly with base classes that I need to inherit from.. I need to save these to the database and after reading about NHibernate decided to use it.

However I have a problem with one of my new inherited classes.. I have setup the subclass map but when I save, it neither attempts to save any of it's base class properties or any of it's new ones that I have assigned in the mapping!

My classes are laid out like the following: (from a small demo app)

core assemblies
DataItem -> User

Anything that will touch the database inherits the DataItem class as it handles the id, modified date etc etc..

In my test I setup user to only have a FirstName..

If I save a new User it works great.. however when I inherit from user and then add another property called LastName and attempt to save this new object.. it only puts a sql statement together of INSERT INTO t_User (id) VALUES(?).. it doesn't attempt to save the first name or last name.. either though both have been set and are mapped.

My nhibernate.config:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory name="DAL">
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="adonet.batch_size">16</property>
    <property name="current_session_context_class">web</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    <mapping assembly="DAL"/>
    <mapping assembly="NHibernateDemo"/>
  </session-factory>
</hibernate-configuration>

As you can see I have 2 assemblies.. my DAL is my core and the NHibernateDemo is a web application that uses the core for inheritance.

My core DataItem mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DAL" namespace="DAL.Model">
  <class name="DataItem" table="t_DataItem" >
    <id name="Id">
      <generator class="native" />
    </id>
    <discriminator column="typeid" type="System.Int32"></discriminator>
    <property name="IsActive" column="isActive" not-null="true" />
    <property name="TypeId" column="typeId" not-null="true"></property>
    <many-to-one name="Parent" column="ParentId" class="DataItem"></many-to-one>
    <bag name="Children" cascade="all-delete-orphan">
      <key column="ParentId"></key>
      <one-to-many class="DataItem"/>
    </bag>
    <joined-subclass name="User" table="t_Users">
      <key column="id"></key>
      <property name="FirstName" column="firstName" not-null="true" ></property>
    </joined-subclass>
    <joined-subclass name="Email" table="t_Emails">
      <key column="emailid"></key>
      <property name="Address" column="Address"></property>
    </joined-subclass>
  </class>
</hibernate-mapping>

My inherited NewUser mapping that doesn't work!:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateDemo" namespace="NHibernateDemo.Model">
  <subclass name="NewUser" extends="DAL.Model.User, DAL" discriminator-value="1">
    <property name="LastName" column="LastName"></property>
  </subclass>

Why is it that when I attempt to save my class NewUser that it doesn't attempt to save any of the other properties set, whether from it's base or newly declared properties?

I'd really appreciate any help or insight to this.. I must be missing something really simple and I just can't see it.

Thanks,

Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你又不是我 2024-08-16 22:56:46

您可能无法在同一类层次结构中混合子类和连接子类映射。由于您的 DataItem 和 User 通过连接子类相关,因此您可能需要使 NewUser 类成为 User 的另一个连接子类。

另一个问题可能是在当前的 NewUser 映射中使用“鉴别器”。鉴别器应该是用户表中的附加列,NHibernate 使用它来区分用户记录和 NewUser 记录。我不确定这是否适用于 User 基类可能未指定其鉴别器值,而 NewUser 则指定的情况。我不确定您是否在任何地方指定了鉴别器列,这也可能是一个问题。

我建议首先尝试使 NewUser 成为 User 的连接子类。

It could be that you're not able to mix subclass and joined-subclass mappings in the same class hierarchy. Since your DataItem and User are related by joined-subclass, you may need to make your NewUser class another joined-subclass of User.

Another issue might be the use of the "discriminator" in your current NewUser mapping. The discriminator should be an additional column in your User table that NHibernate uses to tell the difference between a User record and a NewUser record. I'm not sure if this works where the User base class might not specify its discriminator value, whereas the NewUser does. I'm not sure if you're specifying the discriminator-column anywhere, which might also be a problem.

I would suggest first trying to make NewUser a joined-subclass of User.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文