I am learning about the Spring framework by adopting a found example. The example instantiates an EntityManager
object as follows:
package com.example.demo;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import jpautils.JPAUtils;
import utils.StringUtil;
/**
* @author
*
*/
public class JPAUserDAO implements IDAO<User> {
@PersistenceContext
private final EntityManager entityManager;
/**
* Constructor
*/
public JPAUserDAO() {
entityManager = JPAUtils.getEntityManager();
}
@Override
public void delete(final User user) {
executeInsideTransaction(entityManager -> entityManager.remove(user));
}
private void executeInsideTransaction(final Consumer<EntityManager> action) {
final EntityTransaction tx = entityManager.getTransaction();
try {
tx.begin();
action.accept(entityManager);
tx.commit();
} catch (final RuntimeException e) {
tx.rollback();
throw e;
}
}
[…]
Unfortunately, some wrong setting results in a runtime error:
UserApplication.main([2])
17:28:29.500 [main] DEBUG org.jboss.logging - Logging Provider: org.jboss.logging.Log4j2LoggerProvider
17:28:29.526 [main] INFO org.hibernate.jpa.boot.internal.PersistenceXmlParser - HHH000318: Could not find any META-INF/persistence.xml file in the classpath
17:28:29.526 [main] DEBUG org.hibernate.jpa.HibernatePersistenceProvider - Located and parsed 0 persistence units; checking each
17:28:29.526 [main] DEBUG org.hibernate.jpa.HibernatePersistenceProvider - Found no matching persistence units
javax.persistence.PersistenceException: No Persistence provider for EntityManager named com.example.demo
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at jpautils.JPAUtils.<clinit>(JPAUtils.java:16)
at com.example.demo.JPAUserDAO.<init>(JPAUserDAO.java:35)
at com.example.demo.UserApplication.main(UserApplication.java:30)
Exception in thread "main" java.lang.NullPointerException
at jpautils.JPAUtils.getEntityManager(JPAUtils.java:23)
at com.example.demo.JPAUserDAO.<init>(JPAUserDAO.java:35)
at com.example.demo.UserApplication.main(UserApplication.java:30)
I read similar postings, like this and that, but still could not resolve my problem. Perhaps, one clue to solve this issue may be the errors I get when including the module-info.java
file:
/**
*
*/
/**
* The module for the Web site "Shop".
*
* @author
*
*/
module WSShop {
exports com.example.demo;
requires java.persistence;
requires java.sql;
requires spring.boot;
requires spring.boot.autoconfigure;
requires Utilities;
}
results in the following errors; commenting this file removes all compile-time errors:
Description Resource Path Location Type
Syntax error on token ".", , expected module-info.java /WSShop/src/main/java line 13 Java Problem
Syntax error on token ".", , expected module-info.java /WSShop/src/main/java line 14 Java Problem
Syntax error on token ".", , expected module-info.java /WSShop/src/main/java line 15 Java Problem
Syntax error on token ".", = expected module-info.java /WSShop/src/main/java line 11 Java Problem
Syntax error on token ".", = expected module-info.java /WSShop/src/main/java line 16 Java Problem
Syntax error on token "module", interface expected module-info.java /WSShop/src/main/java line 10 Java Problem
What is the problem with the module-info.java
? What is missing or wrong in my persistence.xml
?
Here is also the pom.xml
file:
<?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>
<!-- parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath /> <!- lookup parent from repository ->
</parent -->
<groupId>com.example</groupId>
<artifactId>WSShop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>WSShop</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>12</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.10.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.14</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.1.6.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>