gpt4 book ai didi

java - 使用单表继承时如何在Ebean查询中使用鉴别器值?

转载 作者:行者123 更新时间:2023-11-30 10:26:37 25 4
gpt4 key购买 nike

我有以下具有单表继承策略的实体。

@Entity
@Inheritance
@DiscriminatorColumn(name="type")
public abstract class Vehicle extends com.avaje.ebean.Model {
@Id
public Long id;
public String name;
public String description;
}

@Entity
@DiscriminatorValue("b")
public class Bus extends Vehicle {
public String field1;
}

@Entity
@DiscriminatorValue("c")
public class Car extends Vehicle {
public String field2;
}

我的 UI 中有一个表,该表应显示包含车辆字段列的行。我想进行高级搜索,用户可以按类型过滤车辆列表。

我的问题是如何创建查询以按车辆类型过滤列表。像这样:

List<Vehicle> list = com.avaje.ebean.Ebean.find(Vehicle.class)
.where().eq("type", "c").findList();

如果我插入一个已经讨论过的具有以下定义的字段 here ,我会收到一条错误消息“注入(inject)构造函数时出错,java.lang.IllegalStateException:在类型类模型的 [id,名称,描述] 中找不到属性 theType。车辆”

@Column(name = "type", insertable = false, updatable = false)
public String theType;

如果我将它设置为@Transient,我将无法在查询中使用它。

最佳答案

您可以使用@Formula 注释,如ebean docs 中所述。 .

@Transient
@Formula(select = "type")
public String theType;

当然要注意:

As the field is also Transient it is not included by default in queries - it needs to be explicitly included.

关于java - 使用单表继承时如何在Ebean查询中使用鉴别器值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45613257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com