users@glassfish.java.net

Re: How to use JPA's EntityManager.persist() in a servlet - mine does not work

From: Sahoo <Sahoo_at_Sun.COM>
Date: Wed, 20 Feb 2008 09:10:29 +0530

Refer to http://weblogs.java.net/blog/ss141213/archive/2005/12/index.html.

Sahoo
glassfish_at_javadesktop.org wrote:
> I am utterly baffled by what is happening. No doubt I am making a stupid mistake, but I cannot figure out what it is, even though what I am trying to do is about as simple as possible.
>
> I have been writing some servlets that access a database stored using the Derby server included with Glassfish. By looking at the examples in the Java EE 5 tutorial and on the web, I was able to write servlets to find and to update entries in the database. However, when I tried to create a new object to insert into the database, I ran into a wall.
>
> When the servlet tries to persist the new object (em.persist(newObj)), it always crashes with a 'Transaction marked for rollback' exception. This happens no matter what method of obtaining an EntityManager I use. All of the servlets inherit from a superclass that is annotated
>
> @PersistenceContext
> EntityManager em;
>
> The insertion servlet fails if it uses this inherited EntityManager, it fails if I annotate the insertion servlet itself to let it have its own EntityManager, it fails if I configure the persistence unit in web.xml and the servlet looks it up in JNDI, it fails if the servlet is annotated with
>
> @PersistenceContext
> EntityManagerFactory emf;
>
> and uses the factory to create an EntityManager inside the transaction, it fails if the servlet is annotated with
>
> @PersitenceContext(unitName="myPU")
>
> it fails if the transaction's begin() and commit() are commented out. In all cases, the servlet works OK if the persist() call is omitted, although of course in that case the new object is not saved.
>
> The persistent entity class used by these servlets was generated by NetBeans from the existing database. The entity has no links to other objects. The database is perfectly accessible through JDBC in other servlets in the same Glassfish container, including inserts and deletes.
>
> Does anyone have any idea why I cannot get this simple operation to work? If there is a problem with the database or the configuration, why do the other servlets work?
> [Message sent by forum member 'bclyon' (bclyon)]
>
> http://forums.java.net/jive/thread.jspa?messageID=259781
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>