我通过采用一个发现的例子来学习Spring框架。该示例如下实例化一个EntityManager
对象:
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;
}
}
[…]
不幸的是,某些错误的设置会导致运行时错误:
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)
我阅读了类似的帖子,例如this和that,但仍然无法解决我的问题。也许,解决此问题的一个线索可能是在包含module-info.java
文件时出现的错误:
/**
*
*/
/**
* 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;
}
导致以下错误;对此文件添加注释会删除所有编译时错误:
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
module-info.java
有什么问题?我的persistence.xml
中缺少或存在什么问题?
这也是pom.xml
文件:
<?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>