You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Furmaniak Christophe <Ch...@atosorigin.com> on 2010/03/06 16:36:07 UTC

Problem with @LocalClient and JPA Entity unit testing

Hi,

I'd like to unit test directly my JPA entities, and Local Client injection (http://openejb.apache.org/3.0/local-client-injection.html) should be the solution.

It works for creation or find actions but I cannot make it work for deletion, I always get a IllegalArgumentException "Removing a detached instance".

I just took the sample from the jpa-hibertate, here is my Test:

==================================================================
package org.superbiz.injection.h3jpa;
import java.util.Properties;
import javax.annotation.Resource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import org.apache.openejb.api.LocalClient;
import org.junit.Assert;
import org.junit.Test;
@LocalClient
public class LocalClientTest {
 @PersistenceContext(unitName = "movie-unit")
 private EntityManager em;
 @Resource
 private UserTransaction tx;
 @Test
 public void testCreateDelete() throws NamingException,
   NotSupportedException, SystemException, IllegalStateException,
   SecurityException, HeuristicMixedException,
   HeuristicRollbackException, RollbackException {
  Properties p = new Properties();
  p.put(Context.INITIAL_CONTEXT_FACTORY,
    "org.apache.openejb.client.LocalInitialContextFactory");
  p.put("movieDatabase", "new://Resource?type=DataSource");
  p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
  p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
  p.put("movieDatabaseUnmanaged", "new://Resource?type=DataSource");
  p.put("movieDatabaseUnmanaged.JdbcDriver", "org.hsqldb.jdbcDriver");
  p.put("movieDatabaseUnmanaged.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
  p.put("movieDatabaseUnmanaged.JtaManaged", "false");
  Context context = new InitialContext(p);
  context.bind("inject", this);
  Assert.assertNotNull("EntityManager should not be null", em);
  Movie movie1 = new Movie("Quentin Tarantino", "Reservoir Dogs", 1992);
  tx.begin();
  em.persist(movie1);
  tx.commit();
  Assert.assertNotSame("id should be valued", 0, movie1.getId());
  tx.begin();
  em.remove(movie1);
  tx.commit();
 }
==================================================================

[I just added a getId() method the provided  Movie class + added a -Dopenejb.tempclassloader.skip=annotations to my Eclipse Junit launcher]

Everything is OK, until the em.remove(movie1) part.

Am I doing something wrong?

Regards

Christophe

________________________________

Ce message et les pièces jointes sont confidentiels et réservés à l'usage exclusif de ses destinataires. Il peut également être protégé par le secret professionnel. Si vous recevez ce message par erreur, merci d'en avertir immédiatement l'expéditeur et de le détruire. L'intégrité du message ne pouvant être assurée sur Internet, la responsabilité du groupe Atos Origin ne pourra être recherchée quant au contenu de ce message. Bien que les meilleurs efforts soient faits pour maintenir cette transmission exempte de tout virus, l'expéditeur ne donne aucune garantie à cet égard et sa responsabilité ne saurait être recherchée pour tout dommage résultant d'un virus transmis.

This e-mail and the documents attached are confidential and intended solely for the addressee; it may also be privileged. If you receive this e-mail in error, please notify the sender immediately and destroy it. As its integrity cannot be secured on the Internet, the Atos Origin group liability cannot be triggered for the message content. Although the sender endeavours to maintain a computer virus-free network, the sender does not warrant that this transmission is virus-free and will not be liable for any damages resulting from any virus transmitted.

Re: Problem with @LocalClient and JPA Entity unit testing

Posted by cfurmaniak <ch...@atosorigin.com>.
Thanks Jean-Louis, it works.

I thought I tried this one, but maybe I've tried so many variants that my
mind got befuddled :).
-- 
View this message in context: http://n4.nabble.com/Problem-with-LocalClient-and-JPA-Entity-unit-testing-tp1582883p1584510.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Problem with @LocalClient and JPA Entity unit testing

Posted by Jean-Louis MONTEIRO <je...@atosorigin.com>.
Hi Christophe,

Excerpt from the specification:
---
void remove(Object entity)

    Remove the entity instance.

    Parameters:
        entity - 
    Throws:
        IllegalStateException - if this EntityManager has been closed. 
        IllegalArgumentException - if not an entity or if a detached entity 
        TransactionRequiredException - if invoked on a container-managed
entity manager of type PersistenceContextType.TRANSACTION and there is no
transaction.
---

The specification requires a managed entity as a parameter.
So basically, you can fix your issue by adding a line


cfurmaniak wrote:
> 
>   Movie movie1 = new Movie("Quentin Tarantino", "Reservoir Dogs", 1992);
>   tx.begin();
>   em.persist(movie1);
>   tx.commit();
>   Assert.assertNotSame("id should be valued", 0, movie1.getId());
>   tx.begin();
>   Movie movie1Managed = em.find(Movie.class, movie1.getId());
>   em.remove(movie1Managed );
>   tx.commit();
>  }
> 

Hope it helps.
JLouis

-- 
View this message in context: http://n4.nabble.com/Problem-with-LocalClient-and-JPA-Entity-unit-testing-tp1582883p1584193.html
Sent from the OpenEJB User mailing list archive at Nabble.com.