I'm trying to solve a problem and beginning to think i'm barking up the wrong tree. Have tried using custom accessors, and ran into issues etc.
I will try to describe what i'm up against. I'll define a few classes. Assume proper getter/setters exist.
Code:
public class VehicleRegistration{
private Date dateIssued;
private int grossWeight;
private Plate plate;
...
}
public class Plate{
private String plateNumber;
private InvType classType;
private InvType classStyle;
}
// objects of this type are generally immutable, there is no
// public constructor
public class InvType{
public static InvType getByCode(String code){
// i won't bother putting the code here, but it will provide
// the appropriate constant InvType given a code.
}
public static InvType TYPE_PASS = new (InvType("PAS", "Passenger");
public static InvType TYPE_COMM = new (Invtype("COMM","Commercial");
public InvType STYLE_REG = new (InvType("REG","Regular");
public InvType STYLE_VANITY = new (InvType("VAN", "Vanity");
private String code;
private String description;
}
okay, that is a pretty simplified version of what i've got. Suffice it to say, there are more than just InvType classes that i've got to deal with.
Now what i need to do is to map the VehicleRegistration class. This object links back to Vehicle, that works, not to worry about that portion of the map. The area of interest is in Plate. My plan is to store in this particular table, PlateNumber, plateClassCode, and plateStyleCode. The codes will be written to the table. Upon reading them from the table, the appropriate InvType object (within Plate) will then be reconstructed from the code using the public static call.
Code:
<class name="VehicleRegistration" table="REGISTRATION">
<id name="registrationID" column="VehicleID">
<generator class="foreign">
<param name="property">vehicle</param>
</generator>
</id>
<discriminator column="JavaSource" type="string"/>
<one-to-one name="vehicle"
class="com.trivin.bo.vehicle.Vehicle"
constrained="true"
/>
<property name="grossWeight" />
<property name="dateIssued" />
<component name="plate" class="Plate">
<property name="plateNumber" />
<property name="plateClass" />
<property name="plateStyle" />
</component>
</class>
1st thought i could handle this by defining the component section thusly
Code:
<component name="plate" class="Plate">
<property name="plateNumber" column = "plateNumber" />
<property name="plateClass" access="myAccessor" column="plateClassCode" />
<property name="plateStyle" access="myAccessor" column="plateStyleCode" />
</component>
And i thought myAccessor would map between the code and the object. Got it all coded up, but it appeared that Hibernate really didn't like it. I would get:
net.sf.hibernate.MappingException: Could not determine a property type for: plateClass
so then i added a type="InvType" in the property tag, and it complained about.
net.sf.hibernate.MappingException: Could not interpret type: InvType
And then i started looking at defining InvType in the mapping file. And at this point, i was wondering if i was even remotely solving this problem in a reasonable way.
I looked at using a custom user type, but it appeared that that solved more of the problem of getting in and out of the database. I didn't feel that that would solve my problem completely, and also i wanted to use this "pattern" if you will in other classes that contain plate, and didn't want to tie the actual location in the DB to this component. Because Plate can live in a lot of different places, i really just wanted to store codes, and then reconstruct the appropriate InvType.
Anybody have any suggestions on how to approach this problem?
appreciate any help or suggestions.
thanks