Session.flush () is not working properly

Inside the service class, I have a method that is called from a method @Transactional. I checked that my transaction is active at the point where this code is called. I understand that I do not have a DA level when I should, but I work with an outdated application that makes things in the “right” ways more complicated than it costs at this stage.

The mappings are as follows:

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

}

The method looks something like this:

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);
}

Now that org.hibernate.SQLlogging is set to DEBUG, I see that the insert instructions for Foo and Bar are created, but the update after the flash is called throws a org.hibernate.UnresolvableObjectException: No row with the given identifier existsexception.

What could be the reason for this? The database used is Oracle 11gR2.

UPDATE

. , currentSession.flush() , . , .

flush/refresh , , ( ) . , .

, ?

, , . , , , . , :

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
  }
}

, Oracle-land, , SQL .., . , Hibernate , SQL , . Oracle 11gR2 V$SQL_BIND_CAPTURE, SQL-, ( insert), 24 , . , , , . , .

, , Oracle . JDBC , INSERT , , Hibernate . Hibernate 3.6.10 - 3.6.5, , . .: P

MISLEAD

" " . , V$SQL_BIND_CAPTURE , . .

- ,

. , ? . , Hibernate XML . , , , , Foo, . , ? . , , - , Foo. , , SO, , , . - , , , .

! ...

, . , BAZ , , @Embeddable ( "" ), FOO_ID, Foo, STATE_ID .

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; }
}

, . . , , " ".:)

+5
1

, , , refresh().

, , , auto-increment. , Foo, : 1.

Hibernate save()!

- , Hibernate , .

, , , :

@GeneratedValue(strategy = GenerationType.AUTO)

, , , , .

0

All Articles