本文介绍了hibernate - 迭代查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个与外键关联的测试表


  • School (在 student_id

  • 学生(带pk on id

  • FK: school.student_id - > students.id



如何使用此查询获取学生姓名?

  Session session = DaoSF.getSessionFactory()。openSession(); 
String query =;
查询条件;

query =from School s+
inner join s.student st+
where s.student_id = st.id;
criteria = session.createQuery(query);

当我尝试迭代数据时:

<$ (Iterator<?> iter = criteria.iterate(); iter.hasNext();)
{
Object item =(Object)iter.next ();
System.out.println(((School)item).getStudent()。getId());
}

它报告:

 线程main中的异常java.lang.ClassCastException:[Ljava.lang.Object;不能转换为table.School 

但映射工作正常,因为当我更改最后一行时to



System.out.println(item.toString());



我得到了所有的数据,但是像这样:

  [Ljava.lang.Object; @ 1d2300e 
[Ljava.lang.Object; @ 51207c
[Ljava.lang.Object; @ 2bd615
...

其次,你重申了学校和学生之间的连接条件。你已经在映射中指定了,所以当你在查询中说join s.student时,Hibernate知道。因此,这里不需要where子句。



您认为学校/学生关系中的数据模型似乎有些倒退。一般来说,一所学校有多名学生(一名学生是与学校有特殊关系的人)。但是,根据你描述的模型...



你还没有明确说明你想从这个查询中得到什么,但是你原来的查询正在返回所有的学生。不知道为什么你甚至要驱动来自School对象的查询。

  Session session = ...; 
列表< Student> results =(List< Students>)session.createQuery(select s from Students s).list();
for(Student student:results){
//处理每个学生
}

如果你只想要学生的名字(你认为这是/种类):

 会话会话= ...; 
列表< String> results =(List< String>)session.createQuery(从学生s选择s.name).list();
for(String student:results){
//处理每个学生的姓名
}

请注意,在上述查询中,select子句实际上是可选的,因为from子句中只引用了一个实体。我更喜欢总是指定一个select子句来明确。


I have two testing tables associated by foreign key

  • table School (with fk on student_id)
  • table Students (with pk on id)
  • FK: school.student_id -> students.id

How can I get students name using this query?

Session session = DaoSF.getSessionFactory().openSession();
String query = "";
Query criteria;

query = "from School s " +
        "inner join s.student st " +
        "where s.student_id = st.id";
criteria = session.createQuery(query);

When I try to iterate data:

for(Iterator<?> iter = criteria.iterate(); iter.hasNext();)
{
  Object item = (Object)iter.next();
  System.out.println(((School) item).getStudent().getId());
}

it reports:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to table.School

but mapping is working, because when i've changed the last row to

System.out.println(item.toString());

I've got all the data but like that:

[Ljava.lang.Object;@1d2300e
[Ljava.lang.Object;@51207c
[Ljava.lang.Object;@2bd615
...
解决方案

In HQL, whenever you join without an explicit select clause Hibernate returns all the joined entities. Its not the most intuitive thing, buts it is legacy behavior at this point.

Secondly you are re-stating the join condition between school and student. You already specified that in the mapping, so Hibernate knows that when you say "join s.student" in the query. So the where clause is unnecessary here.

You data model seems a bit backwards I think from what most think of School/Student relationship. Generally a school has multiple students (a student being a person in a particular relationship with the school). But given the model as you describe...

You have not stated exactly what you want out of this query, but your original query is returning all students. No idea why you even "drive" the query from the School object.

Session session = ...;
List<Student> results = (List<Students>) session.createQuery( "select s from Students s" ).list();
for ( Student student : results ) {
    // handle each student
}

If you want just the names of the students (which you sort-of/kind-of imply):

Session session = ...;
List<String> results = (List<String>) session.createQuery( "select s.name from Students s" ).list();
for ( String student : results ) {
    // handle each student name
}

Note that the select clause is really optional in my above queries because there is only one entity referenced in the from clause. I prefer to always specify a select clause to be explicit.

这篇关于hibernate - 迭代查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 20:06