jakartaee / persistence

https://jakartaee.github.io/persistence/
Other
186 stars 55 forks source link

Supports create criteria parameter for generic type #610

Open quaff opened 3 months ago

quaff commented 3 months ago
ParameterExpression<List> parameter = criteriaBuilder.parameter(List.class, "list");

is not type safe, We need add a Super Type Token like TypeReference

ParameterExpression<List<Integer>> parameter = criteriaBuilder.parameter(new TypeReference<List<Integer>>{}, "list");
gavinking commented 3 months ago

You mean like a CDI TypeLiteral, right?

But what is the usecase here? For use with in conditions? Or something else?

quaff commented 3 months ago

You mean like a CDI TypeLiteral, right?

Yes.

But what is the usecase here? For use with in conditions? Or something else?

Not in but equal.

    @Entity
    public class TestEntity {

        @Id
        @GeneratedValue
        Long id;

        List<Integer> list;

    }
ParameterExpression<List<Integer>> parameter = criteriaBuilder.parameter(List.class);  // cannot compile
Root<TestEntity> root = criteria.from(TestEntity.class);
criteria.select(root).where(criteriaBuilder.equal(root.get(TestEntity_.list), parameter));
gavinking commented 3 months ago

I see. So I suppose this:

List<Integer> list;

is possible in standard JPA with an AttributeConverter.

quaff commented 3 months ago

I see. So I suppose this:

List<Integer> list;

is possible in standard JPA with an AttributeConverter.

Hibernate will map it to array now, no need AttributeConverter.

gavinking commented 3 months ago

Right, I know that, but that's not standard in the Persistence spec.