Hibernate @Formula 结果 org.hibernate.HibernateException: 缺少列

Hibernate @Formula results in org.hibernate.HibernateException: Missing column

我有以下带有计算字段的实体 isIdle:

@Entity
@Table(name = Customer.TABLE)
public class Customer {

    // ...

    @JsonIgnore
    private static final String IS_IDLE_SQL =
        "CASE WHEN (trunc(extract(epoch from now())) - coalesce(last_user_login_time,0) " +
        "> 5 * 24 * 3600 ) THEN 1 ELSE 0 END";

    @Formula(IS_IDLE_SQL)
    private Integer isIdle;

    public Integer getIsIdle() {
        return isIdle;
    }

    public void setIsIdle(Integer isIdle) {
        this.isIdle = isIdle;
    }
}

如果 last_user_login_time 包含最近 5 天的 UNIX 时间戳,该字段应为 0,否则为 1。

据计算,我的数据库中没有对应的列table。 当我部署我的应用程序时,出现以下错误:

org.hibernate.HibernateException: Missing column: isIdle in public.customer
    at org.hibernate.mapping.Table.validateColumns(Table.java:366)

为什么 Hibernate 会尝试为这个计算字段找到一个列? 数据库使用PostgreSQL。

我通过将 SQL 语句放在 @Formula 注释中解决了我的问题,如下所示:

@Formula("CASE WHEN (trunc(extract(epoch from now())) - coalesce(last_user_login_time,0) " +
    "> 5 * 24 * 3600 ) THEN 1 ELSE 0 END")
private Integer isIdle;