Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Foo {
- private String id;
- private Bar bar;
- @Id
- @Column(name = "FOO_ID", unique = true, nullable = false, length = 16)
- @GeneratedValue(generator = "blahIdSeq")
- @GenericGenerator(name = "blahIdSeq",
- strategy = "org.blah.CustomIdGenerator")
- public String getId() {return id;}
- @JoinColumn(name = "FOO_ID")
- @OneToOne(fetch = FetchType.LAZY, optional = false)
- public Bar getBar() { return bar; }
- // SETTERS INCLUDED
- }
- public class Bar {
- private String id;
- private Foo foo;
- @Id
- @Column(name = "FOO_ID")
- @GeneratedValue(generator = "someSeq")
- @GenericGenerator(name = "someSeq",
- strategy = "foreign",
- parameters = {
- @Parameter(name = "property", value = "foo")
- })
- public String getId() { return id; }
- @OneToOne(fetch = FetchType.LAZY)
- @PrimaryKeyJoinColumn(name = "FOO_ID")
- public Foo getFoo() { return foo; }
- // SETTERS INCLUDED
- }
- public String createFoo(Foo foo) {
- Session ses = getSessionFactory().getCurrentSession();
- Bar bar = new Bar();
- bar.setFoo(foo);
- foo.setBar(bar);
- ses.save(foo);
- ses.save(bar);
- System.out.println(foo.getId()); // yields the ID value set by generator
- System.out.println(bar.getId()); // yields same ID value as above
- ses.flush();
- ses.refresh(foo);
- }
- public class Foo {
- private String id;
- private Bar bar;
- @Id
- @Column(name = "FOO_ID", unique = true, nullable = false, length = 16)
- @GeneratedValue(generator = "blahIdSeq")
- @GenericGenerator(name = "blahIdSeq",
- strategy = "org.blah.CustomIdGenerator")
- public String getId() {return id;}
- // SETTERS INCLUDED
- }
- public class Bar {
- private String id;
- private Foo foo;
- @Id
- @Column(name = "FOO_ID")
- public String getId() { return id; }
- // SETTERS INCLUDED
- }
- @Service('fooService')
- @Transactional(readOnly = true)
- class FooService {
- @Autowired
- SessionFactory sessionFactory // populated using Spring config:
- // org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
- @Transactional(readOnly = false)
- public void doSomeStuff(Foo fooToSave) {
- sessionFactory.getCurrentSession().saveOrUpdate(fooToSave);
- Bar bar = new Bar(fooToSave); // this populates the Bar.Id field
- sessionFactory.getCurrentSession().saveOrUpdate(bar);
- sessionFactory.getCurrentSession().flush();
- sessionFactory.getCurrentSession().refresh(fooToSave); // exception thrown here
- }
- }
- public class Foo {
- // OTHER FIELDS INCLUDING IDs AND SUCH
- private Baz bazOfDoom;
- private Baz bazOfLight;
- private Set<Baz> allTheBaz;
- @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
- @JoinColumns({
- @JoinColumn(name = "FOO_ID", referencedColumnName = "FOO_ID", insertable = false, updatable = false, nullable = false)
- @JoinColumn(name = "DOOM_ID", referencedColumnName = "STATE_ID", insertable = false, updatable = false, nullable = false)
- })
- public Baz getBazOfDoom() { return bazOfDoom; }
- @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
- @JoinColumns({
- @JoinColumn(name = "FOO_ID", referencedColumnName = "FOO_ID", insertable = false, updatable = false, nullable = false)
- @JoinColumn(name = "LIGHT_ID", referencedColumnName = "STATE_ID", insertable = false, updatable = false, nullable = false)
- })
- public Baz getBazOfLight() { return bazOfLight; }
- @OneToMany(mappedBy = "key.foo", fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
- public Set<Baz> getAllTheBaz() { return allTheBaz; }
- }
- @GeneratedValue(strategy = GenerationType.AUTO)
Add Comment
Please, Sign In to add comment