Hi Ricardo,
"defaultValueExpression" is a hallucination. There's nothing like that in
Cayenne :) Taking it out of the model and into Java will work:
1. A getter (like you mentioned) :
public BigDecimal getAbsDelta() {
return getDelta().abs();
}
2. An expression for WHERE:
Expression e = FunctionExpressionFactory.absExp("delta");
3. An ordering:
Ordering o = new Ordering(e);
Thanks,
Andrus
> On Sep 26, 2025, at 10:48 AM, Ricardo Parada <[email protected]> wrote:
>
>
> Hello,
>
> I’m looking into what we should do with derived attributes we have defined in
> our EOF object models after migrating to Cayenne.
>
> I would like to be able to use the derived attribute in queries in the where
> clause and order by clause.
>
> For example, if my entity has a DELTA column and a derived column ABS_DELTA
> that uses ABS(DELTA) expression when fetching it or when used in the where
> clause or order by clause.
>
> ChatGPT suggested using defaultValueExpression as one of the options but
> cautioned it would not work with 5.0-M1. Something like this:
>
> <db-entity name="MY_ENTITY" schema="public">
> <db-attribute name="ID" type="INTEGER" isPrimaryKey="true"
> isMandatory="true"/>
> <db-attribute name="DELTA" type="DECIMAL"/>
> <db-attribute name="ABS_DELTA" type="DECIMAL" isGenerated="true">
> <defaultValueExpression>ABS(DELTA)</defaultValueExpression>
> </db-attribute>
> </db-entity>
>
> <obj-entity name="MyEntity" className="com.example.model.MyEntity"
> dbEntityName="MY_ENTITY">
> <obj-attribute name="id" type="java.lang.Integer" db-attribute-path="ID"/>
> <obj-attribute name="delta" type="java.math.BigDecimal"
> db-attribute-path="DELTA"/>
> <obj-attribute name="absDelta" type="java.math.BigDecimal"
> db-attribute-path="ABS_DELTA"/>
> </obj-entity>
>
> It also mentioned using something like this:
>
>
> Property.create("absDelta", BigDecimal.class).alias("ABS(DELTA)").
> Does anybody have any suggestions or recommendations? I would prefer
> something that works with 5.0-M1 since by the time we do this migration I’m
> thinking we would use the latest version which will be 5.0 by that time.
>
> I think that something that allows me to use the derived columns in the where
> and order by clause. In most cases it would be okay to additionally write the
> logic in Java so that in-memory sorting / filtering works. For example, a
> getAbsDelta() that returns Math.abs(getDelta()).
>
>
>
> Thank you
>
> Ricardo Parada