All,
Basically I want Foreign key automatically setup from Parent class
if possible.
Here is two mappings:
Code:
from class Person mapping file:
<set name="vehicles" table="Vehicles" cascade="save-update">
<key column="ssn"/>
<one-to-many class="com.dmv.Vehicle"/>
</set>
from class Vehicle mapping file:
<property name="ssn" type="string">
<column name="SSN" length="9" not-null="true"/>
</property>
and Here is test code:
Code:
/* save basic person info */
personDAO.save(person);
Vehicle vehicle = new Vehicle();
vehicle.setColor("Silver");
vehicle.setMakeModel("Ford/Mustang");
vehicle.setPlate("XXX 3344");
vehicle.setYear("2007");
vehicle.setMileage("100");
Set<Vehicle> vehicles = new HashSet<Vehicle>();
vehicles.add(vehicle);
person.setVehicles(vehicles);
/*add vehicle info to this person */
personDAO.update(person);
And it throw
PropertyValueException because of not-null property ssn from Vehicle class. If i remove not-null="true", then this works fine. but I want to make sure this SSN is not-null property.
Calling vehicle.setSSN(ssn); throws another exception as it try to update ssn property with null value first, and then update ssn property with correct value.
Can anybody knows how to fix this problem?
Many thanks.