Hibernate version: 2.12
Mapping documents:
<class name="com.trivin.bo.vehicle.Registration" table="REGISTRATION">
<!-- registrationID is generated/taken from associated Vehicle. Used registrationID -->
<!-- In reality, the registrationID "IS" the vehicleID. Registrations cannot live in absence of vehicle -->
<id name="registrationID" column="VehicleID">
<generator class="foreign">
<param name="property">vehicle</param>
</generator>
</id>
<discriminator column="JavaSource" type="string"/>
<property name = "productID" />
<one-to-one name="vehicle"
class="com.trivin.bo.vehicle.Vehicle"
constrained="true"
/>
<property name="plateState" />
<component name="plate" class="com.trivin.bo.inventory.Plate">
<property name="plateNumber" />
<property name="plateClass" type="trivin.test.persist.PlateClassUserType" column="plateClassCode"/>
<property name="plateSubClass" type="trivin.test.persist.PlateSubClassUserType" column="plateSubClassCode"/>
<property name="plateStyle" type="trivin.test.persist.PlateStyleUserType" column="plateStyleCode" />
</component>
<property name="plateType" />
<property name="expirationDate" />
<property name="grossWeight" />
<property name="unladenWeight" />
</class>
Code between sessionFactory.openSession() and session.close():
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Vehicle veh = (Vehicle) session.get(Vehicle.class, new Long(id));
tx.commit();
session.close();
Full stack trace of any exception that occurs:
Name and version of the database you are using:
SQL 2000
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
I've placed an excerpt of a larger mapping file above, with the section of interest colored green. Basically, what i have is a Plate object inside of Registration. The Plate object contains other attributes. I'm limiting this to the PlateClass portion, since the other attributes below that are essentially the same pattern.
Example of real data might be:
PlateNumber: ABC123
PlateClassCode: COMM
...
So the data in the Registation Table would have those items.
I want to reconstruct Registration object properly when loading it form the DB.
It needs to contain a Plate Object that contains a string that is platenumber (no problem), but then it needs to get the PlateClassCode ("COMM" in this example) and with that code, i can fetch the appropriate PlateClass Object from our system and return /set it in the Plate Object.
The way i've attempted to implement this is to have Plate as a component containing PlateNumber and PlateClassCode etc. So far, so good. I then created my own UserType "trivin.test.persist.PlateClassUserType".
Still looking good. Below is the method inside my class that does, indeed, get called properly. I've properly indicated in the other required methods the type of object i'll be returning.
Code:
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException,
SQLException {
String name = rs.getString(names[0]);
return rs.wasNull() ? null : getInvType(name, (BusinessObject) owner, getInvTypeType());
When i run the sample, "COMM" is passed properly, here, and i'm ready to pass back the appropriate PlateClass Object. I call a private method to do this. The trick, here, is that in order for me to fetch the object from our system, i also need the productID. The productID is part of Object owner. Indeed, it is present in nearly all of our business objects. You can see it in the above map.
And here's the rub, when i look at the "owner" object in the above code, it is Registration (as it should be). However, at this point in time, productID has not been loaded. In fact, nothing exists inside the owner object that is passed here.
Hibernate samples and documentation indicate that data should be present there. In fact, that is why owner is passed so that i have access to other information that would help me do my job (i think that was the intent). Are there conditions where this would not be the case?
Is it related to the fact that i'm doing this inside of a component? Could it be that components are handled 1st, then non-components which would mean that data outside of components is not yet available?
Of course this raises a the question as to what data is available when, in general, since i'm assuming there needs to be some ordering of how/when data is loaded into the objects.
Any help will be very much appreciated. We have this condition in many places in our system. We store codes, and then reconstruct immutable objects from these codes using productID.
thanks for any input on this.