【问题标题】:Ebean select function always taking id column onlyEbean 选择功能始终只采用 id 列
【发布时间】:2019-08-18 17:06:37
【问题描述】:

在 Ebean 中,选择(字符串属性)函数始终采用表的 id。我知道选择函数采用 id 默认值,但在我的情况下,虽然我给出了不同的属性(列名),但它只采用 id

这是在日志中运行的查询

txn[1001] select t0.id c0, t0.id c1 from commission_rates t0; --bind()

播放版本 - 2.5.9

这是那个实体:

@Entity
@Table(name = "commission_rates")
public class CommissionRates extends Model {

    @Id
    @Column(name = "id")
    private Long id;

    @Enumerated(EnumType.ORDINAL)
    @Column(name = "commission_type", nullable = false)
    private CommissionType commissionType;

    @Column(name = "commission_value", nullable = false)
    private float commissionValue;

    @Column(name = "applicable")
    private int applicable;

    @Column(name = "from_date")
    @Temporal(TemporalType.DATE)
    private Date fromDate;

    @CreatedTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_time", columnDefinition = "datetime", updatable = false)
    private Date createdTime;
}

Ebean 查询:

CommissionRates a = Ebean.find(CommissionRates.class).select("commission_type").findUnique();

我想选择一个commission_type 列以及id。想办法让我出去。

【问题讨论】:

    标签: java sql playframework ebean


    【解决方案1】:

    找到的解决方案:

    在选择函数中,我们需要传递实体的变量名而不是列名 例如:

    CommissionRates a = Ebean.find(CommissionRates.class).select("commissionType").findUnique();
    

    不是:

    CommissionRates a = Ebean.find(CommissionRates.class).select("commission_type").findUnique();
    

    【讨论】:

      【解决方案2】:

      在使用 ebean Model.Finder 进行 DB 查询的情况下,使用 Model 类文件名,在 Ebean.find(ModelClass.class) 的情况下使用 DB 列名。

      注意:这不仅适用于选择,也适用于所有

      如果你想使用你的模型类文件名而不是数据库库名添加

      public static final Finder<Long, ClassName> find = new Finder<>(ClassName.class);
      

      你也可以ClassName.find.select("commissionType").findUnique();

      如果您想获取多个字段,请在 select 方法中使用 , 作为分隔符。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-20
        • 2019-03-15
        相关资源
        最近更新 更多