2016-12-15 34 views
0

我有两个entitis,资料库和类实现规格:弹簧数据与规范和排序财产

@Entity 
    public class Person { 
     @Id 
     @GeneratedValue(strategy=GenerationType.AUTO) 
     private Long id; 
     private String name; 
     @OneToOne 
     @JoinColumn(name = "phone_id") 
     private Phone phone; 

@Entity 
public class Phone { 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Long id; 
    private String number; 

    public Long getId() { 
     return id; 
    } 

public interface PersonRepositories extends CrudRepository<Person, Long>, JpaSpecificationExecutor<Person> { 
} 

public class PersonSpecification implements Specification<Person> { 

    private String name; 
    public PersonSpecification(String name) { 
     this.name=name; 
    } 
    @Override 
    public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder builder) { 
     List<Predicate> predicates = new ArrayList<>(); 
     if(name!=null) { 
      predicates.add(builder.like(root.get("name"), name)); 
     } 
     query.groupBy(root.get("id")); 
     return builder.and((predicates.toArray(new Predicate[predicates.size()]))); 
    } 



    @Service 
    public class PersonSearchServicesImpl implements PersonSearchServices { 
     @Autowired 
     PersonRepositories personRepositories; 
     @Override 
     public List<Person> searchPerson(String name, String order) { 
      PersonSpecification person = new PersonSpecification(name); 
      Direction direction = Sort.Direction.ASC; 
      String[] sortProp = {order}; 
      Sort sort = new Sort(direction, sortProp); 
      Pageable pageable = new PageRequest(0, 10, sort); 

      Page<Person> findAll = personRepositories.findAll(person, pageable); 
     return findAll.getContent(); 
    } 

一些样本数据:

@SpringBootApplication 
public class SpringOrmSpecApplication implements CommandLineRunner { 

    @Autowired 
    PersonSearchServices services; 
    @Autowired 
    PhoneRepositories phoneRepositories; 
    @Autowired 
    PersonRepositories personRepositories; 

    public static void main(String[] args) { 
     SpringApplication.run(SpringOrmSpecApplication.class, args); 
    } 

    @Override 
    public void run(String... arg0) throws Exception { 
     Phone phone = new Phone(); 
     phone.setNumber("AAA"); 
     phoneRepositories.save(phone); 

     Phone phone2 = new Phone(); 
     phone2.setNumber("BBB"); 
     phoneRepositories.save(phone2); 

     Person person = new Person(); 
     person.setName("Barry"); 
     person.setPhone(phone); 
     personRepositories.save(person); 

     Person person2 = new Person(); 
     person2.setName("Alfred"); 
     person2.setPhone(phone2); 
     personRepositories.save(person2); 

     Person person3 = new Person(); 
     person3.setName("Alfred"); 
     person3.setPhone(phone); 
     personRepositories.save(person3); 

     search(); 
    } 
public void search(){ 
     List<Person> searchPerson = services.searchPerson("Alfred", "phone"); 
     searchPerson.stream().forEach(p -> System.out.println(p)); 
    } 

在数据库H2搜索时效果很好,但在Postgres的对于来自这些参数的数据会得到一个错误:

ERROR: column "phone1_.id" must appear in the GROUP BY clause or be used in an aggregate function 

我该如何解决它?

回答

0

对不起,这个答案很薄弱,但我不得不为Oracle解决这个问题(我实际上并不明白它为什么适用于h2)简短的回答,你必须做它要求的内容,并将该字段添加到通过...分组。这个语法可能不完整,并且是错误的。

query.groupBy(root.get("phone").get("id")) 
+0

我还没有找到更好的解决方案。 – Wait