在JPQL中通过排序定义引用字段的排序属性 [英] Define sort property for referenced field during order by in JPQL

查看:156
本文介绍了在JPQL中通过排序定义引用字段的排序属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体和投影的休息库:
$ b

I have rest repository with 2 entities and projection:

@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    String name;

    @ManyToOne
    Language language;
}

@Entity
public class Language {
    @Id
    @GeneratedValue
    private Long id;

    private String name;
}

public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
}

@Projection(name = "details", types = {Person.class})
interface PersonProjection {

    Long getId();

    String getName();

    @Value("#{target.language.name}")
    String getLanguage();
}

当我试图按语言字段排序时b
$ b

When I'm trying to sort it by language field


/ api / persons /?投射=详情& sort =语言

/api/persons/?projection=details&sort=language

它生成SQL,尝试按language.id排序它。

lockquote

select person0_.id as i ... order by language1_.id asc limit?

it produce SQL that trying to sort it by language.id

有没有办法告诉Spring Data,JPA使用不同的默认属性在没有在url中明确指定它的情况下进行排序?

select person0_.id as i... order by language1_.id asc limit ?

推荐答案

Spring配置时使用的属性遍历实体对象。
在您的情况下,对于人员和语言,您在属性名称上有名称冲突。
为了解决这个问题,你的排序参数应该引用正确的属性,即:
按照语言名称排序,你的url应该是/ api / persons /?projection = details& sort = language_name
按人名排序您的url参数shoud look / api / persons /?projection = details& sort = person_name

Spring data-rest as you configured it uses property traversal on your entity objects. In your case, for Person and Language you have a name clash on the property "name". In order to solve this issue your sort param should reference the correct property, i.e: For sorting by language name your url should be like /api/persons/?projection=details&sort=language_name For sorting by person's name your url parameter shoud look /api/persons/?projection=details&sort=person_name

这篇关于在JPQL中通过排序定义引用字段的排序属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆