Re: Derived attribute

Ricardo Parada <[email protected]> Tue, 30 Sep 2025 17:42:46 -0400
Newsgroups gmane.comp.java.cayenne.user
Message-ID <[email protected]>
Thank you Andrus,

I had figured it out.  I=E2=80=99m finding the API easier to work with =
than EOF.

I like that these expressions can then be used with =
ObjectSelect.columnQuery() etc. and I don=E2=80=99t have to attach them =
to the entities like we did with EOF using derived attributes.

Regards,
Ricardo



> On Sep 29, 2025, at 2:43=E2=80=AFPM, Andrus Adamchik =
<[email protected]> wrote:
>=20
> One way is to use ExpressionFactory to further build the expression, =
but it is kinda ugly. Let me rather give a very compact property-based =
example:
>=20
>   Expression e =3D MyEntity.DELTA.abs().gt(10)
>=20
> Should've thought of that before :)
>=20
> Andrus
>=20
>=20
>> On Sep 26, 2025, at 12:54=E2=80=AFPM, Ricardo Parada =
<[email protected]> wrote:
>>=20
>> Thank you Andrus,
>>=20
>> How would I do a query to fetch all where absDelta > 10 for example?
>>=20
>>=20
>>=20
>>> On Sep 26, 2025, at 12:16=E2=80=AFPM, Andrus Adamchik =
<[email protected]> wrote:
>>>=20
>>> Hi Ricardo,
>>>=20
>>> "defaultValueExpression" is a hallucination. There's nothing like =
that in Cayenne :) Taking it out of the model and into Java will work:
>>>=20
>>> 1. A getter (like you mentioned) :
>>>=20
>>> public BigDecimal getAbsDelta() {
>>>    return getDelta().abs();
>>> }
>>>=20
>>> 2. An expression for WHERE:
>>>=20
>>> Expression e =3D FunctionExpressionFactory.absExp("delta");
>>>=20
>>> 3. An ordering:
>>>=20
>>> Ordering o =3D new Ordering(e);
>>>=20
>>> Thanks,
>>> Andrus
>>>=20
>>>=20
>>>=20
>>>> 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
>>>=20
>>=20
>=20