2014-06-29 24 views
9

Stavo usando hibernate ma ho sentito che spring-data-jpa è il migliore così provo e sono soddisfatto fino a quando non affronterò questo problema.Equivalente ai criteri in spring-data-jpa

Ho un modulo di ricerca nel mio jsp con molti criteri e l'utente può scegliere quello che vuole.

Allora, qual è l'equivalente di questa richiesta in spring-data-jpa

if(startDate!=null){ 
    criteria.add(Expression.ge("date",startDate)); 
} 
if(endDate!=null){ 
    criteria.add(Expression.le("date",endDate)); 
} 
if(volume!=null){ 
    criteria.add(Expression.ge("volume",volume)); 
} 
if .... 

risposta

20

E 'QueryDSL. Here's un post sul blog su come usarlo con Spring Data.

+0

Penso che dovresti aver aggiunto un estratto del blog qui, nel caso in cui il collegamento smetta di funzionare. Grandi informazioni, a proposito. – elysch

6

l'equivalente in primavera Jpa Data è Specification ed è possibile utilizzare il repository SpecificationExecutor<T> e Jpa MetaModel per creare i criteri Jpa.

È possibile trovare un'introduzione su Jpa JpaSpecificationExecutor

Un esempio veloce:

  1. Entity

@Entity

public class ClassRoom { 
    // id and other properties 

    @ManyToOne 
    private School school; 

    private Date creationDate; 

    private String reference; 
    // Getters and setters 
} 

2.Repository:

@Repository 
public interface ClassRoomRepository extends JpaSpecificationExecutor<ClassRoom>{ 
} 

interfaccia 2.Service:

public interface ClassRoomService { 

List<ClassRoom> list(String reference, String schoolName, 
     Date creationDate) 
} 

3.Service implementaion:

import static yourpackage.ClassRoomSpecifications.*; 
import static org.springframework.data.jpa.domain.Specifications.*; 
@Service 
public class ClassRoomServiceImpl implements ClassRoomService { 

    @Resource 
    private ClassRoomRepository repository; 


    @Override 
    @Transactional(propagation = Propagation.SUPPORTS) 
    public List<ClassRoom> list(String reference, String schoolName, 
      Date creationDate) { 

     Specifications<ClassRoom> spec = null; 
     Specifications<ClassRoom> tempo = null; 

     spec = where(findPerSchool(schoolName)); 

     if (reference != null) { 
      tempo = where(findPerReference(reference)); 
     } 

     if (creationDate!=null) { 
      tempo = tempo == null ? where(findPerCreationDate(creationDate):tempo.and(findPerCreationDate(creationDate)); 
     } 
     spec = tempo == null ? spec : spec.and(tempo); 
     return repository.findAll(spec); 
    } 
} 
+1

'Specification' sono difficili da codificare e molto prolissi come si dice nel link che mi dai. – Youssef

+0

Guarda l'esempio su http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-four-jpa-criteria-queries/ – kervin

6

Con i dati di Primavera è sufficiente utilizzare i repository.

And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2 
    Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2 
    Between findByStartDateBetween … where x.startDate between 1? and ?2 
    LessThan findByAgeLessThan … where x.age < ?1 
    GreaterThan findByAgeGreaterThan … where x.age > ?1 
    IsNull findByAgeIsNull … where x.age is null 
    IsNotNull,NotNull findByAge(Is)NotNull … where x.age not null 
    Like findByFirstnameLike … where x.firstname like ?1 
    NotLike findByFirstnameNotLike … where x.firstname not like ?1 
    OrderBy findByAgeOrderByLastnameDesc … where x.age > ?1 order by x.lastname desc 
    Not findByLastnameNot … where x.lastname <> ?1 
Problemi correlati