What to put into jta-data-source of persistence.xml?

后端 未结 1 2006
名媛妹妹
名媛妹妹 2020-12-28 13:13

What value should I place into of my persistence.xml?

In glassfish admin panel I created a datasource name \"

相关标签:
1条回答
  • 2020-12-28 13:50

    The problem is that Persistence.createEntityManagerFactory("abc") is the "do it yourself" API and doesn't take advantage of the Embedded EJB Container. You can get a container managed EntityManager in your test case very easily.

    Just as with the related jndi/datasource question I recommend you check out the examples in the examples.zip. They're all designed to take the struggle out of getting started.

    Here's a snippet from the testcase-injection example which shows how you can get an EntityManager and other things from the container for use in a test.

    First, add an empty ejb-jar.xml or application-client.xml to your test to turn on scanning for your test code:

    • src/test/resources/META-INF/application-client.xml

    Then, annotate your test case with @org.apache.openejb.api.LocalClient and use the standard JavaEE annotations for the actual injection.

    @LocalClient
    public class MoviesTest extends TestCase {
    
        @EJB
        private Movies movies;
    
        @Resource
        private UserTransaction userTransaction;
    
        @PersistenceContext
        private EntityManager entityManager;
    
        public void setUp() throws Exception {
            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");
    
            InitialContext initialContext = new InitialContext(p);
    
            // Here's the fun part
            initialContext.bind("inject", this);
        }
    

    As movieDatabase is the only DataSource that we've setup, OpenEJB will automatically assign that DataSource to your persistence unit without the need to modify your persistence.xml. You can even leave the <jta-data-source> or <non-jta-data-source> empty and OpenEJB will still know what to do.

    But for the sake of completeness, here's how this particular application has defined the persistence.xml

    <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    
      <persistence-unit name="movie-unit">
        <jta-data-source>movieDatabase</jta-data-source>
        <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>
        <class>org.superbiz.testinjection.Movie</class>
    
        <properties>
          <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
        </properties>
      </persistence-unit>
    </persistence>
    

    Then the fun part, using it all together in tests

    public void test() throws Exception {
    
        userTransaction.begin();
    
        try {
            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());
    
        } finally {
            userTransaction.commit();
        }
    }
    
    0 讨论(0)
提交回复
热议问题