This reports on a possible solution to a common problem:
I was trialling ObjectDB on an existing, functioning, large JavaEE/JPA web application, which until now had used EclipseLink+MySQL and then Oracle+MySQL.
On trying it out on ObjectDB I hit a problem with my very extensive entity wiring code, which relies on an "external" entity object having a non-null id after it has been passed to EntityManager.persist, thus:
Element e = new Element(); // .. configure element .. em.persist(e); // then do something that relies on Java variable e having e.getId() defined (like wiring up to another element)
I am using the following for the ID:
@Id //@GeneratedValue(strategy = GenerationType.AUTO)// Explicit for EclipseLink + Oracle or MySQL @GeneratedValue//ObjectDB takes as AUTO public Long getId() { return id; }
The javadocs for EntityManager.persist() merely state that it should "Make an instance managed and persistent.", and unlike merge() there is no managed entity returned. My copious entity wiring code assumed that the Java variable passed to persist becomes endowed with an id (the same as the "internal" managed entity), which under EclipseLink it did.
Performing em.flush() immediately after em.persist(e) seems to work (although I'm not sure about the performance hit of flushing every time). This can be encapsulated as a wrapping persist(Object) method that also always flushes after persist(), to afford central experimentation.
I also found this in the documentation at https://www.objectdb.com/java/jpa/entity/generated:
Unlike AUTO and IDENTITY, the SEQUENCE strategy generates an automatic value as soon as a new entity object is persisted (i.e. before commit). This may be useful when the primary key value is needed earlier.
Hoping this is of help,
Webel
PS: The following external links discuss this common problem with JPA handlers other than ObjectDB:
forums.netbeans.org/topic13007.html
Spring forum: Get id of JPA persisted object
(forum.springsource.org/showthread.php?64943-Get-id-of-JPA-persisted-object)