Derived attribute

Ricardo Parada <[email protected]> Fri, 26 Sep 2025 10:48:55 -0400
Newsgroups gmane.comp.java.cayenne.user
Message-ID <[email protected]>
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