查询固有类型的 dsl 投影



我有三个类,如车辆作为基本类型,汽车和自行车是车辆类的固有。

问题是我想使用查询 dsl 获取所有车辆,并且车辆的条件是

  • 汽车对象应填充品牌和总门文件(不是任何其他)
  • 自行车对象应仅使用品牌字段(而不是任何其他字段)填充。

车辆类别

@Entity
@Table(name="vehicles")
@Inheritance(strategy=InheritanceType.JOINED)
public class Vehicle {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "id", length = 30)
  private Integer id;
  private String brand;  
  private String color;
  public String getBrand() {
    return brand;
  }
  public void setBrand(String brand) {
    this.brand = brand;
  }
  public String getColor() {
    return color;
  }
  public void setColor(String color) {
    this.color = color;
  }
}

汽车类

@Entity
@Table(name="cars")
public class Car extends Vehicle {
  private int totalDoor;
  private int numeberofSeats;
  public int getTotalDoor() {
    return totalDoor;
  }
  public void setTotalDoor(int totalDoor) {
    this.totalDoor = totalDoor;
  }
  public int getNumeberofSeats() {
    return numeberofSeats;
  }
  public void setNumeberofSeats(int numeberofSeats) {
    this.numeberofSeats = numeberofSeats;
  }
}

自行车类

@Entity
@Table(name="bikes")
public class Bike extends Vehicle {
  private int numerberOfTiers;
  public int getNumerberOfTiers() {
    return numerberOfTiers;
  }
  public void setNumerberOfTiers(int numerberOfTiers) {
    this.numerberOfTiers = numerberOfTiers;
  }
}

查询所有车辆,但在哪里指定汽车和自行车投影?

List<Vehicle> vehicles = new JPAQuery(manager)
    .from(vehicle)
    .distinct()
    .list(Projections.bean(Vehicle.class , vehicle.brand));

您可以在单独的查询中填充特定的子类型,但不能在一个查询中填充。因此,您可以针对子类型进行特定的单独查询,也可以对结果进行后期处理。

最新更新