0

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
...
2
  • Please show which package does Query criteria uses? Commented Jun 12, 2012 at 12:22
  • What do you mean? This? import org.hibernate.Query; Commented Jun 12, 2012 at 12:26

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Btw, it was a testing example, I did't thought much about school and students relationship. It was only for test, so ofcourse you're right, student have to point to the school where he's study :-)
No worries, I understand. But over the years I've learned to look at the db mapping rather than rely on "usual conceptions" ;) Just in future it usually makes getting an answer easier.
0

You are iterator is of type Object. Try typing it with School.

for(Iterator<School> iter=criteria.iterate();iter.hasNext();){...}

Since that did not seem to work try the following.

String sql="...";
Query query=session.createQuery(sql);
List<School> schools=(List<School>)query.list();
    for(School school:schools)
        System.out.println(school.getStudent().getId());

14 Comments

Thanks, but I've tried it. It doesn't work... same error message
Did you change the type of item to School as well?
Jj, I've tried all the combinations and It can be iterated only over the Object..
As a side note, have you considered using the EntityManager and EntityManagerFactory opposed to working with the session since these are part of JPA and not Hibernate Specific (not sure if this is an option)? This would allow you to write your code a little more cleanly as well. You could write something like em.createQuery(sql,School.class).getResultList();
Thank you, but it's completely the same. It works as Object type, not works as school type :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.