Hi,
I'm using a Criteria to get a Bean with its ID and try to fetch some of its relationships dynamically.
I checked the forum and couple other sites but I couldn't find something similar. Sorry if it has been discussed before.
The code is:
Code:
Criteria oCriteria = mSession.createCriteria(Vehicle.class);
oCriteria.add(Expression.idEq(123));
oCriteria.SetFetch("parts", FetchMode.JOIN);
List<Vehicle> oResults = (List<Vehicle>) oCriteria.list();
I'm expecting one result but I actually get the Vehicle + the parts mapped to it.
So if this Vehicle has 2 parts I'm getting 3 results like:
VehiclePart#345
Vehicle#123
VehiclePart#567
I'm using Criteria to be able to use the Fetch functionnality (I'm trying to avoid the Hibernate.initialize() or the OpenSessionInView pattern).
Code:
<class name="Vehicle" table="vehicles">
<id name="vehicleID" unsaved-value="0">
<generator class="native" />
</id>
<property name="creationTime" not-null="true"/>
<set name="parts" table="VehicleToParts" lazy="true">
<key column="vehicleID" not-null="false"/>
<many-to-many column="partID" class="VehiclePart"/>
</set>
</class>
Am I doing it the wrong way?
Is there a way to get only the bean I'm querying (i.e. the class specified in the Criteria)?