我试图使用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数据包(timeStamp)VALUES(?); SELECT LAST_INSERT_ID()] *
我确信这只是因为我对NHibernate缺乏了解,并且非常感谢你的帮助。
答案 0 :(得分:0)
您的标识列(ID)是否在数据库中实际定义为自动增量/标识?
您的时间戳是数据库时间戳列吗?如果它是你需要access =“readonly”因为nhibernate试图插入它。
此外,您的第一个加入的子类缺少或者它似乎在最后一个ICMP表之后的错误位置..除非我想念您的意图并且您希望它们嵌套。