如何在 JPA/hibernate 中按带有“_”下划线的属性进行排序?

2024-01-15

JPA 默认按“_”拆分我的排序属性,因此它会抛出“找不到属性”异常。 如果我从变量中删除下划线,它就可以正常工作。

但我只想将“_”保留在我的实体属性中,我该怎么办?

多变的:

@Column(name = "CREATE_TIME")
private LocalDateTime create_Time;

可分页:

Pageable pageable = new PageRequest(page, 10, Sort.Direction.DESC,"create_Time");

例外情况:

org.springframework.data.mapping.PropertyReferenceException: No property create found for type Rtm_App!

at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243)
at org.springframework.data.jpa.repository.query.QueryUtils.toJpaOrder(QueryUtils.java:546)
at org.springframework.data.jpa.repository.query.QueryUtils.toOrders(QueryUtils.java:500)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:653)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:608)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:407)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:377)

财产在哪里分割

public class PropertyPath implements Iterable<PropertyPath> {

private static final String DELIMITERS = "_\\.";
private static final String ALL_UPPERCASE = "[A-Z0-9._$]+";
private static final Pattern SPLITTER = Pattern.compile("(?:[%s]?([%s]*?[^%s]+))".replaceAll("%s", DELIMITERS));

public static PropertyPath from(String source, TypeInformation<?> type) {
    List<String> iteratorSource = new ArrayList<String>();
    Matcher matcher = SPLITTER.matcher("_" + source);
    while (matcher.find()) {
        iteratorSource.add(matcher.group(1));
    }

    Iterator<String> parts = iteratorSource.iterator();

    PropertyPath result = null;
    Stack<PropertyPath> current = new Stack<PropertyPath>();

    while (parts.hasNext()) {
        if (result == null) {
            result = create(parts.next(), type, current);
            current.push(result);
        } else {
            current.push(create(parts.next(), current));
        }
    }

    return result;
}

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 JPA/hibernate 中按带有“_”下划线的属性进行排序? 的相关文章

随机推荐