If your association is bidirectional (which seems to be your intention), you should declare a Garage type attribute (instead of an integer) and map it in Vehicle.hbm.xml.
(oversight?)
Something like:
Code:
<many-to-one name="garage"
class="Garage"
column="GARAGE_ID"/>
Moreover your Set is initialisation is quite ugly.
Just write :
Code:
private Set<Vehicle> vehicles = new HashSet();
If you actually choose to set the inverse & cascade attributes on the Garage side, you should add a "addVehicle(...)" method which would link the vehicle to the the garage:
Code:
public void addVehicle(Vehicle v){
vehicles.add(v);
v.setGarage(this);
}
So your main method becomes:
Code:
tx = session.beginTransaction();
Garage garage = new Garage("name");
Vehicle vehicle = new Car("name");
garage.addVehicle(vehicle);
session.save(garage);
tx.commit();
To conclude, may I advise you to read the following sections of the Reference Manual:
1.3.5. Bi-directional associations
1.3.6. Working bi-directional links
of course the reading of the full manual may be usefull ;)
Alexis
Don't forget to rate ;)