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

Discussion in 'Java' started by Giovanni Azua, Feb 19, 2009.

  1. 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
     
    Giovanni Azua, Feb 19, 2009
    #1
    1. Advertising

  2. 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
     
    Giovanni Azua, Feb 19, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. redbull
    Replies:
    2
    Views:
    739
    Kenneth P. Turvey
    Oct 31, 2007
  2. Lord0
    Replies:
    4
    Views:
    450
    Lord0
    Sep 8, 2008
  3. markla
    Replies:
    1
    Views:
    552
    Steven Cheng
    Oct 6, 2008
  4. Amit Jain
    Replies:
    6
    Views:
    535
    Amit Jain
    Nov 10, 2008
  5. Amit Jain
    Replies:
    3
    Views:
    1,757
Loading...

Share This Page