JPA @OrderBy注释不能与@ManyToMany一起使用

时间:2017-09-29 19:47:16

标签: java mysql spring jpa

我有实体类something for applying the function ptr to each element of the containerClassroom。这是我的ApuStudent课程:

Classroom

注释@Entity(name = "Classroom") @Table(name = "classroom") public class Classroom extends BaseEntity { private static final long serialVersionUID = -6119375402140845721L; @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE) @JoinTable(name = "classroom_student", joinColumns = @JoinColumn(name = "classroom_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "student_id", referencedColumnName = "id")) @OrderBy("name") private Set<ApuStudent> student = new HashSet<>(); } 将引发异常

@OrderBy

这是我的Unknown column 'student5_.name' in 'order clause' 课程:

ApuStudent

这是输出中的sql:

@Entity(name = "ApuStudent")
@Table(name = "apu_student")
public class ApuStudent extends BaseEntity
{
    @Column(name = "tp", nullable = false, length = 8, unique = true)
    private String tp;
    @Column(name = "name", nullable = false, length = 100)
    private String name;
}

任何想法?

更新
我正在使用MySQL和Spring JPA,如果我直接从select classroom0_.id as id1_6_0_, classroom0_.date_created as date_cre2_6_0_, classroom0_.date_modified as date_mod3_6_0_, classroom0_.admin as admin7_6_0_, classroom0_.classroom_id as classroo4_6_0_, classroom0_.lecturer as lecturer8_6_0_, classroom0_.module as module9_6_0_, classroom0_.password as password5_6_0_, classroom0_.second_marker as second_10_6_0_, classroom0_.status as status6_6_0_, apuadmin1_.id as id1_1_1_, apuadmin1_.date_created as date_cre2_1_1_, apuadmin1_.date_modified as date_mod3_1_1_, apuadmin1_.aid as aid4_1_1_, apuadmin1_.name as name5_1_1_, apulecture2_.id as id1_2_2_, apulecture2_.date_created as date_cre2_2_2_, apulecture2_.date_modified as date_mod3_2_2_, apulecture2_.lid as lid4_2_2_, apulecture2_.name as name5_2_2_, apumodule3_.id as id1_3_3_, apumodule3_.date_created as date_cre2_3_3_, apumodule3_.date_modified as date_mod3_3_3_, apumodule3_.module_code as module_c4_3_3_, apumodule3_.module_title as module_t5_3_3_, apulecture4_.id as id1_2_4_, apulecture4_.date_created as date_cre2_2_4_, apulecture4_.date_modified as date_mod3_2_4_, apulecture4_.lid as lid4_2_4_, apulecture4_.name as name5_2_4_, student5_.classroom_id as classroo1_6_5_, apustudent6_.id as student_2_7_5_, apustudent6_.id as id1_4_6_, apustudent6_.date_created as date_cre2_4_6_, apustudent6_.date_modified as date_mod3_4_6_, apustudent6_.name as name4_4_6_, apustudent6_.tp as tp5_4_6_ from classroom classroom0_ inner join apu_admin apuadmin1_ on classroom0_.admin=apuadmin1_.id inner join apu_lecturer apulecture2_ on classroom0_.lecturer=apulecture2_.id inner join apu_module apumodule3_ on classroom0_.module=apumodule3_.module_code left outer join apu_lecturer apulecture4_ on classroom0_.second_marker=apulecture4_.id left outer join classroom_student student5_ on classroom0_.id=student5_.classroom_id left outer join apu_student apustudent6_ on student5_.student_id=apustudent6_.id where classroom0_.id=? order by student5_.name 选择Classroom,我发现问题不会发生。当我从其他实体类中选择时会发生问题。例如,在这种情况下,我从classroomRepo.findAll();类中选择了这样的:Assignment。这是我的assignmentRepo.findAll();课程:

Assignment

如果我直接从classroomRepo中选择,它将会有这样的查询:

@Entity(name = "Assignment")
@Table(name = "assignment")
public class Assignment extends BaseEntity
{
    private static final long serialVersionUID = 8962490607828514209L;
    @ManyToOne
    @JoinColumn(name = "classroom", nullable = false, referencedColumnName = "id")
    private Classroom classroom;
    //other columns omitted
}

但是,如果我从assignmentRepo中选择,则查询将会有所不同:

select
    student0_.classroom_id as classroo1_6_0_,
    student0_.student_id as student_2_7_0_,
    apustudent1_.id as id1_4_1_,
    apustudent1_.date_created as date_cre2_4_1_,
    apustudent1_.date_modified as date_mod3_4_1_,
    apustudent1_.name as name4_4_1_,
    apustudent1_.tp as tp5_4_1_ 
from
    classroom_student student0_ 
inner join
    apu_student apustudent1_ 
        on student0_.student_id=apustudent1_.id 
where
    student0_.classroom_id=? 
order by
    apustudent1_.name

它没有选择几列:

left outer join
    classroom_student student5_ 
        on classroom0_.id=student5_.classroom_id 

我可以知道如何解决这个问题吗?

0 个答案:

没有答案