Charles Yapp
Charles Yapp

Reputation: 566

JPA - Only primary key is retrieved, other fields returned null

I am new to Java EE.
I am using Eclipse Juno + Eclipse Link + MySQL.
Currently I have a JPA project and a dynamic web project.
The JPA project is included in the web project's build path.
I created a class "StudentDomain" in JPA project to retrieve rows from Student table.
It works fine in the JPA project.
But in web project, only ID is returned, other fields, name, gender, and email all returned null.

For example:
SQL: SELECT * FROM Student;

In JPA Project, I got the following results:

ID Name   Gender Email
1  John   M      [email protected]
2  Emma   F      [email protected]

In Web Project, I got the following results:

ID Name   Gender Email
1  null   null   null
2  null   null   null

Below is the source code of Student entity class.

package model;

import java.io.Serializable;
import java.lang.String;
import javax.persistence.*;

@Entity
public class Student implements Serializable
{
    @Id
    @Column(name = "ID")
    private int id;

    @Column(name = "name")
    private String name;

    @Column(name = "gender")
    private String gender;

    @Column(name = "email")
    private String email;

    private static final long serialVersionUID = 1L;

    public Student()
    {
    }

    public int getId()
    {
        return this.id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getName()
    {
        return this.name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getGender()
    {
        return this.gender;
    }

    public void setGender(String gender)
    {
        this.gender = gender;
    }

    public String getEmail()
    {
        return this.email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

}

Upvotes: 0

Views: 1593

Answers (1)

Chris
Chris

Reputation: 21145

What version are you using? If this is a native SQL query you are executing, it could be a case sensitivity issue on the column metadata returned from the query. Depending on the EclipseLink version, you can try setting the persistence property "eclipselink.jpa.uppercase-column-names" to true (add in EclipseLink 2.1), or upgrade to the latest version where it is true by default.

You might also try making sure the case of the fields defined in the column annotations matches what is returned from the database - the ID column is mapped to "ID" while the others are all in lower case, which might be a problem on databases returning upper case columns by default.

This would only be an issue with native SQL. You could also try using a JPQL query such as em.createQuery("Select s from Student s") instead.

Upvotes: 2

Related Questions