Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment to computed value misinterpreted in JDQL UPDATE when entity identification variable is ommitted #2183

Closed
Riva-Tholoor-Philip opened this issue Jul 2, 2024 · 5 comments · Fixed by #2224

Comments

@Riva-Tholoor-Philip
Copy link
Contributor

Riva-Tholoor-Philip commented Jul 2, 2024

Describe the bug

When the optional entity identification variable is omitted (for consistency with JDQL) of a JPQL query such as the following,

UPDATE Coordinate SET x = :newX, y = y / :yDivisor WHERE id = :id

EcilpseLink complains that "The left expression is not an arithmetic expression" instead of interpreting the assignment operation correctly.

Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing [UPDATE Coordinate SET x = :newX, y = y / :yDivisor WHERE id = :id]. 
[37, 38] The left expression is not an arithmetic expression.
	at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1848)
	at io.openliberty.data.internal.persistence.RepositoryImpl.invoke(RepositoryImpl.java:1079)
	... 40 more
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 5.0.0-B02.v202404111748): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [UPDATE Coordinate SET x = :newX, y = y / :yDivisor WHERE id = :id]. 
[37, 38] The left expression is not an arithmetic expression.
	at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:169)
	at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:345)
	at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:292)
	at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:174)
	at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:144)
	at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:120)
	at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:107)
	at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:91)
	at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1846)
@ajaypaul-ibm
Copy link
Contributor

We were able to recreate this issue using a simple JPA application. Below is the Coordinate entity we created by referencing the entity present in Jakarta Data.

@Entity
public class Coordinate {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private double x;

    private float y;
}

Here is the code where we executed the JPQL query:

EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();

entityManager.createQuery("UPDATE Coordinate SET x = :newX, y = y / :yDivisor WHERE id = :id")
        .setParameter("newX", newX)
        .setParameter("yDivisor", yDivisor)
        .setParameter("id", id)
        .executeUpdate();

transaction.commit();

This resulted in the following exception stack, which is the same as described in the issue:

Exception in thread "main" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing [UPDATE Coordinate SET x = :newX, y = y / :yDivisor WHERE id = :id]. 
[37, 38] The left expression is not an arithmetic expression.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1751)
    at com.example.repositories.CoordinateRepository.updateCoordinate(CoordinateRepository.java:18)
    at org.example.Main.main(Main.java:34)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.7.9.v20210604-2c549e2208): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [UPDATE Coordinate SET x = :newX, y = y / :yDivisor WHERE id = :id]. 
[37, 38] The left expression is not an arithmetic expression.
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:157)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:336)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:280)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:165)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:118)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:104)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:88)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1749)
    ... 2 more

@ajaypaul-ibm
Copy link
Contributor

FYI , I'm currently looking into this issue.

@OndroMih
Copy link
Contributor

OndroMih commented Aug 3, 2024

I believe this case is already fixed in the master branch with #2218.

OndroMih added a commit to OmniFish-EE/eclipselink that referenced this issue Aug 3, 2024
The issue is already fixed. This is only a test that verifies the fix.
OndroMih added a commit to OmniFish-EE/eclipselink that referenced this issue Aug 3, 2024
The issue is already fixed. This is only a test that verifies the fix.
@Riva-Tholoor-Philip
Copy link
Contributor Author

I believe this case is already fixed in the master branch with #2218.

@OndroMih - so the PR #2218 will fix two issues 2183 and 2187

@OndroMih
Copy link
Contributor

OndroMih commented Aug 3, 2024

Yes. I fixed some issues with implicit alias too, because I hit them with a test query that I used to reproduce the main issue with “lenght”.

@lukasj lukasj linked a pull request Aug 5, 2024 that will close this issue
lukasj pushed a commit that referenced this issue Aug 5, 2024
* Fixes #2197, #2198, #2199 related to using "this" variable in JPQL

When the "this" variable is used explicitly, it's virtual but needs to be skipped
in expressions that expect a virtual variable.
The problem was that expressions like 'this.field' were treated as
if 'this' was a field on the implicit variable.

* Adds a test for #2183

The issue is already fixed. This is only a test that verifies the fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants