How can i sort by a property with "_" underscore in JPA/hibernate?

Viewed 1407

JPA split my sort property by "_" default, so it throw No property found exception. If i remove the underscore from variable, it works fine.

But i just wanna keep the "_" in my entity property, how can i do?

Variable:

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

Pageable:

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

The exception:

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)

Where split the property

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;
}
0 Answers
Related