How to apply where clause in JPA

0

I'm starting with JPA in netbeans and I set out to create a small maintainer of a course (school). I found a problem in obtaining a group of students according to the course, I have not given how to introduce the clause "where" or some grouper to save the records in a list:

 public List<Alumno> alumnos(String curso){
    List<Alumno> a = ejbFacade.findAll();
    return  a;
}

Could someone give me an idea of how to do it?

    
asked by Jonathan 16.09.2017 в 04:51
source

1 answer

-1
Query query = entitymanager.createQuery("select * from alumnos where nombre = :nombre");
query.setParameter("nombre", 'Jose');
List<String> list=query.getResultList();

Another way would be to create with @NamedQuery, the query you define in the entity

@Entity
@Tabel("alumnos")
@NameQuery("select * from alumnos where nombre = :nombre", name="buscar por nombre")// es un ejemplo
pulic class Alumno{

   @Id
   @GeneratedValue
   private Long id;

  @Column
  private String nombre;

}

and then you can do the following

Query query = entitymanager.createNamedQuery("buscar por nombre"); 
query.setParameter("nombre", 'Jose');
List<String> list=query.getResultList();
    
answered by 16.09.2017 в 22:40