OpenJPA persist(...) doesn't assign the Id to the Entity instance

G

Giovanni Azua

hi,

I have a use-case that follows a similar pattern to what's written in the
snippet below. My problem is that the assertion fails meaning once the
Entity instance is persisted in the database by calling persist, that
instance does not get the Id assigned (I also tried calling refresh on that
instance). I have this situation only with OpenJPA though, using Hibernate
as JPA provider works fine.

Maybe is a missing OpenJPA property somewhere in the persistence.xml?

I will be trying meantime to switch to some other provider to test.

TIA,
regards,
Giovanni

EntityManager entityManager = ...
entityManager.getTransaction().begin();
Person myPerson = new Person("Pepito", 19);
entityManager.persist(myPerson);

assert myPerson.getId() != null : "Person Id must not be null";

entityManager.getTransaction().commit();

persistence.xml:
http://perfectjpattern.svn.sourcefo...esources/META-INF/persistence.xml?view=markup
orm.xml:
http://perfectjpattern.svn.sourcefo...c/test/resources/META-INF/orm.xml?view=markup
Person.java:
http://perfectjpattern.svn.sourcefo...ern/example/datamodel/Person.java?view=markup
 
G

Giovanni Azua

I already found what the problem was.

I was using IDENTITY Id generation strategy and until the insert is not
actually commited, the Id generation and assignment to the Entity in memory
will not occur (there are no records commited yet, maybe not even flushed),
therefore doing a simple commit resolved the problem.

regards,
Giovanni
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top