简体   繁体   English

HQL查询问题

[英]Issue with HQL query

I have a problem trying to retrieve items from my database using HQL. 我在尝试使用HQL从数据库检索项目时遇到问题。 I have 2 tables (Student and Classroom) and I want to retrieve all Students together with the Classroom they belong to. 我有2个表(“学生”和“教室”),我想检索所有学生以及他们所属的教室。

Student table 学生桌

user_id  |   name    |    nick   |  password |  surname  | classroom_id
---------|-----------|-----------|-----------|-----------|--------------
   2     |   AAA     | studentA  |  test123  |   AAA     |     1
   3     |   BBB     | studentB  |  test321  |   BBB     |     1

Classroom table 教室桌

  classroom_id |   name     |    year 
 --------------|------------|----------
       1       | TestClass  |  2016/2017

Here are the tables as Java classes in case anyone wants to see them. 这是Java类的表格,以防万一有人希望看到它们。

Student 学生

@Entity
public class Student extends User{

    private Class studentClass;

    public Student() {
    }

    public Student(String name, String surname, String nick, String password, Class studentClass) {
        super(name, surname, nick, password);
        this.studentClass = studentClass;
    }

    @ManyToOne
    @JoinColumn(name = "classroom_id")
    public Class getStudentClass() {
        return studentClass;
    }

    public void setStudentClass(Class studentClass) {
        this.studentClass = studentClass;
    }
}

Classroom 课堂

@Entity
public class Classroom {

    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleStringProperty year = new SimpleStringProperty();

    private int classroom_id;

    public Classroom() {
    }

    public Classroom(String name, String year) {
        this.name.set(name);
        this.year.set(year);
    }


    @Column(nullable = false)
    public String getName() {
        return name.get();
    }

    public SimpleStringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }

    @Column(nullable = false)
    public String getYear() {
        return year.get();
    }

    public SimpleStringProperty yearProperty() {
        return year;
    }

    public void setYear(String year) {
        this.year.set(year);
    }


    @Id
    @GenericGenerator(name="id" , strategy="increment")
    @GeneratedValue(generator="id")
    public int getClassroom_id() {
        return classroom_id;
    }

    public void setClassroom_id(int classroom_id) {
        this.classroom_id = classroom_id;
    }

Now, here is the method with the query I'm trying to use to get the items. 现在,这是我试图用来获取项目的查询方法。 I get an exception when I try to call this method. 尝试调用此方法时出现异常。

public List<Object> getAllStudentsWithClass(){
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction transaction = session.beginTransaction();
    String select = "SELECT student, classroom FROM Student student, Classroom classroom WHERE student.classroom_id = classroomm.classroom_id";
    Query query = session.createQuery(select);
    List<Object> list = query.list();
    return list;
}

Here is the exception I get. 这是我得到的例外。

Exception in thread "JavaFX Application Thread" org.hibernate.QueryException: could not resolve property: classroom_id of: model.Student [SELECT student, classroom FROM model.Student student, model.Classroom classroom WHERE student.classroom_id = classroomm.classroom_id]

Caused by: org.hibernate.QueryException: could not resolve property: classroom_id of: model.Student

I appreciate all answers and ideas, thanks. 我感谢所有答案和想法,谢谢。

I think you're getting burned by your property names. 我认为您的财产名称使您感到厌倦。 JavaBeans specifies bean property names like id where the setter/getter are public void setId(long id) and public long getId(). JavaBeans指定像id这样的bean属性名称,其中的setter / getter是public void setId(long id)和public long getId()。

Your current property names are of the form x_id. 您当前的属性名称的格式为x_id。 I would recommend not confusing the property name with the column name. 我建议不要将属性名称与列名称混淆。 The property name can be id and the column name in the backing table can be class_id for the Class (poorly named as it conflicts with java.lang.Class). 属性名称可以是id,并且支持表中的列名称可以是Class的class_id(名称不佳,因为它与java.lang.Class冲突)。

ADDITIONAL EDIT 附加编辑

@Entity
public class Classroom {

    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleStringProperty year = new SimpleStringProperty();

    // Follow JavaBeans naming conventions
    private int classroomId;

    public Classroom() {
    }

    public Classroom(String name, String year) {
        this.name.set(name);
        this.year.set(year);
    }


    @Column(nullable = false)
    public String getName() {
        return name.get();
    }

    public SimpleStringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }

    @Column(nullable = false)
    public String getYear() {
        return year.get();
    }

    public SimpleStringProperty yearProperty() {
        return year;
    }

    public void setYear(String year) {
        this.year.set(year);
    }


    @Id
    @GenericGenerator(name="id" , strategy="increment")
    @GeneratedValue(generator="id")
    public int getClassroomId() {
        return classroomId;
    }

    public void setClassroomId(int classroomId) {
        this.classroomId = classroomId;
    }
}

In fact there is no field named classroomId on the Student Entity. 实际上,学生实体上没有名为classroomId编号的字段。 How should hibernate know about? 冬眠应该怎么知道? Is it really a column in your database? 它真的是您数据库中的一列吗? If yes, how did it get there? 如果是,它是如何到达那里的?

And is it correct that the type of Student.studentClass is just Class? 并且Student.studentClass的类型只是Class是否正确? Is it a custom type or does it refer to java.lang.Class? 它是自定义类型还是引用java.lang.Class? It should instead point to your Classroom type, shouldn't it? 它应该指向您的教室类型,不是吗? If that is the case, @JoinColumn(name = "class_id") should be @JoinColumn(name = "classroom_id") instead. 在这种情况下,@ JoinColumn(name =“ class_id”)应该改为@JoinColumn(name =“ classroom_id”)。 But then the join column annotation is not needed at all because hibernate knows how to join. 但是然后根本不需要联接列注释,因为休眠知道如何联接。

Then you could optimize your query to something like: 然后,您可以优化查询,例如:

SELECT ... FROM Student s JOIN s.studentClass c WHERE ...

在HQL中,您使用实体而不是表-因此,由于学生包含教室,因此hql查询应为:

Select student from Student student Join Fetch student.classroom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM