I am trying to connect a DB in a SpringBoot Project, where i created a repository file for the access. When i try to extend the repository with JpaRepository, i find that it requires two arguments which are defined as below:
public interface UserRepository extends JpaRepository<T, ID> {
}
In my use case the Template is UserEntity which has a String as primary id. So, my doubt is, what is the ID
that is mentioned in the above JpaRepository<T, ID>
. Almost everywhere i see, it is used as Long. So if i understand it right the ID is the datatype of the primary Key (in my case String) or is it something independent of it and can be always used Long?
Yes,
ID
should be the same type as primary key of your entities (which are of type T
). IT became clear after you take a look at declared methods in JpaRepository
interface, for example:
List<T> findAllById(Iterable<ID> ids);
You see Long
as primary key almost everywhere only because its the most popular primary key type (and works best for autoincremented and/or surrogate primary keys).
In your case, if you have @Id
field in your entity of type String
, then ID
parameter should be String