it simply says that your test method is not transactional which is true.
here a code which works:
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class Movies {
@PersistenceContext(unitName = "movie-unit", type =
PersistenceContextType.TRANSACTION)
private EntityManager entityManager;
public void addMovie(Movie movie) throws Exception {
entityManager.persist(movie);
}
public void deleteMovie(Movie movie) throws Exception {
entityManager.remove(entityManager.find(Movie.class,
movie.getId()));
}
public List<Movie> getMovies() throws Exception {
Query query = entityManager.createQuery("SELECT m from Movie as m");
return query.getResultList();
}
}
public class MoviesTest extends TestCase {
@EJB
private Movies movies;
@PersistenceContext
private EntityManager entityManager;
public void setUp() throws Exception {
Properties p = new Properties();
p.put("movieDatabase", "new://Resource?type=DataSource");
p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
EJBContainer.createEJBContainer(p).getContext().bind("inject",
this);
}
public void test() throws Exception {
movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs",
1992));
movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));
List<Movie> list = movies.getMovies();
assertEquals("List.size()", 3, list.size());
for (Movie movie : list) {
movies.deleteMovie(movie);
}
assertEquals("Movies.getMovies()", 0, movies.getMovies().size());
}
}
Note: i added to movie entity:
@Id
@GeneratedValue
private long id;
+ the getId method.
- Romain
2012/2/22 amber <[email protected]>
> I've tried to modify the example "alternate-descriptors" from
> openejb-examples-4.0.0-beta-2 to have a bean statless with
> TransactionAttributeType.REQUIRED
>
> To me REQUIRED start a transaction if no one is active.
>
> But i got too a javax.persistence.TransactionRequiredException
>
> Here the code :
>
> movies.java :
>
> import javax.ejb.Stateful;
> import javax.ejb.TransactionAttribute;
> import javax.ejb.TransactionAttributeType;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import javax.persistence.PersistenceContextType;
> import javax.persistence.Query;
> import java.util.List;
> import javax.ejb.Stateless;
>
> import static javax.ejb.TransactionAttributeType.MANDATORY;
>
> //START SNIPPET: code
> @Stateless
> @TransactionAttribute(TransactionAttributeType.REQUIRED)
> public class Movies {
>
> @PersistenceContext(unitName = "movie-unit", type =
> PersistenceContextType.TRANSACTION)
> private EntityManager entityManager;
>
> public void addMovie(Movie movie) throws Exception {
> entityManager.persist(movie);
> }
>
> public void deleteMovie(Movie movie) throws Exception {
> entityManager.remove(movie);
> }
>
> public List<Movie> getMovies() throws Exception {
> Query query = entityManager.createQuery("SELECT m from Movie as m");
> return query.getResultList();
> }
> }
>
> MoviesTest.java :
>
> //START SNIPPET: code
> public class MoviesTest extends TestCase {
>
> @EJB
> private Movies movies;
>
> @PersistenceContext
> private EntityManager entityManager;
>
> public void setUp() throws Exception {
> Properties p = new Properties();
> p.put("movieDatabase", "new://Resource?type=DataSource");
> p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
> p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
>
> p.put("openejb.altdd.prefix", "test");
>
> EJBContainer.createEJBContainer(p).getContext().bind("inject",
> this);
> }
>
> public void test() throws Exception {
> entityManager.persist(new Movie("Quentin Tarantino", "Reservoir
> Dogs", 1992));
> entityManager.persist(new Movie("Joel Coen", "Fargo", 1996));
> entityManager.persist(new Movie("Joel Coen", "The Big Lebowski",
> 1998));
>
> List<Movie> list = movies.getMovies();
> assertEquals("List.size()", 3, list.size());
>
> for (Movie movie : list) {
> movies.deleteMovie(movie);
> }
> assertEquals("Movies.getMovies()", 0,
> movies.getMovies().size());
> }
> }
>
> result :
>
> javax.persistence.TransactionRequiredException
> at
>
> org.apache.openejb.persistence.JtaEntityManager.assertTransactionActive(JtaEntityManager.java:91)
> at
>
> org.apache.openejb.persistence.JtaEntityManager.persist(JtaEntityManager.java:120)
> at org.superbiz.altdd.MoviesTest.test(MoviesTest.java:55)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
>
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at
>
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at junit.framework.TestCase.runTest(TestCase.java:168)
> at junit.framework.TestCase.runBare(TestCase.java:134)
> at junit.framework.TestResult$1.protect(TestResult.java:110)
> at junit.framework.TestResult.runProtected(TestResult.java:128)
> at junit.framework.TestResult.run(TestResult.java:113)
> at junit.framework.TestCase.run(TestCase.java:124)
> at junit.framework.TestSuite.runTest(TestSuite.java:232)
> at junit.framework.TestSuite.run(TestSuite.java:227)
> at
>
> org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
> at
>
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
> at
>
> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
> at
>
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
> at
>
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
> at
>
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
> at
>
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
>
>
> note that I removed the userTransaction part (because the contener has to
> start one)
>
> did i miss something ?
>
> I am a bit disappointed, my app was working fine on openEJB 3.1.4 and I
> can't do it working on 4 (moving EJB 3 to 3.1)
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/openEJB-fail-on-second-test-tp4401889p4410822.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>