Search code examples
javajpaeclipselink

JPA EclipseLink |getResultList returns always null


Hi there for my Project for distributed application I need to implement something with JPA.

I set up the database and I can insert new Data to the database. My main problem is that I want to search data and get them to the dialogue (JavaFX)

My entity class is a extended one. Heres the Code:

@Entity
@DiscriminatorValue("Marines")
public class Marines extends Einheit
// Einheit = getter and setter Methods 
{
    private int id;

    private String name;
    private String type; 
    private String punkte;
    private String modelSize;
    private String movement;
    private String closeCombat;
    private String rangedSkill;
    private String strength;
    private String conditions;
    private String wounds;
    private String armorValue;
    private String leadership;
    // Constructor (...) 
}

So my search call is with the id I want to search for the next entry in the dialoge with a buttonclick where id increments.

public List<Marines> showMarinesWithID(int id)
{
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("demo");
    EntityManager em = emf.createEntityManager();

    String jpql = "SELECT m FROM Marines m WHERE m.id = 1";// + id;
    Query query = em.createQuery(jpql);
    List<Marines> ma = query.getResultList();

    Marines foundMarine = em.find(Marines.class, 1);

    System.out.println(foundMarine.getName());
    return ma;
}

And there is already a problem: If I print it out it shows only null. As you can see I already tried the find method and a call with jpql. The value is always null. I have a search Method which works but there is an other case. There I just need the Name of the entries and display them. That looks like this:

public List<Marines> getMarineNames()
{
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("demo");
    EntityManager em = emf.createEntityManager();

    String jpql = "select m.name from Marines m";

    Query query = em.createQuery(jpql);

    List<Marines> marines = query.getResultList();

    return marines;
}

Update 1: The SQL log from the first method

[EL Fine]: sql: 2017-03-06 17:50:40.943--ServerSession(1404945014)--Connection(1537026053)--SELECT ID, ARMORVALUE, CLOSECOMBAT, CONDITIONS, LEADERSHIP, MODELSIZE, MOVEMENT, NAME, PUNKTE, RANGEDSKILL, STRENGTH, TYPE, WOUNDS FROM MARINES WHERE (ID = ?)
bind => [1 parameter bound]

Update 2: Screen of the database values: Database entries


Solution

  • (Posted on behalf of the OP).

    Well simple as that: the entity class Marines needs the getter and setter. With the extension they cannot be used.