How to do a search using jpa with searching for the foreign key

1

I am trying to perform a search with jpa, I have 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.

drug class

@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>();
    
asked by anonymous 26.08.2016 / 01:34

2 answers

1

Your problem is here:

m.usuario_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 entity method of m without the get, which is Drug. Since 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 must use the ": alias". To enter the value of the alias, you enter:

query.setParameter("idUsuario", id);

I hope I have helped:)

    
28.08.2016 / 17:23
1

If you have 1 user and he has a list of medicines and each drug in the list belongs to 1 user, you can do this:

@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 Medication 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 a brother example, note that I opened and closed the in the repository, but in the actual application I inject the in and I have a produces for this, I wrote so just to try to get the idea, hugs.     

26.08.2016 / 02:54