Reputation: 75
I'm having trouble working with the JPA EntityManager, when trying a simple query like:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javax.persistence.*;
import tables.TeamMember;
public class BoburvikDatabase extends Application {
public EntityManagerFactory emf;
public EntityManager em;
public BoburvikDatabase BD;
public Stage primaryStage;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
emf = Persistence.createEntityManagerFactory("BoburvikDatabasePU");
em = emf.createEntityManager();
this.BD = this;
TypedQuery<TeamMember> q1 = em.createQuery(
"SELECT * FROM \"TeamMember\" WHERE \"MEMBER_ID\"=1000;",
TeamMember.class
);
TeamMember result = q1.getSingleResult();
System.out.println(result);
em.close();
}
public static void main(String[] args) {
launch(args);
}
}
But I'm getting errors saying:
[27, 27] A select statement must have a FROM clause.
[7, 7] The left expression is missing from the arithmetic expression.
[9, 27] The right expression is not an arithmetic expression.
[47, 52] '1000;' is not a valid numeric value.
The entity classes are included in the persistence unit, I tried to run the SQL command in pgAdmin and it worked and I think I am connecting to the database judging from this line in output:
[EL Info]: connection: 2020-05-28 13:58:17.977--ServerSession(927866519)--
file:/D:/Java/boburvik/BoburvikDatabase/dist/run285579090/BoburvikDatabase.jar_BoburvikDatabasePU
login successful
Any ideas where the fault could be?
Upvotes: 0
Views: 1340
Reputation: 1560
When you're working with EntityManager you're creating JPQL (Java Persistence Query Language statements). They are translated to SQL Statements by the EntityManager.
So instead of your SQL query try this JPQL query:
"SELECT t FROM TeamMember t WHERE memberId=1000",
Where "memberId" ist the propertyName for MEMBER_ID in your TeamMember entity (change if otherwise). For advanced queries you shouldn't put parameters inside your query string and use query parameters instead.
For more information look up Java Persistence Query Language.
You can also do native SQL queries with EntityManager if you need this.
Upvotes: 2