与 Criteria 无关的实体的左外连接

发布于 2025-01-11 12:02:07 字数 846 浏览 0 评论 0原文

如果该实体未映射,是否可以创建一个对另一个实体执行外连接的条件查询?

我知道当您进行交叉连接并手动添加连接条件时,内部连接是可能的。它看起来像这样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪荡不羁 2025-01-18 12:02:07

如果不建立相关实体,你就不能使用 criteria api 来做到这一点,我也遇到了和你一样的问题。交叉连接也无济于事。我可以建议的是:

  1. 在数据库上创建一个具有左外连接的视图,然后将视图映射为实体
  2. 进行 jpql 查询
  3. 进行本机查询

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:

  1. make a view with the left outer join on the database and then map the view as an entity
  2. make a jpql query
  3. make a native query
习惯成性 2025-01-18 12:02:07

我建议您更改类,以便建立逻辑上已经存在的关系。

class Car {
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "color_uuid", referencedColumnName = "uuid")
    private Color color;
}

class Color {
    private String uuid;
    private String name;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "color")
    private List<Car> cars;
}

然后您可以使用条件构建左连接:

CriteriaQuery<Car> criteriaQuery = criteriaBuilder.createQuery(Car.class);
Root<Car> root = criteriaQuery.from(Car.class);
root.join("color", JoinType.LEFT);
List<Car> cars = em.createQuery(criteriaQuery).getResultList();

I suggest you change the classes in order to have a relationship that logically already exists.

class Car {
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "color_uuid", referencedColumnName = "uuid")
    private Color color;
}

class Color {
    private String uuid;
    private String name;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "color")
    private List<Car> cars;
}

Then you can build the left join using criteria:

CriteriaQuery<Car> criteriaQuery = criteriaBuilder.createQuery(Car.class);
Root<Car> root = criteriaQuery.from(Car.class);
root.join("color", JoinType.LEFT);
List<Car> cars = em.createQuery(criteriaQuery).getResultList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文