与 Criteria 无关的实体的左外连接
如果该实体未映射,是否可以创建一个对另一个实体执行外连接的条件查询?
我知道当您进行交叉连接并手动添加连接条件时,内部连接是可能的。它看起来像这样:
CriteriaBuilder cb = getCriteriaBuilder();
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<Car> car = cq.from(Car.class);
Root<Color> color = cq.from(Ccolor.class);
cq.where(cb.equal(car.get("colorUuid"), color.get("uuid")));
但是,在我的情况下,我需要外部联接的行为。
假设我有这些实体:
class Car {
@Column(name="color_uuid")
private String colorUuid;
}
class Color {
private String uuid;
private String name;
}
假设颜色是可选的,这就是我需要外部连接的原因。 SQL 看起来像
SELECT * from car LEFT OUTER JOIN color ON car.color_uuid = color.uuid;
Can I do this with Criteria?
Is it somehow possible to create a criteria query that performs an outer join on another entity if that entity is not mapped?
I know that an inner join is possible when you do a cross join and add the join condition manually. It would look like this:
CriteriaBuilder cb = getCriteriaBuilder();
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<Car> car = cq.from(Car.class);
Root<Color> color = cq.from(Ccolor.class);
cq.where(cb.equal(car.get("colorUuid"), color.get("uuid")));
However I need the behaviour of an outer join in my case.
So let's say I have these entities:
class Car {
@Column(name="color_uuid")
private String colorUuid;
}
class Color {
private String uuid;
private String name;
}
Lets say Color is optional and that's why I need an outer join. The SQL would look like
SELECT * from car LEFT OUTER JOIN color ON car.color_uuid = color.uuid;
Can I do this with Criteria?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果不建立相关实体,你就不能使用 criteria api 来做到这一点,我也遇到了和你一样的问题。交叉连接也无济于事。我可以建议的是:
You can’t do this with criteria api without making the entities in relation, I’ve faced the same problem as you. Also a cross join can’t help. What I can suggest is:
我建议您更改类,以便建立逻辑上已经存在的关系。
然后您可以使用条件构建左连接:
I suggest you change the classes in order to have a relationship that logically already exists.
Then you can build the left join using criteria: