Configuring JPA/Eclipselink project with Payara Server and Postgresql

Viewed 24

I´m trying to set up a project using Payara Server Community Edition v5.2022.2, Postgresql 14 and Eclipselink.

When I test a simple rest endpoint returning a String, everything works fine, so I know that the rest configuration is working.

The problem is getting persistence to work. Every time I deploy the project to Payara and try the endpoint which will call the appropriate method to persist an entity I get an error saying no object was available for injection. I believe the problem is with the EntityManager, but I didn´t find the server.log very helpful.

These are my files.

persistence.xml

<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0"
    xsi:schemalocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd">
    <persistence-unit name="selfbookingPU"
        transaction-type="JTA">
        <jta-data-source>jdbc/postgresqlpool</jta-data-source>
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.ddl-generation"
                value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode"
                value="database" />
            <property name="eclipselink.logging.level" value="FINEST" />
        </properties>
    </persistence-unit>
</persistence>

web.xml


<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
    version="5.0">
    <data-source>
        <name>jdbc/postgresqlpool</name>
        <class-name>org.postgresql.ds.PGSimpleDataSource</class-name>
        <server-name>localhost</server-name>
        <port-number>5432</port-number>
        <database-name>selfbooking_dsv</database-name>
        <user>postgres</user>
        <password>${ALIAS=postgresql_dsv_pwd}</password>
    </data-source>
</web-app>

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>
    <groupId>com.rjso.selfbooking</groupId>
    <artifactId>selfbooking</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>Selfbooking App</name>
    <url>http://www.rjso.com/selfbooking</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.platform</groupId>
            <artifactId>jakarta.jakartaee-api</artifactId>
            <version>10.0.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink -->
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>4.0.0-RC2</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>selfbooking</finalName>
        <pluginManagement>
            <!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

APIConfig.java


import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

@ApplicationPath("/rest")
public class APIConfig extends Application {

}

ViajanteAPI.java


import entidades.Viajante;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import servicos.ViajanteServico;

@Path("/viajantes")
public class ViajanteAPI {

    @Inject
    ViajanteServico viajanteServico;

    @GET
    public void gravaViajante() {

        Viajante v = new Viajante("Rodrigo", "Oliveira", "Rodrigo J S Oliveira", "mylogin", "myemail@gmail.com");

        viajanteServico.gravaViajante(v);

    }
}

Usuario.java


import java.time.LocalDate;

import enums.SimNaoEnum;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.MappedSuperclass;

@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Usuario {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "primeiro_nome")
    private String primeiroNome;
    
    @Column(name = "ultimo_nome")
    private String ultimoNome;
    
    @Column(name = "nome_completo")
    private String nomeCompleto;
    
    @Column(name = "login")
    private String login;
    
    @Column(name = "email")
    private String email;
    
    @Column(name = "dt_nascimento")
    private LocalDate dtNascimento;
    
    @Column(name = "ativo")
    private SimNaoEnum isAtivo;
    
    @Column(name = "bloqueado")
    private SimNaoEnum isBloqueado;

    public Usuario() {
    };

    public Usuario(String primeiroNome, String ultimoNome, String nomeCompleto, String login, String email) {
        this.primeiroNome = primeiroNome;
        this.ultimoNome = ultimoNome;
        this.nomeCompleto = nomeCompleto;
        this.login = login;
        this.email = email;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getPrimeiroNome() {
        return primeiroNome;
    }

    public void setPrimeiroNome(String primeiroNome) {
        this.primeiroNome = primeiroNome;
    }

    public String getUltimoNome() {
        return ultimoNome;
    }

    public void setUltimoNome(String ultimoNome) {
        this.ultimoNome = ultimoNome;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public LocalDate getDtNascimento() {
        return dtNascimento;
    }

    public void setDtNascimento(LocalDate dtNascimento) {
        this.dtNascimento = dtNascimento;
    }

    public SimNaoEnum getIsAtivo() {
        return isAtivo;
    }

    public void setIsAtivo(SimNaoEnum isAtivo) {
        this.isAtivo = isAtivo;
    }

    public SimNaoEnum getIsBloqueado() {
        return isBloqueado;
    }

    public void setIsBloqueado(SimNaoEnum isBloqueado) {
        this.isBloqueado = isBloqueado;
    }

    public String getNomeCompleto() {
        return nomeCompleto;
    }

    public void setNomeCompleto(String nomeCompleto) {
        this.nomeCompleto = nomeCompleto;
    }

}

Viajante.java


import enums.SimNaoEnum;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;

@Entity
@Table(name = "viajante")
public class Viajante extends Usuario {

    @Column(name = "viajante_vip")
    private SimNaoEnum isVIP;

    public Viajante() {
        super();
    };

    public Viajante(String primeiroNome, String ultimoNome, String nomeCompleto, String login, String email) {
        super(primeiroNome, ultimoNome, nomeCompleto, login, email);
    }

    public SimNaoEnum getIsVIP() {
        return isVIP;
    }

    public void setIsVIP(SimNaoEnum isVIP) {
        this.isVIP = isVIP;
    }

}

RepositorioBase.java


import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;

public abstract class RepositorioBase {

    @PersistenceContext
    protected EntityManager entityManager;
}

ViajanteRepositorio.java


import entidades.Viajante;

public class ViajanteRepositorio extends RepositorioBase {

    public void gravaViajante(Viajante viajante) {
        entityManager.persist(viajante);
    }
}

ViajanteServico.java


import entidades.Viajante;
import jakarta.inject.Inject;
import repositorios.ViajanteRepositorio;

public class ViajanteServico {
    
    @Inject
    ViajanteRepositorio viajanteRepositorio;
    
    public void gravaViajante(Viajante viajante) {
        viajanteRepositorio.gravaViajante(viajante);
    }

}

Server.log (excerpt)

MultiException stack 1 of 3
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=ViajanteServico,parent=ViajanteAPI,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1081564559)
    at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:51)
    at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:188)

MultiException stack 2 of 3
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of api.ViajanteAPI errors were found
    at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:224)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:334)

MultiException stack 3 of 3
java.lang.IllegalStateException: Unable to perform operation: resolve on api.ViajanteAPI
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:363)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:463)

Any ideas on what am I doing wrong?

Thanks!

0 Answers
Related