How to search using jpa with searching for foreign key

Asked

Viewed 1,470 times

1

I am trying to perform a search with jpa, I am with the following query:

String jpql = "Select m from Medicamento m where m.usuario_id = ?1";

but this error occurs:

Exception in thread "main" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Problem compiling [Select m from Medicamento m where m.usuario_id = ?1]. 
[34, 46] The state field path 'm.usuario_id' cannot be resolved to a valid type.

class of medicine

@Entity
public class Medicamento implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String codigoBarras;
    private String nomeProduto;
    private String principioAtivo;
    private String apresentacao;
    private String laboratorio;
    private String classeTerapeutica;

user class

@Entity()
public class Usuario implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Integer idUsuario;
    private Integer idade;
    private String nome;
    private String email;
    private String senha;
    @JoinColumn(name = "usuario_id")
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<Medicamento> medicamentos = new ArrayList<Medicamento>();

2 answers

1


Your problem is here:

m.username_id = ? 1

As you are using jpql, the field name is not the name of the column in the database, but rather the name of the entity method of m without the get, which is Medicine. As the field is called id, the query should look like this:

String jpql = "Select m from Medicamento m where m.id = :idUsuario";

Also note that to use parameterized queries you should use ":alias". To enter the value of the alias, you put:

query.setParameter("idUsuario", id);

I hope I’ve helped :)

1

if you have 1 user and this has a list of medicines and each medicine on the list belongs to 1 user, you can do so:

@Entity()
public class Usuario implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Integer idUsuario;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "usuario_id")
    private List<Medicamento> medicamentos = new ArrayList<Medicamento>(); 
}

@Entity
public class Medicamento implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @JoinColumn(name = "usuario_id", referencedColumnName = "usuario_id")
    @ManyToOne(fetch = FetchType.LAZY)
    private Usuario usuario_id
}

Note the Mappedby that says that Drug is the owner of the relationship, so in the query you can do:

public Medicamento findMedicamentoByUserID(Long usuario_id) {
    EntityManager manager = JPAUtil.getEntityManager();
    Query query = null;
    Medicamento medicamento = null;
    try {
        query = manager.createQuery("SELECT m FROM Medicamento m where m.usuario_id =:usuario_id");
        query.setParameter("usuario_id", usuario_id);
        medicamento = (Medicamento) query.getSingleResult();
        JPAUtil.closeEntityManager();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (NoResultException noResultException) {
    }
    return medicamento;
}

This is just an example brother, note that I opened and closed it in the repository, but in the actual application I inject in and I have a produces for it, I wrote so just to try to pass the idea, hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.