使用HQL从数据库获取数据

时间:2013-04-15 19:55:13

标签: java hibernate hql

嗨, 我有3个表的数据库工作人员:

mysql> describe person;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| person_id  | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| date       | datetime     | YES  |     | NULL    |                |
| first_name | varchar(255) | YES  |     | NULL    |                |
| last_name  | varchar(255) | YES  |     | NULL    |                |
| position   | varchar(255) | YES  |     | NULL    |                |
| salary     | double       | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

mysql> describe department;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| department_id | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| dept_name     | varchar(255) | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+

mysql> describe department_person;
+---------------+------------+------+-----+---------+-------+
| Field         | Type       | Null | Key | Default | Extra |
+---------------+------------+------+-----+---------+-------+
| department_id | bigint(20) | NO   | PRI | NULL    |       |
| person_id     | bigint(20) | NO   | PRI | NULL    |       |
+---------------+------------+------+-----+---------+-------+

所以,我想编写HQL语句,使用person.lastName从person.position获取数据,我使用以下代码来提取这些数据,但没有运气:

public List<Person> findPosition(){
        Session session =
                HiberUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Person> result = session.createQuery("select position from Person p where p.lastName= 'Anderson'").list();
        for(Person a : result) {
            Hibernate.initialize(a.getDepartmentList());
        }
        session.getTransaction().commit();
        return result;

    }

在这是它向我展示了这个例外:

Hibernate: select person0_.position as col_0_0_ from person person0_ where person0_.last_name='Anderson'
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to main.Person
    at main.StaffDAO.findPosition(StaffDAO.java:100)
    at main.Main.main(Main.java:37)

你能帮我编写正确的HQL语句吗?

3 个答案:

答案 0 :(得分:1)

List<Person> result = session.createQuery("select position from Person p where p.lastName= 'Anderson'").list();

您正在选择一个包含VARCHAR(255)的属性,但希望它返回为List<Person>。这根本不算。

你可以做到

List<Person> result = session.createQuery("select p from Person p where p.lastName= 'Anderson'").list();

答案 1 :(得分:1)

好吧,您在查询中选择position,并尝试将其放入Person列表中。相反,你应该select p from Person p where p.lastName= 'Anderson'

答案 2 :(得分:0)

从Person p中选择Person,其中p.lastName ='Anderson'