Re: Derived attribute
Andrus Adamchik <[email protected]> Fri, 26 Sep 2025 12:16:57 -0400
| Newsgroups | gmane.comp.java.cayenne.user |
|---|---|
| Message-ID | <[email protected]> |
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 =3D FunctionExpressionFactory.absExp("delta");
3. An ordering:
Ordering o =3D new Ordering(e);
Thanks,
Andrus
> On Sep 26, 2025, at 10:48=E2=80=AFAM, Ricardo Parada =
<[email protected]> wrote:
>=20
>=20
> Hello,
>=20
> I=E2=80=99m looking into what we should do with derived attributes we =
have defined in our EOF object models after migrating to Cayenne.=20
>=20
> I would like to be able to use the derived attribute in queries in the =
where clause and order by clause.=20
>=20
> 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.=20
>=20
> ChatGPT suggested using defaultValueExpression as one of the options =
but cautioned it would not work with 5.0-M1. Something like this:
>=20
> <db-entity name=3D"MY_ENTITY" schema=3D"public">
> <db-attribute name=3D"ID" type=3D"INTEGER" isPrimaryKey=3D"true" =
isMandatory=3D"true"/>
> <db-attribute name=3D"DELTA" type=3D"DECIMAL"/>
> <db-attribute name=3D"ABS_DELTA" type=3D"DECIMAL" =
isGenerated=3D"true">
> <defaultValueExpression>ABS(DELTA)</defaultValueExpression>
> </db-attribute>
> </db-entity>
>=20
> <obj-entity name=3D"MyEntity" className=3D"com.example.model.MyEntity" =
dbEntityName=3D"MY_ENTITY">
> <obj-attribute name=3D"id" type=3D"java.lang.Integer" =
db-attribute-path=3D"ID"/>
> <obj-attribute name=3D"delta" type=3D"java.math.BigDecimal" =
db-attribute-path=3D"DELTA"/>
> <obj-attribute name=3D"absDelta" type=3D"java.math.BigDecimal" =
db-attribute-path=3D"ABS_DELTA"/>
> </obj-entity>
>=20
> It also mentioned using something like this:
>=20
>=20
> 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=E2=80=99m thinking we would use the latest version which will be 5.0 =
by that time. =20
>=20
> 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()).=20
>=20
>=20
>=20
> Thank you
>=20
> Ricardo Parada