3

I've got following entity with calculated field 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;
    }
}

The field should be 0, if last_user_login_time contains UNIX timestamp which is in the last 5 days, and 1 otherwise.

As it is calculated, I have not corresponding column in my DB table. When I deploy my application, I get the following error:

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

Why does Hibernate try to find a column for this calculated field? The database uses PostgreSQL.

1 Answer 1

1

I solved my problem by putting SQL statement right inside @Formula annotation, like this:

@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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.