Java – hibernate subquery

I have a problem creating subqueries using hibernate Unfortunately, the subqueries class has almost no documentation at all, so I have no idea how to convert the following SQL to hibernate criteria:

SELECT id
FROM car_parts
WHERE car_id IN ( SELECT id FROM cars WHERE owner_id = 123 )

I want the following to "work":

session.createCriteria(CarParts.class).add(eq("car.owner",myCarOwner));

Unfortunately, it didn't So it seems that I actually have to use the subqueries class to create criteria But I can't find a reasonable example, but it leads me to ask it here

Solution

Try to create an alias for the "car" attribute before adding the following EQ expression:

session.createCriteria(CarParts.class)  
        .createAlias("car","c")  
        .add(eq("c.owner",myCarOwner));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>