I'm defined a bi-directional many-to-one relationship between two entities in Spring Roo.
@RooEntity
public class Car {
@OneToMany(mappedBy="car")
private Set<Wheel> wheels = new HashSet<Wheel>();
}
@RooEntity
public class Wheel {
@ManyToOne
@JoinColumn (name = "wheels_fk")
private Car car;
}
Changes on the owner-side (Wheel) are persisted.
When I try to update anything from the Car-entity it doesn't work though.
What can I do about this?
The answer is in the question: the owner side of the association is Wheel, and Hibernate will only use the owner side to decide if an association exists or not. You should always update the owner side when updating the non-owner side (and vice-versa, if you want a coherent object graph). The most robust way is to encapsulate it in the Car entity:
public void addWheel(Wheel w) {
this.wheels.add(w);
w.setCar(this);
}
public void removeWheel(Wheel w) {
this.wheels.remove(w);
w.setCar(null);
}
public Set<Wheel> getWheels() {
// to make sure the set isn't modified directly, bypassing the
// addWheel and removeWheel methods
return Collections.unmodifiableSet(wheels);
}