@Entity
public class Troop {
@OneToMany(mappedBy="troop")
public Set<Soldier> getSoldiers() {
...
}
@Entity
public class Soldier {
@ManyToOne
@JoinColumn(name="troop_fk")
public Troop getTroop() {
...
}
I do something like this:
Troup t = new Troup();
t.getSoldiers().add(soldier);
This saves troop and soldier also. Foreign key is also added since soldier is the owner. But which one is better way, I mean saving toop or soldier using:
Troop t = ..... // Get troop from session
Soldier s = new Soldier();
s.setTroop(t);
You will arrive at the same result with both approaches. However, I suppose that saving individual Soldier entities is a little more efficient:
Soldier table gets updated anyway. However, perhaps you should think about which business concept fits better, meaning whether are you working with individual Soldiers or with Troops at this point in the application.
Soldier to a Troop orTroop to a SoldierThe difference may be subtle, but depending on you business case either one may be the more readable and comprehensible solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With