Updating existing persistence entries


I have stored some instances of MyObject via em.persist(myObjectInstance);

I can fetch them and bind them to a kind of property editor in my app (it simply calls the setters to reflect gui editing actions).

When I try to persist an object later on, I get an error like

com.objectdb.o.UserException: Attempt to reuse an existing primary key value

which makes sense to me but I'm missing a em.update() method.

I found examples for updating objects in the knowledge base, but they make use of a pe.currentTransaction() which I don't have (deprecated?) and an em.getObjectById() which I don't have either (I'm using ObjectDB 2.4.4_15 atm).

Please can someone point me into the right direction?

Thank you!



The documentation that you mentioned (from the knowledge base) describes the JDO API. It is still supported but only when using JDO rather than JPA.

You should use the new manual that also includes instructions on how to update and object using JPA.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Dear support,

thank you for your reply and the documentation link. But even with this I don't get it to work.

The transparent update needs a transaction to be started and then committed after the setter of an persisted object is called, right? My problem is that I have bound my persisted bean to a property editor which calls my setters automagically when changing associated values in my GUI component. There's no way (at least I know of) to do something before and after a value change.

I'm checking out ObjectDB as a thin replacement to hibernate, because I just want 2 simple beans to be persisted with CRUD operations. When I use hibernate I can initiate a transaction, feed my persisted object which may have changed for one or more fields to a call like "session.update(objectInstance)". I can commit the session afterwards and that's it. I mean I don't need to modify the object *within* the session, I can do it wherever and whenever I want and hibernate identifies the object by runtime polymorphism and finds the correspondent row in the DB by the matching and unique ID field.

Is there a similar option für ObjectDB? If I were unable to make myself clear, here's what I want in short:

  1. Load persisted instance from a DB (works).
  2. Assign the loaded object to my bean-editor which calls none, one or many updates via setters (works).
  3. When a save button is pushed, get the assigned object from the bean editor and save its current state to the db table/row it was initially loaded/created from.

Please tell me if this is possible and how I can achieve this.

Thank you!



In this aspect it seems that there is no difference between Hibernate and ObjectDB. Both Hibernate and ObjectDB have an old API (Session in Hibernate, JDO PersistenceManager in ObjectDB) and a new API, JPA, which is common to Hibernate and ObjectDB.

Transparent update works for managed objects. Therefore, if you modify a managed object within an active transaction the changes are identified automatically. Of course, you have to begin and commit a transaction (when SAVE is selected).

If a detached object is modified, it has to be merged (maybe merge is your missing method?) in order for the update to take effect. It would be simpler to use the managed entity objects as the beans, and then you do not have detached objects and merge is not required. This is how ObjectDB Explorer (which is also a GUI object editor) works.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Dear support,

thank you very much, for the explanation. I managed to get it working with beginning a transaction and committing it on save. I never close the session manually, is that ok?

Thank you very much!



You should close the EntityManager and EntityManagerFactory when the application is closed, in order to close the database file properly.

In an ordinary application you may consider setting a shutdown hook for this purpose.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login