HQL查询问题

时间:2016-12-29 16:40:01

标签: java database hibernate hql

尝试使用HQL从我的数据库中检索项目时遇到问题。我有2个桌子(学生和课堂),我想要将所有学生与他们所属的课堂一起检索。

学生表

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

课堂表

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

这些表是Java类,以防有人想看到它们。

学生

@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;
    }
}

教室

@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;
    }

现在,这是我尝试用来获取项目的查询方法。我尝试调用此方法时遇到异常。

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;
}

这是我得到的例外。

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

感谢所有的答案和想法,谢谢。

3 个答案:

答案 0 :(得分:0)

我认为你被你的财产名称灼伤了。 JavaBeans指定bean属性名称,如id,其中setter / getter是public void setId(long id)和public long getId()。

您当前的属性名称格式为x_id。我建议不要将属性名称与列名混淆。属性名称可以是id,并且支持表中的列名称可以是Class的class_id(由于它与java.lang.Class冲突,名称很差)。

其他编辑

@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;
    }
}

答案 1 :(得分:0)

实际上学生实体上没有名为classroomId的字段。 hibernate应该怎么知道?它真的是数据库中的一列吗?如果是的话,它是如何实现的?

Student.studentClass的类型只是Class是正确的吗?它是自定义类型还是引用java.lang.Class?它应该指向你的课堂类型,不应该吗?如果是这种情况,则@JoinColumn(name =“class_id”)应该是@JoinColumn(name =“classroom_id”)。但是根本不需要连接列注释,因为hibernate知道如何加入。

然后,您可以将查询优化为:

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

答案 2 :(得分:0)

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

Select student from Student student Join Fetch student.classroom