hello,
I have two basic questions regarding persistent objects:
Question 1:
I create a POJO of type A. After that, I create an arbitrary query.
Code:
A myPojo = new A();
Query q = session.createQuery("from ....");
The createQuery() causes Hibernate to flush. myPojo gets an ID assigned, but is apparently not persisted to the database. How come? Isn't flushing supposed to save the objects to the database?
Question 2:Later in my code, I try to do saveOrUpdate() on myPojo.
Code:
session.saveOrUpdate(myPojo);
transaction.commit();
Since myPojo already has an ID, I get an exception telling me I have a foreign-key problem (ORA-02291: integrity constraint xxx violated - parent key not found). myPojo contains collections of other POJOs, which Hibernate tries to save
before myPojo, which is not possible.
I understand that setting the flushMode to COMMIT would be a workaround, but that is probably a bad solution as I suspect there is some other basic design flaw here...
If myPojo.id is null, everything goes fine. Could someone explain this behaviour, I would be very grateful.
cheers,
pj