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

huangapple 未分类评论46阅读模式
英文:

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

问题

以下是您要翻译的内容:

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

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

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

@Entity
public class Person {
    // ...  
    @Convert(converter = NameAttributeConverter.class)
    @Column
    private Name name;
    // ...
}

public final class Name implements Comparable, Serializable {
    // ...
    private String firstName;
    private String lastName;
    // ...
}

public class NameAttributeConverter implements AttributeConverter<Name, String> {
    {
        String firstName = attribute.getFirstName() == null ? " " : attribute.getFirstName();
        String lastName = attribute.getLastName() == null ? " " : attribute.getLastName();
        
        return firstName + " " + lastName;
    }

    public Name convertToEntityAttribute(String dbData) {
        if(dbData != null && dbData.split(" ").length > 0) {
            String fname = dbData.split(" ")[0];
            String lname = dbData.split(" ")[1];
            
            return new Name(fname, ' ', lname);
        }
        
        return null;
    }
}

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

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

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

它使用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...

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

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...

@Entity
public class Person {
    // ...  
@Convert(converter = NameAttributeConverter.class)
@Column

private Name name;
    // ...
}

public final class Name implements Comparable, Serializable {
      // ...
      private String firstName;
      private String lastName;
      // ...
}

public class NameAttributeConverter implements AttributeConverter&lt;Name,String&gt; {
 {
		String firstName = attribute.getFirstName() == null ? &quot; &quot; : attribute.getFirstName();
	    String lastName =  attribute.getLastname() == null ? &quot; &quot; : attribute.getLastname();
	    
	    return  firstName+&quot; &quot;+lastName;
	}

	public Name convertToEntityAttribute(String dbData) {
	    if(dbData!=null &amp;&amp; dbData.split(&quot; &quot;).length &gt; 0) {
	        String fname = dbData.split(&quot; &quot;)[0];
	        String lname = dbData.split(&quot; &quot;)[1];
	        
	        return new Name(fname,&#39; &#39;,lname);
	      }
	        
	      return null;
	}
}

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

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

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://java.coder-hub.com/61057726.html
匿名

发表评论

匿名网友

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

确定