gpt4 book ai didi

NHibernate 映射外键

转载 作者:行者123 更新时间:2023-12-03 19:47:34 27 4
gpt4 key购买 nike

我有两个表,Vehicle 和 Make。使用 MakeId 作为 Vehicle 表上的外键将两者连接起来。我的映射文件看起来与此类似

    <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Demo.Business.Objects.Vehicle, Demo.Business.Objects" table="Vehicle" >
<id name="VehicleId" type="int" >
<generator class="native" />
</id>
<property name="RegNumber" type="String" />
<property name="VehicleId" type="int" />
<property name="CustomerId" type="int" />

<join table="Make" fetch="join">

<key column="MakeId" foreign-key="MakeId"/>
<property name="Description" type="String" />
</join>
</class>
</hibernate-mapping>

我原以为这会在 make id 上连接两个表,但是 ios 生成的 SQL 尝试以下连接:vehicle.vehicleid = make.makeid。

我怎样才能让它发挥作用? IE。我希望:

    select * from Vehicle
inner join Make on Make.MakeId = Vehicle.Make Id

最佳答案

您需要以不同方式映射 Make 类:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Demo.Business.Objects.Vehicle, Demo.Business.Objects" table="Vehicle" >
<id name="VehicleId" type="int" >
<generator class="native" />
</id>
<property name="RegNumber" type="String" />
<property name="VehicleId" type="int" />
<property name="CustomerId" type="int" />
<many-to-one name="Make" column="MakeId"/>
</class>
<class name="(blahblah).Make, blahblah">
<id name="MakeId" type="int">
<generator class="native" />
</id>

<property name="Description" type="String" />
</class>
</hibernate-mapping>

您的“Vehicle”类应该有一个 Make 类型的 Make 属性。

关于NHibernate 映射外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1560563/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com