Sort by descending

The descending() method defined in Sort class is used to retrieve all the records of the entity from the underlying database and sort on the property provided in decending direction.

drop database if exists AppleDb;
create database AppleDb; 
use AppleDb;
CREATE TABLE apple ( apple_id bigint NOT NULL, apple_name varchar(255) DEFAULT NULL, taste varchar(255) DEFAULT NULL,  PRIMARY KEY (apple_id)) ENGINE=InnoDB;
create table hibernate_sequence (next_val bigint) engine=InnoDB;
insert into hibernate_sequence values ( 1 );
insert into apple (apple_name, apple_id, taste) values ("Macintosh", 1, "tangy");
insert into apple (apple_name, apple_id, taste) values ("Fuji", 2, "sweet");
insert into apple (apple_name, apple_id, taste) values ("Gala", 3, "juicy");
insert into apple (apple_name, apple_id, taste) values ("Jonagold", 4, "firm sweet");
insert into apple (apple_name, apple_id, taste) values ("GrannySmith", 5, "sharp");
insert into apple (apple_name, apple_id, taste) values ("PinkLady", 6, "mild sweet");
insert into apple (apple_name, apple_id, taste) values ("Golden Delicious", 7, "mild sweet");
insert into apple (apple_name, apple_id, taste) values ("Fireside", 8, "very sweet");
insert into apple (apple_name, apple_id, taste) values ("Jazz", 9, "tart ");
insert into apple (apple_name, apple_id, taste) values ("Mutsu", 10, "juicy");
insert into apple (apple_name, apple_id, taste) values ("Pinova", 11, "tart");
package com.sujan.example.jpa.controller;

import com.sujan.example.jpa.entity.Apple;
import com.sujan.example.jpa.repository.AppleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AppleController {
    @Autowired
    private AppleRepository appleRepository;

    @GetMapping
    void findAllSortByOrder() {
        List<Apple> appleList = appleRepository.findAll(Sort.by("appleName").descending());
        appleList.forEach(System.out::println);
    }
}
package com.sujan.example.jpa.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Data
@ToString
@Entity
@NoArgsConstructor
public class Apple {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long appleId;
    private String appleName;

    public Apple(String appleName) {
        this.appleName = appleName;
    }
}
package com.sujan.example.jpa.repository;

import com.sujan.example.jpa.entity.Apple;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AppleRepository extends JpaRepository<Apple, Long> {
}
package com.sujan.example.jpa;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JpaApplication {
    public static void main(String[] args) {
        SpringApplication.run(JpaApplication.class, args);
    }
}
spring.datasource.url=jdbc:mysql://localhost:3306/AppleDb
spring.datasource.username=root
spring.datasource.password=root
plugins {
    id 'org.springframework.boot' version '2.3.3.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}
group = 'com.sujan'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}
repositories {
    mavenCentral()
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    annotationProcessor 'org.projectlombok:lombok'
    compileOnly 'org.projectlombok:lombok'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
test {
    useJUnitPlatform()
}
curl --location --request GET 'http://localhost:8080/'
Hibernate: select apple0_.apple_id as apple_id1_0_, apple0_.apple_name as apple_na2_0_, apple0_.taste as taste3_0_ from apple apple0_ order by apple0_.apple_name desc
Apple(appleId=11, appleName=Pinova, taste=tart)
Apple(appleId=6, appleName=PinkLady, taste=mild sweet)
Apple(appleId=10, appleName=Mutsu, taste=juicy)
Apple(appleId=1, appleName=Macintosh, taste=tangy)
Apple(appleId=4, appleName=Jonagold, taste=firm sweet)
Apple(appleId=9, appleName=Jazz, taste=tart )
Apple(appleId=5, appleName=GrannySmith, taste=sharp)
Apple(appleId=7, appleName=Golden Delicious, taste=mild sweet)
Apple(appleId=3, appleName=Gala, taste=juicy)
Apple(appleId=2, appleName=Fuji, taste=sweet)
Apple(appleId=8, appleName=Fireside, taste=very sweet)

follow us on