Getting data from database using HQL

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 22:54:58

问题


Hi, I have database Staff with 3 tables:

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    |       |
+---------------+------------+------+-----+---------+-------+

So, I want to compose HQL statement for getting data from person.position using person.lastName and I've used the following code to exctract this data but without luck:

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;

    }

In this was it shows me this exception:

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)

Can you please help me in composing correct HQL statement?


回答1:


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

You are selecting an attribute that holds a VARCHAR(255) but expect it returned as a List<Person>. That simply does not compute.

You could do

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



回答2:


Well, you're selecting a position in your query, and trying to put that in a list of Person. Instead you should select p from Person p where p.lastName= 'Anderson'.




回答3:


select Person from Person p where p.lastName= 'Anderson'



来源:https://stackoverflow.com/questions/16023703/getting-data-from-database-using-hql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!