Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 14th, 2012  |  syntax: None  |  size: 0.93 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. What does a relationship owner mean in Hibernate?
  2. @Entity
  3. public class Troop {
  4.     @OneToMany(mappedBy="troop")
  5.     public Set<Soldier> getSoldiers() {
  6.     ...
  7. }
  8.  
  9. @Entity
  10. public class Soldier {
  11.     @ManyToOne
  12.     @JoinColumn(name="troop_fk")
  13.     public Troop getTroop() {
  14.     ...
  15. }
  16.        
  17. Troop has a bidirectional one to many relationship with Soldier through the troop property. You don't have to (must not) define any physical mapping in the mappedBy side.
  18.        
  19. Troup t = new Troup();
  20. t.getSoldiers().add(soldier);
  21.        
  22. Troup t = new Troup();
  23. t.getSoldiers().add(soldier);
  24. session.SaveOrUpdate(t);
  25. session.Flush();
  26.        
  27. throws references transient instances
  28.        
  29. INSERT INTO troops (id, ...) VALUES (1, ...)
  30. INSERT INTO soldiers (..., troop_fk) VALUES (..., NULL)
  31. UPDATE soldiers SET troop_fk=1    <- troop sets its key
  32.        
  33. INSERT INTO troops (id, ...) VALUES (1, ...)
  34. INSERT INTO soldiers (..., troop_fk) VALUES (..., 1) <- soldier saves the reference