Search code examples
jakarta-eeejb

EJB Queries with WHERE clause


I'm working on a JavaEE application with EJB and I have these two entities:

@Entity
public class Utente implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private int cod_utente;
private String nome_utente;
private String morada_utente;
@Temporal(TemporalType.DATE)
private GregorianCalendar dnasc_utente;
private int tel_utente;
private List<GregorianCalendar> agenda;
@OneToMany(targetEntity=Entities.Prescricao.class,fetch=EAGER)
private List<Prescricao> lista_presc;

and

@Entity
public class FichaClinica implements Serializable{

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private int cod_ficha;
@OneToOne
private Utente cod_utente;
@OneToMany(targetEntity=Entities.AtoEnfermagem.class,fetch=EAGER)
private List <AtoEnfermagem> lista_atos_enf;
@OneToMany(targetEntity=Entities.AtoMedico.class,fetch=EAGER)
private List <AtoMedico> lista_atos_medicos;
@OneToMany(targetEntity=Entities.Consulta.class,fetch=EAGER)
private List<Consulta> lista_consultas;

I want to select the FichaClinica using a given Utente, so I'm using the following query (em is the EntityManager):

FichaClinica fc=(FichaClinica)em.createQuery("select object(fc) from FichaClinica fc where fc.cod_utente like :cod").setParameter("cod",u).getResultList().get(0);

But this isn't working. An error occurs:

Syntax error: Encountered "LIKE"

Can please someone help me with this?


Solution

  • If you have an non-detached instance of Utente and want to get the corresponding FichaClinica, consider adding the reverse side of the relationship in order to get a direct reference to it from the class Utente.

    If you want to use JPQL, you cannot use like with entities. Use =:

    select object(fc) from FichaClinica fc where fc.cod_utente = :cod