4

I am developing a Dynamic web project (Java EE) using JSF, PrimeFaces, JPA, and running on Tomcat 7. The project development is based on http://www.simtay.com/simple-crud-web-application-with-jsf-2-1-primefaces-3-5-maven-and-jpa/
Now I am developing the JPA part of the software. In past, when I developed some little things (as exercises) in Java SE, I used to use the following database properties:

jdbc.drivers=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=password

But now I am learning JPA on Java EE.

In the book "Pro JPA 2 Mastering the Java trade Persistance API", Chapter 3, paragraph "Packaging It Up" you can read:

In the Java EE environment, many properties required in the persistence.xml file for Java SE can be omitted. In Listing 3-32, you see the persistence.xml file from Listing 2-11 converted for deployment as part of a Java EE application. Instead of JDBC properties for creating a connection, we now declare that the entity manager should use the data source name “jdbc/EmployeeDS”. If the data source was defined to be available in the application namespace instead of the local component naming context then we might instead use the data source name of “java:app/jdbc/EmployeeDS”. The transaction- type attribute has also been removed to allow the persistence unit to default to JTA. The application server will automatically find entity classes, so even the list of classes has been removed. This example represents the ideal minimum Java EE configuration. Because the business logic that uses this persistence unit is implemented in a stateless session bean, the persistence.xml file would typically be located in the META-INF directory of the corresponding EJB JAR.

Listing 3-32. Defining a Persistence Unit in Java EE

<persistence>
    <persistence-unit name="EmployeeService">
        <jta-data-source>jdbc/EmployeeDS</jta-data-source>
    </persistence-unit>
</persistence>

Listing 2-11. Elements in the persistence.xml File

<persistence>
    <persistence-unit name="EmployeeService" transaction-type="RESOURCE_LOCAL">
        <class>examples.model.Employee</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527 EmpServDB;create=true"/>
            <property name="javax.persistence.jdbc.user" value="APP"/>
            <property name="javax.persistence.jdbc.password" value="APP"/>
        </properties>
    </persistence-unit>
</persistence>

My question is: how can I adapt a generic persistence.xml file in Java EE environment to connect to a MySQL/JDBC database using the properties I entered at the top of the post?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Germano Massullo
  • 2,572
  • 11
  • 40
  • 55
  • 1
    Tomcat as being a barebones JSP/Servlet container can hardly be considered a "Java EE environment". It doesn't support container managed transactions (JTA) at all, which is what you ultimately need. Real Java EE containers such as Glassfish, JBoss AS and TomEE support JTA (and JPA, EJB, JSF, etc, etc) out the box. On "plain" Tomcat, you'd need to install JTA separately (like as you installed JSF and JPA separately). Googling "install jta tomcat" should give sufficient hints. Or just migrate to a full fledged Java EE container so that you don't need to install loose Java EE artifacts everytime. – BalusC Jun 27 '13 at 16:46
  • I don't like TomEE, I had a lot of troubles with it. The moment I gave up using it it neither started. I like to more to import libraries in Tomcat. After seeing http://stackoverflow.com/questions/2552612/how-to-use-jta-support-in-tomcat-6-for-hibernate I started learning how to use jbossts-full to let my project use JTA correctly. If you put your previous comment as an answer, I will give you the green V mark – Germano Massullo Jun 28 '13 at 16:07

2 Answers2

5

The given example requires JTA, Java Transaction API. It will delegate the transaction management to the container. With JTA enabled, if you're using a @Stateless EJB, then a single method call counts by default as a single complete transaction. This allows you to write clean code without any tx.begin, tx.commit, tx.rollback, etc boilerplate.

Like JSF, EJB and JPA, JTA is by default not available on a barebones JSP/Servlet container as Tomcat and Jetty. Like JSF, EJB and JPA, you'd need to install JTA separately on Tomcat.

An alternative is to switch from a JSP/Servlet container to a real Java EE (web profile) container, such as Glassfish, JBoss AS and TomEE. It offers everything directly out the box with regard to Java EE. Note that JBoss AS and TomEE basically use Tomcat's JSP/Servlet engine under the covers.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
-1

Create persistence.xml in WEB-INF/classes/META-INF folder. WEB-INF will be already present. But we need to crate classes and META-INF folder.

enter image description here

Vikas s kumar
  • 287
  • 3
  • 3