I'm using Hibernate 3.3.2.ga, Hibernate EntityManager 3.3.1.ga, with Hibernate Annotations 3.3.0.ga
I have a configuration identical to the @OneToMany Bidirectional one in the documentation:
Code:
@Entity
public class Troop {
@OneToMany(mappedBy="troop")
public Set<Soldier> getSoldiers() {
...
}
@Entity
public class Soldier {
@ManyToOne
@JoinColumn(name="troop_fk")
public Troop getTroop() {
...
}
However, whenever I do something like this:
Code:
Troop troop = new Troop();
HashSet soldierSet = new HashSet();
soldierSet.add(new Soldier("soldier1"));
soldierSet.add(new Soldier("soldier2"));
troop.setSoldiers(soldierSet);
mgr.save(troop);
All three objects are saved, but the troop_fk column in each Soldier is null.
Seeing that, as expected, adding @JoinColumn(name="troop_fk",
nullable="false") throws a PersistenceException
It seems logical that one would want the troop_fk to be automatically populated -- is there a configuration I'm missing here? Or is it just expected that I will have to:
1) create the Troop first (no soldiers)
2) do a setTroop(troop) on each new Soldier
3) add the Soldiers to the Troop
4) re-save the Troop?