Jpql extract year, month and ALIAS

I have a ValueCollectionDatasource defined from the screen as follows that works smoothly.

select e.productionStage, e.operation, e.resource, e.plant, e.quantity, e.planOrActual, e.material, e.capacityOrProduction, e.capacityHours, EXTRACT(YEAR FROM e.prodDate) from erp$ProductionPlanContinuousProdPivot e

now I need to extract MONTH as well as use the date fields in my pivotTable.

When I use ALIAS EXTRACT(MONTH FROM e.prodDate) AS month, EXTRACT(YEAR FROM e.prodDate) as year it doesn’t work. Seems like not supported. Thanks for any help.

I think it is supported. It is listed as a supported function in the documentation:
https://doc.cuba-platform.com/manual-6.10/jpql_functions.html

Also, I was able to test it out by doing the following:

  1. Navigate to Administration | JMX Console
  2. Search for “persistence”.
  3. Open app-core.cuba
  4. Scroll to jpqlLoadList()
  5. Invoke: select extract(month from e.createTs) as test from sec$User e

Maybe provide your error?

Hi Keith
Here is the error:

JpqlSyntaxException: Errors found for input jpql:[select e.productionStage, e.operation, e.resource, e.plant, e.quantity, e.planOrActual, e.material, e.capacityOrProduction, e.capacityHours, EXTRACT(YEAR FROM e.prodDate) as year, EXTRACT(MONTH FROM e.prodDate) as month from erp$ProductionPlanContinuousProdPivot e] CommonErrorNode [<mismatched token: [@77,192:195='year',<144>,1:192], resync=year>] CommonErrorNode [<mismatched token: [@92,232:236='month',<116>,1:232], resync=month>]

Hmm. Both extract year and month work for me. Perhaps it is the alias. Is it really required? I usually don’t use them but rather extract from the result list using the order of the select.

Query query = entityManager.createQuery("select e.productionStage, e.operation, e.resource, e.plant, e.quantity, e.planOrActual, e.material, e.capacityOrProduction, e.capacityHours, EXTRACT(YEAR FROM e.prodDate), EXTRACT(MONTH FROM e.prodDate) from erp$ProductionPlanContinuousProdPivot e");
List results = query.getResultList();
for(List result : results) {
    year = result.get(9);
    month = result.get(10);
}

You may also consider:

Query query = entityManager.createQuery("select e from erp$ProductionPlanContinuousProdPivot e");
List pivots = query.getResultList();
for(ProductionPlanContinuousProdPivot pivot : pivots) {
    year = pivot.getProdDate()
    Calendar cal = Calendar.getInstance();
    cal.setTime(pivot.getProdDate());
    year = cal.get(Calendar.YEAR);
    month = cal.get(Calendar.MONTH);
}

Thanks. But as I am using this query in pivot Table, I don’t have that choice to loop! and as you know we have to define the properties with a name in pivot table where property name remains prodDate even Extracted to Year unless I use ALIAS!

Hy,

You can also create a SQL view and then create a persistent entity, which is based on it. This way, you can define whatever you want. It is most likely also more performant compared to value datasources…

Bye
Mario

Have you tried using something other than ‘year’ and ‘month’ for the alias names? They look like reserved words.

@mario
SQL View in JPA environment, Could you please give a little more hints?

Hi,

well - for the JPA layer it cannot see any difference between a SQL View and a SQL table. Therefore, it will just transparently work as a regular table. When it only comes to read operations, this is a safe bet.

So, what you can do is to create a view in SQL that defines some kind of aggregation or uses some database specific functions (like date operations and so on). But for the JPA layer it can just be seen as regular columns from a table.

This way, for a lot of cases, when you have problems with the ORM layer and it is only about reading → create a SQL view and map a regular JPA entity (persistent) on it. This way, all the complexity from the JPA layer goes away and is pushed into the datastore.

You can find some information about it in the forum: Результаты поиска по запросу «sql view» - CUBA.Platform

Studio even supports model generation for views, which means that you can basically just define the view, and generate a JPA model from it and be done. The UI can just use the entity as any other one…

Bye
Mario

Thank you so much Mario.