2017-08-10 40 views
2

我有以下实体单表继承策略。如何在使用单表继承时在Ebean查询中使用判别器值?

@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; 
} 

我在我的用户界面中有一张表,应该显示具有Vehicle字段列的行。我想要进行高级搜索,以便用户可以按类型过滤车辆列表。

我的问题是我如何创建一个查询按照Vehicle的类型过滤列表。事情是这样的:

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

如果我插入与已经讨论here下面定义的字段,我会得到一个错误与消息“错误注射构造,java.lang.IllegalStateException:房产theType没有找到[ ID,名称,描述]类型类models.Vehicle”

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

如果我将其设置为@Transient,我不能在查询中使用它。

回答

1

您可以使用@Formula注释,因为它已在ebean docs中解释过。这

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

通知:

由于现场还瞬态它不是默认包含在查询中 - 它需要被明确列入。