我与Car的关系@OneToMany - >工具。工具在汽车中使用后,先前已在系统中注册。我有以下代码:
public void addToolToCar(ActionEvent event){
if (car == null)
car = new Car();
Tool tool = findToolByNumer('1234');
tool.setSituation('PUTTED');
car.addTool(tool);
/*
After adding the first Tool object to Car, everything is fine, but when i try
to find the Tool '667', the Hibernate UPDATE in PostgreSQL the Tool '1234'.
The BIG problem is that user can choose don't click in "save button" and the Car will not inserted (this is complete normally and often, as a CANCEL action) but the Tool
situation was changed, this is a problem for my bussiness rule because when Tool
is in "PUTTED" situation, this Tool cannot be used for another Car.
And have a wrost situation, the Tool can be removed from the Car list, like:
'car.removeTool('1234'), but as Tool was changed in Database i will have a serius
problem of consistence.
*/
tool = findToolByNumer('667');
tool.setSituation('PUTTED');
car.addTool(tool);
}
public void save(ActionEvent event){
dao.persist(car);
}
EDIT1:Car类中的addTool方法非常简单,没有任何错误
public Car(){
tools = new ArrayList<Tool>();
}
private List<Tool> tools;
...
public void addTool(Tool tool){
tools.add(tool);
}