0

我正在尝试使用 NHibernate 映射多个级别的子类,我必须承认我是新手。我正在映射的数据是网络数据包捕获。

基本上,我希望能够在我的数据库中的表上拆分对象的不同继承级别,例如,当我收到 TCP 数据包时,时间戳等非常一般的数据存储在“数据包”表中,并且更具体的数据存储在其各自的表中,例如“IP”表中的IP头和“TCP”表中的TCP头。

我的映射

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="WindowsFormsApplication1.BasePacket, WindowsFormsApplication1" table="Packets" lazy="false">

    <id name="ID" column="ID">
      <generator class="identity" />
    </id>

    <property name="timeStamp" column="timeStamp" type="datetime"></property>

    <joined-subclass table="IP" name="WindowsFormsApplication1.IP, WindowsFormsApplication1" lazy="false">
      <key column="IPID"/>

      <property name="identification" column="identification" type="UInt16"></property>
      <property name="sourceIP" column="sourceIP" type="BinaryBlob" ></property>
      <property name="destinationIP" column="destinationIP" type="BinaryBlob"></property>
      <property name="version" column="version" type="int"></property>
      <property name="IPHeaderLength" column="IPHeaderLength" type="byte"></property>
      <property name="sizeOfDatagram" column="sizeOfDatagram" type="UInt16"></property>
      <property name="reserved" column="reserved" type="bool"></property>
      <property name="dontFragment" column="dontFragment" type="bool"></property>
      <property name="moreFragments" column="moreFragments" type="bool"></property>
      <property name="fragmentOffset" column="fragmentOffset" type="UInt16"></property>
      <property name="timeToLive" column="timeToLive" type="byte"></property>
      <property name="protocol" column="protocol" type="int"></property>
      <property name="headerChecksum" column="headerChecksum" type="UInt16"></property>
      <property name="additionalData" column="additionalData" type="BinaryBlob"></property>

      <joined-subclass table="TCP" name="WindowsFormsApplication1.TCP, WindowsFormsApplication1" lazy="false">
        <key column="TCPID"/>

        <property name="sourcePort" column="sourcePort" type="UInt16"></property>
        <property name="destinationPort" column="destinationPort" type="UInt16"></property>
        <property name="ISN" column="ISN" type="long"></property>
        <property name="ASN" column="ASN" type="long"></property>
        <property name="innerProtocolHeaderLength" column="innerProtocolHeaderLength" type="byte"></property>
        <property name="FIN" column="FIN" type="bool"></property>
        <property name="SYN" column="SYN" type="bool"></property>
        <property name="RST" column="RST" type="bool"></property>
        <property name="PSH" column="PSH" type="bool"></property>
        <property name="ACK" column="ACK" type="bool"></property>
        <property name="URG" column="URG" type="bool"></property>
        <property name="windowSize" column="windowSize" type="UInt16"></property>
        <property name="checksum" column="checksum" type="UInt16"></property>
        <property name="urgentPointer" column="urgentPointer" type="UInt16"></property>
        <property name="options" column="options" type="BinaryBlob"></property>
        <property name="payload" column="payload" type="BinaryBlob"></property>
        <property name="anomalies" column="anomalies" type="string"></property>
      </joined-subclass>

      <joined-subclass table="UDP" name="WindowsFormsApplication1.UDP, WindowsFormsApplication1" lazy="false">
        <key column="UDPID"/>

        <property name="sourcePort" column="sourcePort" type="UInt16"></property>
        <property name="destinationPort" column="destinationPort" type="UInt16"></property>
        <property name="length" column="length" type="UInt16"></property>
        <property name="checksum" column="checksum" type="UInt16"></property>
        <property name="payload" column="payload" type="BinaryBlob"></property>
      </joined-subclass>

      <joined-subclass table="ICMP" name="WindowsFormsApplication1.ICMP, WindowsFormsApplication1" lazy="false">
        <key column="ICMPID"/>

        <property name="type" column="type" type="byte"></property>
        <property name="code" column="code" type="byte"></property>
        <property name="checksum" column="checksum" type="UInt16"></property>
        <property name="additionalDataICMP" column="additionalDataICMP" type="BinaryBlob"></property>
      </joined-subclass>
    </joined-subclass>
  </class>
</hibernate-mapping>

当我尝试使用此映射运行我的程序时,我收到此错误消息,或类似的错误消息,指的是不同的子类。

* NHibernate.dll 中发生了“NHibernate.Exceptions.GenericADOException”类型的未处理异常

附加信息:无法插入:[WindowsFormsApplication1.UDP][SQL: INSERT INTO Packets (timeStamp) VALUES (?);SELECT LAST_INSERT_ID()]*

我确信这只是因为我对 NHibernate 缺乏了解,非常感谢您的帮助。

4

1 回答 1

0

您的身份列 (ID) 是否在数据库中实际定义为自动增量/身份?

您的时间戳是数据库时间戳列吗?如果是你需要 access="readonly" 因为 nhibernate 正在尝试插入它。

此外,您的第一个加入的子类缺少或者它似乎在最后一个 ICMP 表之后的错误位置.. 除非我错过了您的意图并且您希望它们嵌套。

于 2012-05-25T02:59:27.217 回答