Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 对于jpa,如何强制它按特定变量排序?_Mysql_Spring_Spring Boot_Spring Data Jpa - Fatal编程技术网

Mysql 对于jpa,如何强制它按特定变量排序?

Mysql 对于jpa,如何强制它按特定变量排序?,mysql,spring,spring-boot,spring-data-jpa,Mysql,Spring,Spring Boot,Spring Data Jpa,我有一个项目,我使用SpringBootJPA中的Pageable类进行查询和排序。 想象一下,我有一个与ForumPost相关的项目对象,它基本上是一个论坛。 在我的DAO定义中,我有一个抽象类,它为所有ID定义一个uuid,一个createdDate、一个lasModified、一个createdBy和一个lastModifiedBy。当我试图从按createdDate排序的项目中获取一页帖子时,它是按项目createdDate而不是帖子排序的。 我有 其中order=“createdDat

我有一个项目,我使用SpringBootJPA中的Pageable类进行查询和排序。 想象一下,我有一个与ForumPost相关的项目对象,它基本上是一个论坛。 在我的DAO定义中,我有一个抽象类,它为所有ID定义一个uuid,一个createdDate、一个lasModified、一个createdBy和一个lastModifiedBy。当我试图从按createdDate排序的项目中获取一页帖子时,它是按项目createdDate而不是帖子排序的。 我有

其中order=“createdDate”和orderDir=“ASC”

如何强制它使用post createdDate进行排序

编辑:如评论中所述

这是hibernate打印出来的查询

select posts1_.id as id1_7_, posts1_.created_by as created_2_7_, posts1_.created_date as created_3_7_, posts1_.message as message4_7_, posts1_.project_id as project_5_7_ from projects projectdao0_ inner join forumposts posts1_ on projectdao0_.id=posts1_.project_id where projectdao0_.name=? order by projectdao0_.created_date desc limit ?
这是POJO

public class ForumPostDAO implements DAO {

    private static final long serialVersionUID = -7552574683834215423L;
    
    @Id
    @GeneratedValue(generator = "uuid2")
    @Column(columnDefinition = "BINARY(16)", unique = true)
    @GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
    private UUID id;
    
    @NotBlank
    @Size(min = 25)
    @Column(length = 2048)
    private String message;
    
    @CreatedDate
    private long createdDate;
    
    @CreatedBy
    private String createdBy;
    
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @JoinColumn(name = "project_id")
    @ManyToOne(cascade = { CascadeType.PERSIST , CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.LAZY)
    private ProjectDAO project;

    public ForumPostDAO(ForumPostDTO post) {
        this.message = post.getMessage();
        this.createdBy = post.getCreatedBy();
        this.createdDate = post.getCreatedDate();
    }
}

你能告诉我们你试过什么吗?除了创建一个基本上不包含关系的投影或重命名变量,没有任何效果。您想看什么?至少查询和部分POJO,@Aman我已经用您请求的内容编辑了帖子。从查询中,似乎您正在使用错误的存储库传递
PageRequest
public class ForumPostDAO implements DAO {

    private static final long serialVersionUID = -7552574683834215423L;
    
    @Id
    @GeneratedValue(generator = "uuid2")
    @Column(columnDefinition = "BINARY(16)", unique = true)
    @GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
    private UUID id;
    
    @NotBlank
    @Size(min = 25)
    @Column(length = 2048)
    private String message;
    
    @CreatedDate
    private long createdDate;
    
    @CreatedBy
    private String createdBy;
    
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @JoinColumn(name = "project_id")
    @ManyToOne(cascade = { CascadeType.PERSIST , CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.LAZY)
    private ProjectDAO project;

    public ForumPostDAO(ForumPostDTO post) {
        this.message = post.getMessage();
        this.createdBy = post.getCreatedBy();
        this.createdDate = post.getCreatedDate();
    }
}