у меня возникли проблемы с настройкой подключения к базе данных в моем динамическом веб-проекте с использованием Eclipse. Я добавил источник данных в Wildfly, поэтому файл standalone.xml выглядит так:
<datasource jta="true" jndi-name="java:jboss/datasources/weathermonitor" pool-name="weathermonitor" enabled="true" use-ccm="true">
<connection-url>jdbc:postgresql://localhost:5432/weathermonitor</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<driver>postgres</driver>
<security>
<user-name>postgres</user-name>
<password>student</password>
</security>
<validation>
<validate-on-match>false</validate-on-match>
<background-validation>false</background-validation>
</validation>
<timeout>
<set-tx-query-timeout>false</set-tx-query-timeout>
<blocking-timeout-millis>0</blocking-timeout-millis>
<idle-timeout-minutes>0</idle-timeout-minutes>
<query-timeout>0</query-timeout>
<use-try-lock>0</use-try-lock>
<allocation-retry>0</allocation-retry>
<allocation-retry-wait-millis>0</allocation-retry-wait-millis>
</timeout>
<statement>
<share-prepared-statements>false</share-prepared-statements>
</statement>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="postgres" module="org.postgres">
<driver-class>org.postgresql.Driver</driver-class>
</driver>
</drivers>
</datasources>
Затем в моем проекте Eclipse я добавил JPA к аспектам проекта и изменил файл persistence.xml следующим образом:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="weathermonitor"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/weathermonitor</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
</properties>
</persistence-unit>
In my Java DAO Class, I tried to inject the Entity Manager:
@PersistenceContext(unitName = "weathermonitor")
private EntityManager em;
Однако в итоге я получаю исключение NullPointerException.
Кто-нибудь может сказать мне, что я делаю неправильно?