Hibernate version:
2.1.1
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
none
Name and version of the database you are using:
Postgres 7.4
Debug level Hibernate log excerpt:
none
Hi all,
I'm using Hibernate in 3tier architecture with objects loaded in one session and saved(uptaded) in another.
The problem occur when I update/save one object.
I have an object (the Car class) that have an attribute engine (the Engine class). When I load the car (ex. car.id=100) the engine attribute is null (car.engine=null). Then, in GUI, the user select an engine for this car in a JComboBox. Each element of JComboBox model is a tuple of "id" (Long) and "name" (String) of Engine (not an engine object, because its big and only relevant properties are loaded in JComboBox).
The cascades options between car and engine are "save-update".
Then, the client code to save/update is:
Code:
//currentCar was loaded in one session from app server.
currentCar.setColor(jtxtCarColor.getText());
//Get selected id of engine and create an engine object with this id
Long idSelected = (Long) ((Object[]) jcmbCarEngine.getSelectedItem())[0];
Engine engineSelected = new Engine();
engineSelected.setId(idSelected);
//Update engine property in currentCar
currentCar.setEngine(engineSelected);
//Store car
persistence.saveOrUpdate(currentCar);
The update ocours with succes for "currentCar", but (obviously) the "engineSelected" are updated too and its properties are lost (setted with null) in db.
My question is: How to update "currentCar" but not lost "engineCar" properties in db using this approach?