Bjorn Pijpops
Bjorn Pijpops

Reputation: 373

JPA createQuery/TypedQuery, not recognising my table

This is the function I am calling:

    public List<Student> getAllStudents() {
        TypedQuery<Student> query = entityManager.createQuery("SELECT e FROM Student as e", Student.class);
        List<Student> students = query.getResultList();

        return students;
    }

This is my Student class:

@Entity
@Table(name = "Students")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(unique = true)
    private String studentNumber;
    private String firstName;
    private String lastName;
    private boolean uploaded;
    private boolean downloaded;
    private boolean active;

    // Default constructor
    public Student() {

    }
}

Problem solved! Thanks for help guys, still a new guy to this and made a few dumb mistakes!

Upvotes: 0

Views: 1001

Answers (2)

Bjorn Pijpops
Bjorn Pijpops

Reputation: 373

So I fixed the issue.

The code itself is correct, Intellij however used the wrong standard JPA Framework configuration. After implementing the correct one, all the errors went away.

Another thing I changed was add this line to my persistence.xml, to make sure preload was true:

<property name="openjpa.MetaDataRepository" value="Preload=true" />

Thanks for all the people responding to this question!

Upvotes: 0

Max
Max

Reputation: 1157

Your query should look like this: "SELECT e FROM Student e"

Note that the entity is singular (Not students), you need to ad "FROM", and you dont't need to include the word "AS".

I learned JPA from this tutorial: https://docs.oracle.com/javaee/7/tutorial/partpersist.htm#BNBPY

You can view JPQL examples here: https://docs.oracle.com/javaee/7/tutorial/persistence-querylanguage002.htm#BNBRG

Upvotes: 1

Related Questions