Spring Data Neo4j - findAll()没有返回排序结果

时间:2013-04-30 10:05:06

标签: spring neo4j spring-data-neo4j

我正在尝试使用具有排序属性的PageRequest使用GraphRepository的findAll(),但是对于某些共振,我的数据不会以排序的方式返回。这是我的代码:

public class Playground {

    /**
     * @param args
     */
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DefaultApplicationConfig.class);
        SocialRescueManager manager = (SocialRescueManager)context.getBean("socialRescueManager");

        manager.Civilians.deleteAll();

        manager.Civilians.save(new Civilian("B", "lastName1", Nationality.USA, EyeColor.BLUE, HairColor.BLOND, 180, new DateTime(1984, 1 , 9, 0, 0)));
        manager.Civilians.save(new Civilian("C", "lastName2", Nationality.USA, EyeColor.GREEN, HairColor.BLACK, 170, new DateTime(1985, 6 , 9, 0, 0)));
        manager.Civilians.save(new Civilian("A", "lastName3", Nationality.USA, EyeColor.BLUE, HairColor.BLOND, 175, new DateTime(1990, 10 , 23, 0, 0)));

        Pageable pageable = new PageRequest(0, 5, Direction.DESC, "firstName");

        Page<Civilian> results = manager.Civilians.findAll(pageable);

        for(Iterator<Civilian> i = results.iterator(); i.hasNext(); ) {
            Civilian civilian = i.next();
            System.out.println(civilian.getFirstName());
            System.out.println(civilian.getDateOfBirth());
        }

        context.close();
    }
}

My Civilian课程如下:

@NodeEntity
public class Civilian extends Profile {

    @GraphProperty(propertyType = Long.class)
    DateTime dateOfBirth;

    public Civilian() {
    }

    public Civilian(String firstName, String lastName, Nationality nationality, EyeColor eyeColor, HairColor hairColor, int height, DateTime dateOfBirth) {
        super(firstName, lastName, nationality, eyeColor, hairColor, height);
        this.setDateOfBirth(dateOfBirth);
    }

    public DateTime getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(DateTime dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }   
}

Civilian类扩展了Profile类,如下所示:

public abstract class Profile extends AbstractEntity {

    @Indexed(indexType = IndexType.FULLTEXT, indexName = "firstName")
    String firstName;

    @Indexed(indexType = IndexType.FULLTEXT, indexName = "lastName")
    String lastName;

    @Indexed
    EyeColor eyeColor;

    @Indexed    
    HairColor hairColor;

    @Indexed
    Nationality nationality;

    @Indexed
    int height;

    public Profile() {

    }

    public Profile(String firstName, String lastName, Nationality nationality, EyeColor eyeColor, HairColor hairColor, int height) {
        this.setFirstName(firstName);
        this.setLastName(lastName);
        this.setNationality(nationality);
        this.setHairColor(hairColor);
        this.setEyeColor(eyeColor);
        this.setHeight(height);
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public EyeColor getEyeColor() {
        return eyeColor;
    }

    public void setEyeColor(EyeColor eyeColor) {
        this.eyeColor = eyeColor;
    }

    public HairColor getHairColor() {
        return hairColor;
    }

    public void setHairColor(HairColor hairColor) {
        this.hairColor = hairColor;
    }

    public Nationality getNationality() {
        return nationality;
    }

    public void setNationality(Nationality nationality) {
        this.nationality = nationality;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

因此,我的Main()方法的结果如下:

B
1984-01-09T00:00:00.000+02:00
C
1985-06-09T00:00:00.000+03:00
A
1990-10-23T00:00:00.000+02:00

但正如您所看到的,我设置了我的PageRequest对象,以“firstName”属性按DESC顺序对结果进行排序,因此我希望首先打印C,然后打B,最后打A。

我做错了什么?

感谢。

1 个答案:

答案 0 :(得分:3)

CRUDRepository.findAll(Pageable)的javadoc状态:

  

注意:排序尚未实施

您必须提供修复,或者您也可以考虑使用启用了排序的密码查询。 E.g。

START n=node:firstName('firstName:abc')
RETURN n
ORDER BY n.firstName DESC