repository.deletebyId() throws exception if id is of type io.jmix.core.id

getById, findById, existById all work fine but deleteById throws ClassCastException

image

Hello, Tom!

Jmix Data Repository implements Spring Data Repository interfaces, where the ID parameter is expected to be the primary key type of the entity.

See org.springframework.data.repository.Repository:

/**
 * ...
 * Type parameters:
 * <T> – the domain type the repository manages
 * <ID> – the type of the id of the entity the repository manages
*/
//...
public interface Repository<T, ID> {
/...

and org.springframework.data.repository.CrudRepository:

// ...
public interface CrudRepository<T, ID> extends Repository<T, ID> {
  //...
  Optional<T> findById(ID id);
  void deleteById(ID id);
  //...
}

Therefore, the correct parameter type for these methods must be the type specified in the repository definition (UUID according to attached exception).

getById, findById, and existsById also work with io.jmix.core.Id because of implementation details and the use of DataManager under the hood, which also accepts this type in some methods.

If you need to support Id as a method parameter, please overload this method in your repository

public interface CustomerRepository extends JmixDataRepository<Customer, UUID> {
    void deleteById(Id<Customer> id);
}

Alternatively, if such support is needed in multiple places, you can introduce a base repository:

@NoRepositoryBean
public interface ProjectBaseRepository<T,ID> extends JmixDataRepository<T,ID> {
    void deleteById(Id<T> id);
}
public interface CustomerRepository extends ProjectBaseRepository<Customer, UUID> {
}

Is there any specific inconvenience in working with the entity’s primary key type, or do you have a common use case where accepting Id would be more convenient?

Regards,
Dmitry