我没有使用 EJB/Spring 等。
只有 Jpa + Servlet。
我正在尝试初始化
public abstract class AbstractEntityManagerFactory {
private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("HibernateTask");
public static EntityManager getEntityManager(){
return emf.createEntityManager();
}
当我运行它时public static void main(String[] args)
- 一切都很好。
但是如果我使用 Tom Cat 9.0.58 - 我会收到一个错误:
05-Apr-2022 22:58:58.985 INFO [http-nio-8080-exec-4] org.hibernate.service.internal.AbstractServiceRegistryImpl.stopService HHH000369: Error stopping service [class org.hibernate.hikaricp.internal.HikariCPConnectionProvider]
java.lang.NullPointerException: Cannot invoke "com.zaxxer.hikari.HikariDataSource.close()" because "this.hds" is null
at org.hibernate.hikaricp.internal.HikariCPConnectionProvider.stop(HikariCPConnectionProvider.java:122)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.stopService(AbstractServiceRegistryImpl.java:390)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.cleanup(EntityManagerFactoryBuilderImpl.java:1449)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1508)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.aimprosoft.hopak.factory.AbstractEntityManagerFactory.<clinit>(AbstractEntityManagerFactory.java:9)
持久性.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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">
<persistence-unit name="HibernateTask">
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.hikari.jdbcUrl" value="jdbc:mysql://localhost:3306/Departments"/>
<property name="hibernate.hikari.username" value="root"/>
<property name="hibernate.hikari.password" value="ISBN97859910"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.connection.provider_class" value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider"/>
<property name="hibernate.hikari.connectionTimeout" value="20000"/>
<property name="hibernate.hikari.idleTimeout" value="150000"/>
<property name="hibernate.hikari.maximumPoolSize" value="10"/>
<property name="hibernate.hikari.poolName" value="HikariPool"/>
<property name="hibernate.hikari.autoCommit" value="true"/>
<property name="hibernate.hikari.maxLifetime" value="90000"/>
</properties>
</persistence-unit>
</persistence>
我认为这与一些特殊的 EntityManagerFactory 声明有关。
我有一个纯 jdbc 实现。如果我先对其进行查询。然后在魔术的帮助下,一切都会初始化。
目前我在页面上有 2 个按钮。
部门 - (JDBC) 员工 - (JPA)
如果我立即点击 Employee(JPA) - 我得到NullPointerException
,但如果首先点击 Department(JDBC),然后点击 Employee(JPA) - 一切都像时钟一样工作。
也许静态初始化有问题?
称呼
public class JpaTransactionManager {
public static <T> T doInTransaction(JpaTransactionCallback<T> callback) {
EntityManager em = AbstractEntityManagerFactory.getEntityManager(); // Попытка инициализации
T result = null;
em.getTransaction().begin();
result = callback.execute(em);
em.getTransaction().commit();
Log.logger.info("Transaction is done");
em.close();
return result;
}
}
pom.xml
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.6.7.Final</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-hikaricp</artifactId>
<version>5.6.7.Final</version>
</dependency>
</dependencies>
问题是 Hibernate 无法提取文件。尽管一切都在它的位置,他根本做不到。
解决方案:我只是通过 SessionFactory 做所有事情,而不是 emf