我有2个实体:车辆和引擎。它们具有一对一的关系,并且发动机的寿命取决于车辆的寿命,因此它们被映射为共享数据库中的一个PK。 当我加载整个Vehicle实体时,我具有所有基本属性,包括已加载的Engine。但是,当我尝试搜索车辆并仅加载某些属性时,我最终获得了Engine属性的空值。这是我尝试过的映射和查询以及我希望获得的结果:(请原谅,我删除了一些代码,使其尽可能地被束缚,并且仍然可以理解,有些代码可能看起来不正确,但是它在项目中)
车辆映射:
<hibernate-mapping>
<class name="Vehicle" table="VEHICLE" lazy="false">
<id name="id" type="long" access="field">
<!-- ordinary code, skip it -->
</id>
<discriminator column="VEHICLE_TYPE" type="string" />
<property type="string" name="description" column="description" access="field" not-null="true" length="5000" />
<one-to-one
name="engine"
fetch="join"
access="field"
entity-name="Engine"
cascade="all"
/>
<subclass name="Car" discriminator-value="CAR"></subclass>
</class>
</hibernate-mapping>
引擎映射:
<hibernate-mapping>
<class name="Engine" table="ENGINE" lazy="false">
<id name="id" column="vehicle_id" type="long" access="field">
<generator class="foreign">
<param name="property">vehicle</param>
</generator>
</id>
<one-to-one
name="vehicle"
access="field"
entity-name="Vehicle"
constrained="true"
/>
<property type="int" name="horsePower" column="horsePower" access="field" not-null="true" />
</class>
</hibernate-mapping>
从car car中选择car.description->具有描述值的一个属性
从Car car中选择car.description,car.engine->描述值和一个null属性即引擎。
从car car中选择car.description,car.engine.horsePower->引擎的描述值和horsePower属性。
选择car.description,car.engine.horsePower来自汽车左联接car.engine carEngine->与上一个结果相同。
从car car左侧联接中选择car.description,car.engine car.engine carEngine->再次加载了description属性,并为引擎提供了空值。
Object[3] = [
0: "Audi",
1: [Engine[id=1]] <- here is the object engine with its simple properties
2: "some-other-basic-car-property"
]