How to write web.xml and persistence.xml to connect Java EE to DB Server

I aim something very simple - create an empty maven, download in pom - javaee-api, and then to connect this simple maven EE project - to DataBase Server - currently my local machine's MySLQ Server. I also aim to keep it simple - only with web.xml in which I put my DataSource details, and a persistence.xml in which I mention my DataSource. I really want to keep it simple - therefore I do not put any jpa provider in persistence.xml since I want to use the provider in any of the application servers I deploy on. I do now want to use explicitly Hibernate whatever, I want an app that will use the default persistence provider on the application server. However I downloaded (also as maven dependency) the mysql-connector-java. I created a simple Entity and I run the project. I am using IntelliJ, I deploy on Tomcat 8 alles gut but.. no tables are created in my DB.

Sorry for the messy explanations I provided, but after 5 hours fight and a headache this is the best I can. I also provide you my web.xml, persistence.xml, pom.xml, even my Entity class...

Please nice dude help how to connect my so simple app to MySQL Server.

ALSO PLEASE PRIOR TRAVERSE ME TO OTHER THREAD - please read carefully what I need, I do not want to connect via method etc. I want to do it exactly as I am trying. I want - THIS WAY !

This is the pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>rado</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>
    <build>
           <plugins>
               <plugin>
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <version>3.7.0</version>

                   <configuration>
                       <source>8</source>
                       <target>8</target>
                   </configuration>
               </plugin>
           </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.14</version>
        </dependency>

    </dependencies>

</project>
enter code here

This is my web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <data-source>
        <name>java:app/rocky/myCon</name>
        <class-name>com.mysql.jdbc.Driver</class-name>
        <url>jdbc:mysql://localhost:3306/rocky</url>
        <user>root</user>
        <password>****</password>
    </data-source>

</web-app>

This is the persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">

    <persistence-unit name="pu" transaction-type="JTA">
        <jta-data-source>java:app/rocky/myCon</jta-data-source>
    </persistence-unit>

</persistence>

This is the only entity I have

package entities;

import javax.persistence.*;

@Entity
@Table(name =  "employees")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;

    public Employee() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Solution 1:

you need to deploy to a JavaEE or JakartaEE application server like TomEE, GlassFish, Payara, WildFly,... Tomcat isn't a full JavaEE server, it doesn't support JPA out of the box which is needed to use persistence.xml and JPA entities.

In theory, you could add hibernate to Tomcat or to your application, but I'm not sure it would just work. Instead, if you want to stay with Tomcat, use TomEE, which is based on Tomcat but adds all the missing JavaEE components and everything just works.

Otherwise your code looks good.