Search code examples
sqloracleentity-frameworkjpaeclipselink

jpql select returns null in eclipselink


I have a very simple statement that bothers me and returns null eventhough the record is well saved in the database !!

I have an "administrateur" and "exploitant" entities that inherit "Utilisateur" entity (user in english xd) as follows :

====================== entity administrateur ===============

    @Entity
@DiscriminatorValue("A")
@Table(name="ADMINISTRATEUR")
public class Administrateur extends Utilisateur{

    public Administrateur(){

    }
}

====================== entity utilisateur ===============

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="TYPE_UTILISATEUR")
@Table(name="UTILISATEUR")
@Id
    @GeneratedValue(strategy=GenerationType.AUTO, generator="utilisateur_seq_gen")
    @SequenceGenerator(name="utilisateur_seq_gen", sequenceName="UTILISATEUR_SEQ", allocationSize = 1, initialValue = 1)
    protected Long id_utilisateur;
    @Column(name="NOM")
    protected String nom;
    @Column(name="PRENOM")
    protected String prenom;

    @Column(name="LOGIN")
    protected String login;
    @Column(name="MOT_PASSE_UTILISATEUR")
    protected String mot_passe_utilisateur;
    @Column(name="EMAIL")
    protected String email;

====================== entity explotant ===============

@Entity
@DiscriminatorValue("E")
@Table(name="EXPLOITANT")
public class Exploitant extends Utilisateur{

    @Column(name="ROLE")
    private String role;
public Exploitant(){

    }
}

I use this request to retrieve a user :

    public Utilisateur getUserTest(){
    EntityManager em=new JPAContainerFactory().createEntityManagerForPersistenceUnit("addressbook");
    em.getTransaction().begin();

TypedQuery<Utilisateur> query_user = em.createQuery("SELECT u FROM Utilisateur AS u WHERE u.id_utilisateur=1", Utilisateur.class);
    em.getTransaction().commit();   

    return query_user.getSingleResult();
}

Then i call it here :

final Utilisateur us=saut.getUserTest();

This is a simple request that doesn't work, the same is for this request :

    public Utilisateur getUserFromMail(String email){
    EntityManager em=new JPAContainerFactory().createEntityManagerForPersistenceUnit("addressbook");
    em.getTransaction().begin();

    TypedQuery<Utilisateur> query_user = em.createQuery("SELECT u FROM Utilisateur AS u WHERE u.email= :email", Utilisateur.class);
    query_user.setParameter("email", email);
    em.getTransaction().commit();   

    return query_user.getSingleResult();
}

Then i call it from here :

Utilisateur user=saut.getUserFromMail(emailString);
//user.setMot_passe_utilisateur(String.valueOf(pswd));
em.getTransaction().begin();
em.merge(user);
em.flush();
em.getTransaction().commit();

It says that getsingle result doesn't retrieve any result !!

In the database i'm 100% that the result exists :

enter image description here

I can't figure out the problem, can anyone help with a pertinent answer.

Thanks.

Edit

I got log gives the following error : enter image description here

Sauthentifier.java:150 = refer to :return query_user.getSingleResult();

===== Creer_encaissement.java:332= refer to final Utilisateur us=saut.getUserTest();

The request in the database return the appropriate result. Also i got this logout :

enter image description here


Solution

  • Common reasons for this issue:

    1. You might be checking the database with a different user/schema from the application
    2. You have the full data that is needed to build your entity. In this case, your Utilisateur should have a row in both the "UTILISATEUR" and "ADMINISTRATEUR" tables, and the ADMINISTRATEUR table needs an ID= 1 and Type=A. Both have to be there for a Utilisateur instance to exist.

    If that does not work, try persisting a Utilisateur and then reading that same instance back in.