Thursday March 30, 2006 Using TopLink persistence RI in Tomcat New EJB 3.0 persistence can be used in web module as well but all samples show this new feature in JavaEE5 projects. I assume that many users want to use this cool feature in web projects that have J2EE 1.4 version. I would like to show in this post how to use persistence in web project that is deployed in Tomcat. Of course, on the Tomcat we can't use container managed entity manager. Therefore, the application managed EM is used in this sample. You can use JTA entity managers or resource-local entity manager. Since the Tomcat doesn't support JTA we can use only resource-local entity manager. Let's create new web project with J2EE 1.4 specification and target server Tomcat in NetBeans 5.5. Don't forget that project should have defined 1.5 jdk as platform and we should change source level to 1.5. Of course, the Tomcat server must run on jdk 1.5 as well. Add JavaEE5 library and JDBC driver on project's classpath.Then, add persistence unit and choose application managed entity manager and generate entity classes from database or create them from scratch. In this post is presented using entity manager in servlet but similar approch might be used in JSF managed bean or in class too. The Entity manager might be used as follows:
EntityManagerFactory emF = Persistence.createEntityManagerFactory("Advertisement");
EntityManager em = emF.createEntityManager();
try{
em.getTransaction().begin();
List advertisements =
em.createQuery("SELECT a FROM Advertisement a").getResultList();
for (Advertisement elem : advertisements) {
out.println(elem.getDescription() + ", " + elem.getId() + "
");
}
em.getTransaction().commit();
}catch(Exception ex) {
em.getTransaction().rollback();
}finally {
em.close();
}
Build and run project. This works on Tomcat but I encountered security issues on Sun Application server 8.2. The workaround is that you should grant permissions for web module in server.policy. Add following lines in this file:
permission java.lang.RuntimePermission "createClassLoader";
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
Now, you can use Toplink persistence in existing web projects instead of Hibernate :-).
Posted by pblaha
( Mar 30 2006, 03:28:46 PM CEST )
Permalink
Comments [6]
Posted by D. J. on April 06, 2006 at 05:32 AM CEST #
In Tomcat, the web app can use Java Persistence API as if it were a Java application using Java Persistence API in *Java SE* environment. That means, *no injection* of emf or em. It is already mentioned in the blog that container managed em can not be used and so is JTA entity manager. More over, all the persistence classes need to be enumerated in persistence.xml file. I hope NetBeans takes care of this last requirement.
Some questions:
1. Why do I need Java EE 5 libraries when javax.persistence classes are part of toplink-essentials.jar?
2. It is not clear where are the toplink classes? Is toplink-essentials.jar kept in war/WEB-INF/lib? Or is Tomcat started with toplink-essentaisl-agent.jar as javaagent? If Tomcat is not started with javaagent, then lazy loading of 1:1 relationships may not be possible using TopLink.
Thanks, SahooPosted by Sahoo on April 06, 2006 at 08:37 AM CEST #
Posted by pblaha on April 13, 2006 at 10:21 PM CEST #
EJB3 and TopLink on Tomcat 5.5
Where should I put the persistence.xml file? I have tried WEB-INF, and other locations, but all I get is:060424 141148 10 SEVERE StandardWrapper.Throwable javax.persistence.PersistenceException: No Persistence provider for EntityManager named persistence
I have added the ‘javaagent’ to the JVM:
-javaagent:C:/Sun/AppServer/lib/toplink-essentials-agent.jar
Regards, nps
Posted by Nps on April 24, 2006 at 02:20 PM CEST #
EJB3 and TopLink on Tomcat 5.5
Where should I put the persistence.xml file? I have tried WEB-INF, and other locations, but all I get is:060424 141148 10 SEVERE StandardWrapper.Throwable javax.persistence.PersistenceException: No Persistence provider for EntityManager named persistence
I have added the ‘javaagent’ to the JVM:
-javaagent:C:/Sun/AppServer/lib/toplink-essentials-agent.jar
Regards, nps
Posted by Nps on April 24, 2006 at 02:21 PM CEST #
Posted by PetrB on April 26, 2006 at 03:25 PM CEST #