users@glassfish.java.net

Re: Unable to update data using Hibernate + Glassfish

From: <glassfish_at_javadesktop.org>
Date: Thu, 21 Jun 2007 14:04:08 PDT

Sahoo,

Your question did the magic because it made me ask myself the right question. So here is the short version of the story - there are possible bugs in NB 5.5 and TopLink Essentials. Hibernate actually behaves as it should, IMHO.

[u][b]What I was trying to do[/b][/u]

I used NB 5.5 wizard to generate JSF classes from entities. NB 5.5 generates the JSF managed bean (controller class is what they call it) for each entity. So for my entity, NewsEntity, there is a NewsEntityController. Now NewsEntityController injects a UserTransaction. And, in the create(), edit(), and destroy() handlers NB wizard generates code with the begin() and commit() demarcations to take care of changes. For instance, below is the code that gets generated for create() in the NewsEntityController-

public string create() {

  // Gets application-managed extended scoped EM
  EntityManager em = getEntityManager();

  try {
    utx.begin();
    em.persist(newsEntity);
    utx.commit();
  } catch (Exception ex) {
    ...
  }
  return "newsEntity_list";
}

[u][b]What is wrong with the above?[/b][/u]

The problem in the code above is that someone forgot to put em.joinTransaction() call in it. EM is created outside of an active transaction, so it needs to be told about the active transaction. NB team needs to fix the "JSF from entity classes" wizard so that it generates the correct code.

[u][b]TopLink's weird behavior[/b][/u]

I think one of the reasons why I was led astray in troubleshooting is because NB generated code works just fine for TopLink Essentials. I have no idea why TLE works. It should not, don't you think? TLE is flushing the state of entities in an EM that has not been associated with the active transaction. Why should it do so? I would really like to hear your thoughts on this.

[u][b]Conclusion[/b][/u]

I modified the NB 5.5 generated code to add em.joinTransaction() right after utx.begin(). And, that makes the inserts/updates work on Hibernate as expected. I will go ahead and file a bug with NB 5.5 in the meantime.

Regards,
Rima.
[Message sent by forum member 'rpatel' (rpatel)]

http://forums.java.net/jive/thread.jspa?messageID=223421