0

I faced a really big problem with creating database using persistent.xml . Ok, i start from the beginning. I created a maven (artifact ->webapp) project. I added a META-INF directory with file persistence.xml into my source directory like that:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
    <persistence-unit name="myDatabase" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/Lab6" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
        </properties>
    </persistence-unit>
</persistence>

Then i put some model into my java sources. I added apropiate annotations like that:

package model;

import javax.persistence.*;

@Entity
@Table(name="author")
public class Author
{
    private int Id;
    private String firstName;
    private String lastName;

    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator="seq")
    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    @Column(name = "first_name", nullable = false)
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "last_name", nullable = false)
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

After this I added a servlet class to connect to let the program make some operations using POST method. Nevermind :) After that I run the project and ... nothing hapened; program run successfully, I can get into servlet but database hadnt been created. Then I started to search the solution on a web and nothing helped my with my problem. Below is my pom.xml file:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Lab6</groupId>
  <artifactId>Lab6</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Lab6 Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <build>
    <finalName>Lab6</finalName>
    <testResources>
      <testResource>
        <directory>src/resources</directory>
        <filtering>true</filtering>
      </testResource>
    </testResources>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.persistence</groupId>
      <artifactId>persistence-api</artifactId>
      <version>1.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>6.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.1-api</artifactId>
      <version>1.0.0.Final</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>3.0-alpha-1</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>4.2.0.Final</version>
    </dependency>
    <dependency>
      <groupId>net.java.dev.glassfish</groupId>
      <artifactId>glassfish-persistence-api</artifactId>
      <version>b32g</version>
    </dependency>
  </dependencies>
</project>

I really stuck. Until now i checked other versions of providers (eclipse) in persistence.xml, and made some really weird changes inside that file but without success. I was checking also war file. Every needed file were inside (especcially persistence.xml). FI change my current glassfish version of server to different ones but it didnt help :((( Reeally guys please help me with that. My knowlage is ropably too low to have better ideas ghow to fix that

Thanks in advance

  • 2
    You've called it `perstistance.xml`, `persistent.xml`, and `persistence.xml`. You need to copy and paste the file name because any misspelling will ensure that it fails to work. What is the exact path of `persistence.xml` in the WAR file? – ngreen Jun 01 '17 at 05:55
  • sorry for mistake in that name but I was almost sleeping when i wrote this. i ensure you that i have proper name for persistence.xml – fOrest94 Jun 01 '17 at 11:02

1 Answers1

0

You are missing the part of persistence.xml that tells it how to find your classes. Here are a couple options:

<jar-file>MyLibrary.jar</jar-file>
<class>model.Author</class>

Which option you choose will depend on how your project is set up.

ngreen
  • 1,559
  • 13
  • 22