1

I'm coming back to Java after a few years away and this is my 2nd day looking at hibernate and don't fully understand it yet.

I have the following criteria which is performing a join:

Criteria cr = s.createCriteria(Bla.class, "bla");
cr.setFetchMode("bla.nodePair", FetchMode.JOIN);
cr.createAlias("bla.nodePair", "node_pair");
cr.add(Restrictions.in("bla.blaName", (List<Bla>) getBlas()));

ProjectionList columns = Projections.projectionList()
    .add(Projections.property("node_pair.priNode"))
    .add(Projections.property("bla.blaName"))
    .add(Projections.property("node_pair.secNode"))
    .add(Projections.property("bla.port"));

cr.setProjection(columns);
List<Object[]> list = cr.list(); // Exception occurs here

This is creating as far as I can tell a valid SQL query and exactly what i'm after.

However, when I try to generate a list of results cr.list(); I get:

java.lang.ClassCastException: com.some.package.domainobject.Bla cannot be cast to java.lang.String

How should I construct my List. Any pointers much appreciated.

1
  • Your example seems like it's entirely something you made up for practice. Is the thing in the database actually of type class Bla? Did you put it there? You could be trying to read a different object out of the database and that's tripping you up. Commented Mar 13, 2015 at 15:33

1 Answer 1

2

In Criteria API, the Exception is always(?) thrown when attempting to get the results.

In your case, the problematic line is probably

cr.add(Restrictions.in("bla.blaName", (List<Bla>) getBlas()));

You seem to check if a java.lang.String is part of a Collection<Bla>.

If your equals and hashcode methods in Bla use the blaName field, you should be able to use

cr.add(Restrictions.in("bla", (List<Bla>) getBlas()));

Otherwise, implements a getBlasNames() function which returns a List<String>

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

4 Comments

If I comment out List<Object[]> list = cr.list(); then no exception is thrown so I don't believe cr.add(Restrictions.in("bla.blaName", (List<Bla>) getBlas())); is the issue :/
No SQL is genereated before you attempt to get the result list. That is what happens when you call cr.list();.
You are quite right. Not sure what you are talking about regarding equals and hashcode methods? Can you give an example ?
I just meant that if you consider your Bla entities equals if their blaName field is, then your equals and hashcode methods should use that field to compute their result. In this case, you should be able to use Restrictions.in("bla", (List<Bla>) getBlas()). usually, entities have an id field that is used to check equality. In this latter case, you should build a List<String> in a getBlasNames() method

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.