java - Hibernate Query.list returns actual Object instance instead of expected type -


when performing hibernate query in hql using join , subsequently calling query.list return list of matched objects, ending list of actual object instances (i.e. query.list().get(0).getclass() == object.getclass()) instead of instances of expected object.

running query without join returns objects of expected type correctly , can cast , used appropriately.

so far searches have not turned causing this. there else need when using join in hql ensure object mapped correctly?

edit: added code excerpts below. had change names , attempt extract relevant portions (the real code not cars).

working query:

from car car car.name :name 

non-working query:

from car car left join car.occupants occupant car.name :name or (occupant.name :oname) 

car entity:

@entity @table(uniqueconstraints = {@uniqueconstraint(columnnames = {"someid"}),                              @uniqueconstraint(columnnames = {"someotherid"})}) public class car extends someparententity {     @id     @generatedvalue(strategy = generationtype.identity)     private long id;      @column(nullable = false, length = 64)     private string someid;      @column(length = 64)     private string name;      // ... many columns , mappings removed ...      @onetomany(mappedby = "car", fetch = fetchtype.lazy)     private list<occupant> occupants;      // ... } 

occupant entity:

@entity(name = "car.occupant") @table(uniqueconstraints = {@uniqueconstraint(columnnames = { "name" }) }) public class user extends someparententity {      @manytoone     @joincolumn(name = "carid", nullable = false)     private car car;      @column(length = 64, nullable = false)     private string name;          // ... many mappings / columns removed ... } 

your join in hql making hibernate retrieve 2 types of objects. can see if activate sql logging.

assuming have x-to-x relation in entity, problem should go away if change query use

... join fetch entity.relation ... 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -