2014-06-29 8 views
9

Ich war mit hibernate aber ich hörte, dass spring-data-jpa ist das beste, also versuche ich es und ich bin zufrieden mit ihm, bis ich dieses Problem habe.Äquivalent von Kriterien in Frühling-Daten-Jpa

Ich habe ein Suchformular in meinem jsp mit vielen Kriterien und der Benutzer kann wählen, was er will.

Also, was ist das Äquivalent zu dieser Anfrage 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 .... 

Antwort

20

Es QueryDSL ist. Here's ein Blogbeitrag zur Verwendung mit Spring Data.

+0

Ich denke, Sie sollten hier einen Auszug des Blogs hinzugefügt haben, nur für den Fall, dass der Link nicht mehr funktioniert. Tolle Infos übrigens. – elysch

6

Das Äquivalent in Spring Jpa Daten ist Specification, und Sie können das Repository SpecificationExecutor<T> und Jpa MetaModel verwenden, um Ihre Jpa-Kriterien zu erstellen.

Sie eine Einführung über JPA JpaSpecificationExecutor

ein kurzes Beispiel finden:

  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>{ 
} 

2.Service Schnittstelle:

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

'Spezifikation' sind schwer zu codieren und sehr ausführlich wie das sagen in dem Link, den Sie mir geben. – Youssef

+0

Beachte das Beispiel unter http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-four-jpa-criteria-queries/ – kervin

6

mit Frühlings-Daten, die Sie brauchen nur Repositories zu verwenden.

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