Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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
Java 如何使用@OrderBy对Hibernate中的一个组合键进行排序_Java_Hibernate_Sorting_Sql Order By_Composite Key - Fatal编程技术网

Java 如何使用@OrderBy对Hibernate中的一个组合键进行排序

Java 如何使用@OrderBy对Hibernate中的一个组合键进行排序,java,hibernate,sorting,sql-order-by,composite-key,Java,Hibernate,Sorting,Sql Order By,Composite Key,我有两个班课程和学生Student类使用firstName和lastName作为复合键。我想在课程类中使用@OrderBy(“firstName ASC”),但出现错误“未找到@OrderBy子句的属性:Student.firstName” 如何对其中一个组合键(如firstName)进行排序? public class Course{ @OneToMany(mappedBy="course") @OrderBy("firstName ASC") // Error:

我有两个班
课程
学生
Student
类使用
firstName
lastName
作为复合键。我想在
课程
类中使用
@OrderBy(“firstName ASC”)
,但出现错误“未找到@OrderBy子句的属性:Student.firstName”

如何对其中一个组合键(如
firstName
)进行排序?

public class Course{
    @OneToMany(mappedBy="course")
    @OrderBy("firstName ASC")  
    // Error: property from @OrderBy clause not found: Student.firstName, why?
    private List<Student> students;
.....
}


public class Student{
    @Id
    @Column(name="first_name")
    private String firtName;
    @Id
    @Column(name="last_name")
    private String lastName;

    @ManyToOne
    @JoinColumn(name="course_id")
    private Course course;
.....
}
公共课{
@OneToMany(mappedBy=“课程”)
@订购人(“名ASC”)
//错误:@OrderBy子句中的属性未找到:Student.firstName,为什么?
私人名单学生;
.....
}
公立班学生{
@身份证
@列(name=“first_name”)
私有字符串firtName;
@身份证
@列(name=“last_name”)
私有字符串lastName;
@许多酮
@JoinColumn(name=“课程id”)
私人课程;
.....
}

您将其拼错为,
firtName
——注意
s
缺失。解决了这个问题,事情很可能会好起来

[编辑]

以防它仍然不起作用。请尝试用
@EmbeddedId
替换它。如下图所示

public class Student implements Serializable{
    @EmbeddedId
    private StudentPK name;

    @ManyToOne
    @JoinColumn(name="course_id")
    private Course course;
    .....

    @Embeddable
    public static class StudentPK implements Serializable {
        @Column(name="first_name")
        private String firtName;

        @Column(name="last_name")
        private String lastName;

        ....
    }
}
那么它应该使用

public class Course{
    @OneToMany(mappedBy="course")
    @OrderBy("name.firstName ASC")  
    private List students;
    .....
}

您将其拼错为,
firtName
——注意
s
缺失。解决了这个问题,事情很可能会好起来

[编辑]

以防它仍然不起作用。请尝试用
@EmbeddedId
替换它。如下图所示

public class Student implements Serializable{
    @EmbeddedId
    private StudentPK name;

    @ManyToOne
    @JoinColumn(name="course_id")
    private Course course;
    .....

    @Embeddable
    public static class StudentPK implements Serializable {
        @Column(name="first_name")
        private String firtName;

        @Column(name="last_name")
        private String lastName;

        ....
    }
}
那么它应该使用

public class Course{
    @OneToMany(mappedBy="course")
    @OrderBy("name.firstName ASC")  
    private List students;
    .....
}

这不是拼写错误造成的,我纠正了它,但仍然得到了错误。似乎hibernate将复合键实现为一个内部字段,而firstName对Student不再是有效字段。如果这是真的,有没有办法按名字排序?默认值是按主键{firstName,lastName}排序,但我只需要按名字排序。@hebingliu:相应地更新了我的帖子。这不是拼写错误造成的,我更正了,但仍然得到了错误。似乎hibernate将复合键实现为一个内部字段,而firstName对Student不再是有效字段。如果这是真的,有没有办法按名字排序?默认是按主键{firstName,lastName}排序,但我只需要按名字排序。@hebingliu:相应地更新了我的帖子。