0

I am totally new to JPA and try to get at it with the tools provided from my University. So what I am trying to do is to set up a database carsdb with one table car and read the table from my main with a JPQL Query.

What I did so far: I created a user carsdbuser with password carsdbpw, created a postgres database carsdb, which is owned by carsdbuser, added a table car and inserted a few columns.

I created a new Intellij project with JPA.

I then added the postgresql jdbc drivers (I think... the file was given to us by the university) postgresql-42.2.1.jar as well as the oracle provider eclipslink.jar.

I then modified the persistence.xml to look like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

<persistence-unit name="carsdb" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>Car</class>
    <properties>
        <property name="eclipselink.jdbc.url" value="jdbc:postgresql://localhost:5432/carsdb"/>
        <property name="eclipselink.jdbc.driver" value="org.postgresql.Driver"/>
        <property name="eclipselink.jdbc.user" value="carsdbuser"/>
        <property name="eclipselink.jdbc.password" value="carsdbpw"/>
        <property name="eclipselink.target-database" value="PostgreSQL"/>
        <property name="eclipselink.logging.level" value="ALL"/>
    </properties>
</persistence-unit>
</persistence>

I created the Class Car.java:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "car")
public class Car {

    @Id
    @GeneratedValue(generator = "incrementor")
    private int Id;

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }
    private String Name;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    @Override
    public String toString() {
        return "Car " + Id + ": Name: " + Name;
    }
}

as well as the Main:

import javax.persistence.*;
import java.util.List;

public class Main {
    public static void main(String[] args){
        var factory = Persistence.createEntityManagerFactory("carsdb");
        EntityManager em = factory.createEntityManager();

        Query query = em.createQuery("select a from Car a");
        List<Car> list = query.getResultList();
        for (Car c : list) {
            System.out.println(c);
        }
    }
}

Given the tutorials I am following this looks good to me, however when I run the program I get the following Error Message:

Exception in thread "main" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Problem compiling [select a from Car a]. 
[14, 17] The abstract schema type 'Car' is unknown.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1743)
    at Main.main(Main.java:9)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.7.1.v20171221-bd47e8f): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [select a from Car a]. 
[14, 17] The abstract schema type 'Car' is unknown.
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:347)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:140)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1741)
    ... 1 more

I tried to find some solutions with the help of some classmates and google, but did not find any solution that helped.

I added the Database in Intellij to make sure I got the right url. The connection test works properly and I also find my car table in the Intellij database view.

The following Question discusses a similar Issue: Error on compiling query: The abstract schema type 'entity' is unknown

I do however have Car in the select statement which is the case sensitive name of the entity, so I cant see how it is related to my problem.

Sidereus
  • 200
  • 3
  • 13
  • 2
    Never, ever, ever put your classes in the default package. – JB Nizet Jul 04 '19 at 21:41
  • Possible duplicate of [Error on compiling query: The abstract schema type 'entity' is unknown](https://stackoverflow.com/questions/20193581/error-on-compiling-query-the-abstract-schema-type-entity-is-unknown) – Andrey Jul 05 '19 at 06:28
  • @JBNizet Is this a possible cause or just some additional information? – Sidereus Jul 05 '19 at 07:56
  • @Andrey I already tried this. It does not seem to be the solution I need. I tried it with the Table as well as without the Table Attribute. I tried select c from car c as well as select c from Car c, but nothing seems to work, which is why I think I have some configuration issue. Maybe something with the persistence.xml. I really don't know. – Sidereus Jul 05 '19 at 08:01

0 Answers0