Reputation: 7242
When performing a hibernate query in HQL using a join and subsequently calling query.list
to return the list of matched objects, I am ending up with a list of actual Object instances (i.e. query.list().get(0).getClass() == Object.getClass()
) instead of instances of the expected object.
Running the query without the join returns objects of the expected type correctly and they can be cast and used appropriately.
So far my searches have not turned up anything about what could be causing this. Is there something else I need to do when using a join in hql to ensure the object is mapped correctly?
Edit: Added code excerpts below. I had to change all the names and attempt to extract only the relevant portions (the real code is not really about cars).
Working query:
from Car car where car.name like :name
Non-working query:
from Car car left join car.occupants occupant where car.name like :name OR (occupant.name like :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 and 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 ...
}
Upvotes: 2
Views: 5472
Reputation: 7242
+1's all around to the other answerers for your help, it is much appreciated.
However, the solution turned out to be very simple. The query just needed to have the select specified at the beginning for the join case in order to narrow the field (I assume).
So the non-working query:
from Car car left join car.occupants occupant where car.name like :name OR (occupant.name like :oname)
Becomes:
select car from Car car left join car.occupants occupant where car.name like :name OR (occupant.name like :oname)
Upvotes: 2
Reputation: 41
Why are you using explicit left join btw.
from Car car left join car.occupants occupant where car.name like :name OR (occupant.name like :oname)
I think we can simple use:
from Car car where car.name like :name or car.occupant.name like :oname
Then the query.list should give you List of object which should be casted back to List of car
Upvotes: 2
Reputation: 280168
Your JOIN
in the HQL is making Hibernate retrieve two types of objects. You can see this if you activate the SQL logging.
Assuming you have a X-to-X relation in your entity, the problem should go away if you change the query to use
... JOIN FETCH entity.relation ...
Upvotes: 3