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

SELECT ID(entityvar) only returns part of IdClass #2211

Open
Riva-Tholoor-Philip opened this issue Jul 16, 2024 · 5 comments
Open

SELECT ID(entityvar) only returns part of IdClass #2211

Riva-Tholoor-Philip opened this issue Jul 16, 2024 · 5 comments

Comments

@Riva-Tholoor-Philip
Copy link
Contributor

The following JPQL,

SELECT ID(THIS) FROM City WHERE (name=?1) ORDER BY population DESC

is used on an entity,

@Entity
@IdClass(CityId.class)
public class City {
    @Id
    public String name;
    public int population;
    @Id
    public String stateName;
    ...
}

with an IdClass

public record CityId(String name, String state) implements Serializable

The result ought to be a list of CityId.
Instead, it returns a list of String where the values are the stateName entity attribute, which is only one of the two components of the ID.

It looks like the same error occurs with the ID() function regardless of whether the entity identification variable is THIS or something else. I tried with SELECT ID(o) FROM City o and saw the same result.

It should be noted that section 4.7.11 of the Jakarta Persistence 3.2 specification states that, "A persistence provider is not required to support the use of the ID function for entities with composite primary keys." That means EclipseLink would have the option of raising an error and declaring the ID() function to be unsupported when IdClass is used. However, that is not what is happening here either. It is clearly trying to support the function, but returning the wrong value--only part of the composite primary key rather than the IdClass value that is the entire primary key.

@Riva-Tholoor-Philip
Copy link
Contributor Author

I am looking into this issue

@lukasj
Copy link
Member

lukasj commented Aug 27, 2024

visit(IdExpression) should probably be updated; one may want to be checking also mapping.derivesId()

@lukasj
Copy link
Member

lukasj commented Aug 28, 2024

probably a problem on line 66 there

@Riva-Tholoor-Philip
Copy link
Contributor Author

Thanks Lukas. Will look into this

@rfelcman
Copy link
Contributor

A few hints about this issue:

  • org.eclipse.persistence.internal.jpa.jpql.JPQLFunctionsAbstractBuilder#visit(org.eclipse.persistence.jpa.jpql.parser.IdExpression) primaryKeyFields variable is list so instead of String idAttributeName = this.getIdAttributeNameByField(descriptor.getMappings(), (DatabaseField)primaryKeyFields.get(0)); there should be some loop into something like List<String> idAttributeNames.
  • useful info about target @IdClass(....) should be in descriptor.cmpPolicy
  • it should leads into org.eclipse.persistence.internal.descriptors.ObjectBuilder#buildNewRecordInstance call instead of simple String

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

No branches or pull requests

3 participants