0

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.

Peter Mulm
  • 60
  • 9
  • Since you are decided to use capital letters in your table and/or column names, you should not forget to do quoting such tables/columns in your queries, and to write them in a query exactly like you named it (any letter in the name of table/column must be in same case as it was when you created the table/column). – Rabban Keyak Feb 22 '23 at 07:30
  • How should I quote the query? Like this: `String queryString = "SELECT t FROM 'Translation' t";`, then this error comes up: **An exception occurred while creating a query in EntityManager: Exception Description: Problem compiling [SELECT t FROM 'Translation' t]. [14, 27] The abstract schema type ''Translation'' is unknown.** – Peter Mulm Feb 22 '23 at 07:58
  • Like this: `String queryString = "SELECT t FROM \"Translation\" t";`, then this error comes up: **An exception occurred while creating a query in EntityManager: Exception Description: Problem compiling [SELECT t FROM "Translation" t]. [14, 27] The abstract schema type '"Translation"' is unknown.** – Peter Mulm Feb 22 '23 at 07:58
  • You seem to be attempting an SQL query that is incomplete using api meant for JPQL (which is based on your entity object definitions, not the database table/columns). It should just be "Select t from Translation t" since you seem to have the query expecting fully formed Translation instances as the result. Use the Translation java entity property names in JPQL. – Chris Feb 22 '23 at 16:00
  • 1
    @Chris PostgreSQL has a naming convention. Therefor it couldn't find the "Translation" table, because it should be "translation". -> https://stackoverflow.com/questions/2878248/postgresql-naming-conventions – Peter Mulm Feb 28 '23 at 14:05
  • Database are usually case insensitive by default, and so if your table is translation in Postgres, you can query using Translation or TRANSLATION without issues. The issue in your question above is that you were using "SELECT ID FROM Translation t" and passing that string to EntityManager.createQuery. CreateQuery accepts JPQL, which if you just wanted an ID value returned, would need to have been "Select t.id from Translation t". You use table and column names in SQL, Java entity and property names in JPQL. You can use createNativeQuery("select ID from Translation") to execute SQL. – Chris Feb 28 '23 at 15:23
  • Otherwise, if you wanted translation instances back, you should be using em.createQuery("select t from Translation t").getResultList() as I suggested initially, or sql: em.createNativeQuery("select * from translation", Translation.class).getResultList() . JPQL is generally used as it matches the model you are getting back and allows the db to change underneath. – Chris Feb 28 '23 at 15:27
  • Yes the "SELECT ID FROM Translation t" query was was one big issue which I fixed immediatly after I realised what I wrote. – Peter Mulm Mar 01 '23 at 14:04

1 Answers1

0

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.