Guest User

Untitled

a guest
Jan 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. public class Foo {
  2.  
  3. private String id;
  4. private Bar bar;
  5.  
  6. @Id
  7. @Column(name = "FOO_ID", unique = true, nullable = false, length = 16)
  8. @GeneratedValue(generator = "blahIdSeq")
  9. @GenericGenerator(name = "blahIdSeq",
  10. strategy = "org.blah.CustomIdGenerator")
  11. public String getId() {return id;}
  12.  
  13. @JoinColumn(name = "FOO_ID")
  14. @OneToOne(fetch = FetchType.LAZY, optional = false)
  15. public Bar getBar() { return bar; }
  16.  
  17. // SETTERS INCLUDED
  18.  
  19. }
  20.  
  21. public class Bar {
  22. private String id;
  23. private Foo foo;
  24.  
  25. @Id
  26. @Column(name = "FOO_ID")
  27. @GeneratedValue(generator = "someSeq")
  28. @GenericGenerator(name = "someSeq",
  29. strategy = "foreign",
  30. parameters = {
  31. @Parameter(name = "property", value = "foo")
  32. })
  33. public String getId() { return id; }
  34.  
  35. @OneToOne(fetch = FetchType.LAZY)
  36. @PrimaryKeyJoinColumn(name = "FOO_ID")
  37. public Foo getFoo() { return foo; }
  38.  
  39. // SETTERS INCLUDED
  40.  
  41. }
  42.  
  43. public String createFoo(Foo foo) {
  44. Session ses = getSessionFactory().getCurrentSession();
  45. Bar bar = new Bar();
  46. bar.setFoo(foo);
  47. foo.setBar(bar);
  48. ses.save(foo);
  49. ses.save(bar);
  50.  
  51. System.out.println(foo.getId()); // yields the ID value set by generator
  52. System.out.println(bar.getId()); // yields same ID value as above
  53.  
  54. ses.flush();
  55. ses.refresh(foo);
  56. }
  57.  
  58. public class Foo {
  59.  
  60. private String id;
  61. private Bar bar;
  62.  
  63. @Id
  64. @Column(name = "FOO_ID", unique = true, nullable = false, length = 16)
  65. @GeneratedValue(generator = "blahIdSeq")
  66. @GenericGenerator(name = "blahIdSeq",
  67. strategy = "org.blah.CustomIdGenerator")
  68. public String getId() {return id;}
  69.  
  70. // SETTERS INCLUDED
  71.  
  72. }
  73.  
  74. public class Bar {
  75. private String id;
  76. private Foo foo;
  77.  
  78. @Id
  79. @Column(name = "FOO_ID")
  80. public String getId() { return id; }
  81.  
  82. // SETTERS INCLUDED
  83.  
  84. }
  85.  
  86. @Service('fooService')
  87. @Transactional(readOnly = true)
  88. class FooService {
  89. @Autowired
  90. SessionFactory sessionFactory // populated using Spring config:
  91. // org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
  92.  
  93. @Transactional(readOnly = false)
  94. public void doSomeStuff(Foo fooToSave) {
  95. sessionFactory.getCurrentSession().saveOrUpdate(fooToSave);
  96. Bar bar = new Bar(fooToSave); // this populates the Bar.Id field
  97. sessionFactory.getCurrentSession().saveOrUpdate(bar);
  98. sessionFactory.getCurrentSession().flush();
  99. sessionFactory.getCurrentSession().refresh(fooToSave); // exception thrown here
  100. }
  101. }
  102.  
  103. public class Foo {
  104.  
  105. // OTHER FIELDS INCLUDING IDs AND SUCH
  106.  
  107. private Baz bazOfDoom;
  108. private Baz bazOfLight;
  109. private Set<Baz> allTheBaz;
  110.  
  111. @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
  112. @JoinColumns({
  113. @JoinColumn(name = "FOO_ID", referencedColumnName = "FOO_ID", insertable = false, updatable = false, nullable = false)
  114. @JoinColumn(name = "DOOM_ID", referencedColumnName = "STATE_ID", insertable = false, updatable = false, nullable = false)
  115. })
  116. public Baz getBazOfDoom() { return bazOfDoom; }
  117.  
  118. @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
  119. @JoinColumns({
  120. @JoinColumn(name = "FOO_ID", referencedColumnName = "FOO_ID", insertable = false, updatable = false, nullable = false)
  121. @JoinColumn(name = "LIGHT_ID", referencedColumnName = "STATE_ID", insertable = false, updatable = false, nullable = false)
  122. })
  123. public Baz getBazOfLight() { return bazOfLight; }
  124.  
  125. @OneToMany(mappedBy = "key.foo", fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
  126. public Set<Baz> getAllTheBaz() { return allTheBaz; }
  127. }
  128.  
  129. @GeneratedValue(strategy = GenerationType.AUTO)
Add Comment
Please, Sign In to add comment