I'm working with Springboot Application and I've created a genericRepository and all my Repository extend from it and inside the generic repository I defined findByIdAndName(String id,String name) function.My problem is when starting the application it failes because not all entities Have a name attribute But other entities Have it.can I find a way to keep putting the function inside the generic repository? is there any anotation or sth to avoid the runtime error,here's the repo code
import java.util.Optional;
import org.springframework.data.repository.NoRepositoryBean;
import com.mobinets.nep.client.backend.repository.AbstractEntityRepository;
@NoRepositoryBean
public interface GenericCatalogRepository<E,ID> extends AbstractEntityRepository<E,ID> {
Optional<E> findByIdAndName(String id,String name);
}
does anyone face simillar problem before,Thanks in advance
In my perspective, it appears to be somewhat of an overhead, especially considering it might not qualify as a high-level abstraction for all cases, particularly when not every entity possesses a 'name' field. While one potential solution that comes to mind involves the creation of multiple GenericCatalogRepositories (one for entities with a 'name' field and another for those without), it still introduces a degree of unnecessary complexity.
I propose an alternative approach: refrain from overusing generics and instead, opt for the creation of dedicated repositories for each entity. These repositories can extend the default JPA repositories (e.g., JpaRepository). While this might result in some duplicated code, especially in the context of searching by name, I believe it's a manageable trade-off and doesn't pose a significant problem.