Your question sounds like a collection of @Embeddable class where a component (The part - Wheel) has its lifecycle bound to that of its owning entity instance (Automobile)
@Entity
public class Automobile {
private Integer id;
private Set<Wheels> wheelsSet = new HashSet<Wheels>();
@Id
@GeneratedValue
public Integer getId() {
return this.id;
}
@CollectionsOfElements
@JoinTable("AUTO_WHEELS")
public Set<Wheels> getWheelSet() {
return this.wheelsSet;
}
}
If you do
Automobile auto = session
.createQuery("from Automobile a left join fetch a.wheelSet where a.id = :id")
.setParameter("id", id)
.list().get(0);
And
/**
* All of wheels removed
*/
auto.getWheelSet().clear();
/**
* New Wheels added
*/
auto.getWheelSet().add(new Wheel());
auto.getWheelSet().add(new Wheel());
auto.getWheelSet().add(new Wheel());
auto.getWheelSet().add(new Wheel());
session.update(auto);
It will fullfil your needs. But keep in mind you must provide equals and hashCode implementation To work fine
@Embeddable
public class Wheel implements Serializable {
/**
* equals and hashCode goes here
* Prefer To use business key
*/
}