我正在研究一个REST API,我试图在会话中找到表现最好的n个。
这是存储用户信息的实体用户
@Entity
@Table(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 7401548380514451401L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@Column(name = "name")
String name;
@NotNull
@Email
@Column(name = "email")
String email;
@Column(name = "registration_number")
String registrationNumber;
// getter and setter
}
这是保存会话详细信息的第二个实体:-
@Entity
@Table(name = "session")
public class Session implements Serializable {
private static final long serialVersionUID = 0x7E414EAA3DB0C391L;
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
Long id;
@Column(name = "start_time")
@NotNull
String startTime;
@Column(name = "end_time")
@NotNull
String endTime;
@Column(name = "distance")
Long distance;
@JoinColumn(name = "driver_id", referencedColumnName = "id")
@ManyToOne
User trainer;
@JoinColumn(name = "sessionr_id", referencedColumnName = "id")
@ManyToOne
User trainee; //getter and setters
}
这是第三个用于保存会话和用户实体结果的模型
公共类TopTrainer {
private String name;
private String email;
private Long totalSessionDurationInSeconds;
private Long maxSessionDurationInSecods;
private Double averageDistance;
/**
* Constructor for TopTrainer
*
* @param name
* @param email
* @param totalSessionDurationInSeconds
* @param maxSessionDurationInSecods
* @param averageDistance
*/
public TopTrainer(String name, String email, Long totalSessionDurationInSeconds, Long maxSessionDurationInSecods,
Double averageDistance) {
this.setName(name);
this.setEmail(email);
this.setAverageDistance(averageDistance);
this.setMaxSessionDurationInSecods(maxSessionDurationInSecods);
this.setTotalSessionDurationInSeconds(totalSessionDurationInSeconds);
}
public TopTrainer() {
}
//Getter and Setter}
这是调用培训师服务以获得所需结果的控制器
@GetMapping(path = "/api/top-trainer")
public ResponseEntity<List<TopTrainer>> getTopTrainer(@RequestParam(value = "max", defaultValue = "5") Long count,
@RequestParam(value = "startTime", required = true) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime startTime,
@RequestParam(value = "endTime", required = true) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime endTime) {
//To do
}
我被困在这里已经很长时间了。在其中,根据他们的平均会话次数,所选的总会话时间和最大会话时间,我在所选时间戳之间列出了前n名教练。
我是JPA (CrudRepository<Session,Long>)
的新手,我无法理解如何提取所需的结果以及如何对实体执行聚合。
@RestResource(exported = false)
public interface SessionRepository extends CrudRepository<Session, Long> {
Optional<List<TopTrainer>> findByStartTimeAfterAndEndTimeBefore(String startTime, String endTime);
}
任何人都可以]回答此问题。我们也可以使用条件查询