Search code examples
javadatabasepostgresqleclipselinkjava-websocket

EclipseLink can't get the data from PostgreSQL


this is my first stackoverflow question.

I can't read out the data from my PostgreSQL table and I have tried different methods, but I couldn't get it to work. All my services are on the newest version available.

Maybe you can find my problem:)

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="3.0" 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="uebersetzungstool_PU" transaction-type="RESOURCE_LOCAL">
        
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.mycompany.uebersetzungstool.entity.Translation</class>
        <properties>
            <property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql://localhost/mydatabase"/>
            <property name="jakarta.persistence.jdbc.driver" value="org.postgresql.Driver"/>
            <property name="jakarta.persistence.jdbc.user" value="myuser"/>
            <property name="jakarta.persistence.jdbc.password" value="pwd"/>
            <property name="eclipselink.target-database" value="PostgreSQL"/>
        </properties>
    </persistence-unit>
</persistence>

Translation.java file:

package com.mycompany.uebersetzungstool.entity;

import java.io.Serializable;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name="Translation")
public class Translation implements Serializable {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String key;
    private String value;
    private String language;
    private String project;
    private String version;
    private String comment;
    private boolean translated;
    private String create_ts;
    private String create_user;
    private String update_ts;
    private String update_user;

    
    public Translation() {
    }

    public Translation(int id, String key, String value, String language, String project, String version, String comment, boolean translated, String create_ts, String create_user, String update_ts, String update_user) {
        this.id = id;
        this.key = key;
        this.value = value;
        this.language = language;
        this.project = project;
        this.version = version;
        this.comment = comment;
        this.translated = translated;
        this.create_ts = create_ts;
        this.create_user = create_user;
        this.update_ts = update_ts;
        this.update_user = update_user;
    }
    
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getProject() {
        return project;
    }

    public void setProject(String project) {
        this.project = project;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public boolean isTranslated() {
        return translated;
    }

    public void setTranslated(boolean translated) {
        this.translated = translated;
    }

    public String getCreate_ts() {
        return create_ts;
    }

    public void setCreate_ts(String create_ts) {
        this.create_ts = create_ts;
    }

    public String getCreate_user() {
        return create_user;
    }

    public void setCreate_user(String create_user) {
        this.create_user = create_user;
    }

    public String getUpdate_ts() {
        return update_ts;
    }

    public void setUpdate_ts(String update_ts) {
        this.update_ts = update_ts;
    }

    public String getUpdate_user() {
        return update_user;
    }

    public void setUpdate_user(String update_user) {
        this.update_user = update_user;
    }
            
    
}

"test" file:

import com.mycompany.uebersetzungstool.entity.Translation;
import org.junit.jupiter.api.Test;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import java.util.List;

public class DatabaseConnectionTest {

    @Test
    public void connectionTest() {
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("uebersetzungstool_PU");
            EntityManager em = emf.createEntityManager();

            List<Translation> translations = em.createQuery("SELECT ID FROM Translation t", Translation.class).getResultList();

            for (Translation translation : translations) {
                System.out.println(translation.getProject() + " " + translation.getVersion());

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Result of the JUnit test:

Running DatabaseConnectionTest
[EL Info]: 2023-02-21 15:16:33.412--ServerSession(1746570062)--EclipseLink, version: Eclipse Persistence Services - 4.0.0.v202210051929
An exception occurred while creating a query in EntityManager: 
Exception Description: Problem compiling [SELECT ID FROM Translation t]. 
[7, 9] The identification variable 'ID' is not defined in the FROM clause.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.157 s - in DatabaseConnectionTest

Results:

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time:  8.294 s
Finished at: 2023-02-21T15:16:34+01:00
------------------------------------------------------------------------

I would like to be able to get the data from my database, maybe with a query call. It has the same variabales as in the Translation.java file.


Solution

  • First you have to map each variable to the specific column it refers to. So that EclipseLink knows where to put the data. The mapping describes your table composition. For example what‘s the ID (PK), what‘s a column, …

    And you can try to use the Query object. With that you can use native queries. In your case it is the jakarta.persitence.Query import.