mercredi 20 mai 2015

postgres database with wildfly in eclipse

i’m having troubles to set up a database connection in my Dynamic Web Project, using Eclipse. I added a datasource to Wildfly, so the standalone.xml looks like this:

<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>

Then, in my Eclipse project, I added JPA to project facets, and modified the persistence.xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://ift.tt/1cKbVbQ" xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/1cKbVbQ http://ift.tt/1kMb4sd">

<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;

However, I end up getting a NullPointerException.

Can anyone tell me what I’m doing wrong?

Aucun commentaire:

Enregistrer un commentaire