spring data,PagingAndSorting repository,基于类类型的(嵌套的)属性值进行排序

huangapple go评论108阅读模式
英文:

spring data, PagingAndSorting repository, Sort based on (nested) attribute value of class type

问题

以下是您要翻译的内容:

我有一个用于简单应用程序的PagingAndSorting存储库,其中包含人员信息。
像年龄这样的属性的简单排序效果不错...

  1. Sort sort = Sort.by("age").ascending();
  2. for (Person p : personRepository.findAll(sort)) {
  3. log.info(p.toString());
  4. }

我还想按lastName对人员进行排序。但是我没有为名和姓各创建两个属性。我创建了一个Name类,它包含名和姓,并在Person中创建了一个名为name的此类型属性...

  1. @Entity
  2. public class Person {
  3. // ...
  4. @Convert(converter = NameAttributeConverter.class)
  5. @Column
  6. private Name name;
  7. // ...
  8. }
  9. public final class Name implements Comparable, Serializable {
  10. // ...
  11. private String firstName;
  12. private String lastName;
  13. // ...
  14. }
  15. public class NameAttributeConverter implements AttributeConverter<Name, String> {
  16. {
  17. String firstName = attribute.getFirstName() == null ? " " : attribute.getFirstName();
  18. String lastName = attribute.getLastName() == null ? " " : attribute.getLastName();
  19. return firstName + " " + lastName;
  20. }
  21. public Name convertToEntityAttribute(String dbData) {
  22. if(dbData != null && dbData.split(" ").length > 0) {
  23. String fname = dbData.split(" ")[0];
  24. String lname = dbData.split(" ")[1];
  25. return new Name(fname, ' ', lname);
  26. }
  27. return null;
  28. }
  29. }

如何按lastName(或firstName)排序?类似于

  1. Sort sort = Sort.by("name").ascending();
  2. for (Person p : personRepository.findAll(sort)) {
  3. log.info(p.toString());
  4. }

执行并且没有错误(例如人员在控制台上打印出来),但不起作用(如预期所望)。

它使用firstName升序排序(不知道为什么)。

如何根据类名的lastName属性实现排序?

谢谢和问候。

英文:

I have a PagingAndSorting repository for a simple application which consists of Persons.
A simple sort for a property like age works well...

  1. Sort sort = Sort.by(&quot;age&quot;).ascending();
  2. for (Person p : personRepository.findAll(sort)) {
  3. log.info(p.toString());
  4. }

I want to sort persons by lastName as well. But I dont create two attributes for first and last name. I created a class Name which holds first and last name and make an name attribute of this type in Person...

  1. @Entity
  2. public class Person {
  3. // ...
  4. @Convert(converter = NameAttributeConverter.class)
  5. @Column
  6. private Name name;
  7. // ...
  8. }
  9. public final class Name implements Comparable, Serializable {
  10. // ...
  11. private String firstName;
  12. private String lastName;
  13. // ...
  14. }
  15. public class NameAttributeConverter implements AttributeConverter&lt;Name,String&gt; {
  16. {
  17. String firstName = attribute.getFirstName() == null ? &quot; &quot; : attribute.getFirstName();
  18. String lastName = attribute.getLastname() == null ? &quot; &quot; : attribute.getLastname();
  19. return firstName+&quot; &quot;+lastName;
  20. }
  21. public Name convertToEntityAttribute(String dbData) {
  22. if(dbData!=null &amp;&amp; dbData.split(&quot; &quot;).length &gt; 0) {
  23. String fname = dbData.split(&quot; &quot;)[0];
  24. String lname = dbData.split(&quot; &quot;)[1];
  25. return new Name(fname,&#39; &#39;,lname);
  26. }
  27. return null;
  28. }
  29. }

How can I sort by lastName (or firstName)? Something like

  1. Sort sort = Sort.by(&quot;name&quot;).ascending();
  2. for (Person p : personRepository.findAll(sort)) {
  3. log.info(p.toString());
  4. }

executes and brings no error (e.g. persons are printed on console) but doesnt work like expected.

It sorts ascending using the firstName (dont no why).

How can I acieve a sorting based on the lastName attribute of the class name?

Thanks and regards

答案1

得分: 1

你正在将name列的数据存储为数据库中的firstName+&quot; &quot;+lastName

因此,JPA按照名称对数据进行排序,意味着按照firstName+&quot; &quot;+lastName进行排序。

如果您在数据库中单独定义了firstname和lastname列,则可以通过lastname进行排序。
您可以在此处查看我的答案这里以解决此问题。

英文:

You are storing name column's data as firstName+&quot; &quot;+lastName in database.

So JPA sort data by name means sort by firstName+&quot; &quot;+lastName.

If you define firstname and lastname in database separate column then its possible to sort by lastname also.
You can follow my answer here for solve this issue.

huangapple
  • 本文由 发表于 2020年4月6日 18:29:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/61057726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定