RomZes
RomZes

Reputation: 127

Hibernate doesn't return proper objects in inner join

I'm working on Spring MVC application that uses Hibernate. One of my requests in SQL looks like this:

SELECT work_order.* FROM work_order
inner join user
on
work_order.user_id = user.id
and
user.user_name = 'Jenna'

and I do get a result as a row of workorders. When I'm trying to do the same with Hibernate I get five objects(which is correct) but can't convert into WorkoOrder objects. Here's my Hibernate request:

List<WorkOrder> workorders =  (List<WorkOrder>)currentSession.createQuery(
"from WorkOrder w inner join w.user as u where user_name=:tempName")
.setParameter("tempName", tempName).getResultList();

Where tempName is a parameter. I do get objects, but can't cast them to Workorder, probably because Hibernate returns Workorders and Users combined. How to fix this so only Workorders will be returned? Update: User is mapped in WorkOrder

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="user_id", nullable = true)
private User user;

Upvotes: 0

Views: 769

Answers (1)

John Bollinger
John Bollinger

Reputation: 181114

Your HQL does not have a select clause. If you want only the WorkOrder entities from the join you form, then you'll need to tell Hibernate so:

currentSession.createQuery(
        "select w from WorkOrder w inner join w.user as u where u.user_name=:tempName")

Upvotes: 2

Related Questions