0

I have a repository interface that extends JpaRepository and a NameRepositoryCustom. My repository is annotated with @RepositoryRestRessource(collectionResourceRel="pathname", path="pathname"). The problem I have is that every second restart of my application the URL of the repository gets changed so I can't find the exposed data of the repository under the URL I defined and some features like the search of the repository aren't exposed in the API anymore either.

The "NameRepositroyCustom" is used for a search function which uses another Repository to implement Specification with JPA Criteria Api for a searchbar in my frontend.

Does anybody have a solution for this? The only repository annotated as @RepositoryRestRessource is the main repository that implements all the others. The NameRepositorySpec is annotated with @Repository, could this maybe be the cause?

Edit: I implemented the code as an example to clarify the relations between the mentioned classes and interfaces. This is the basic repository related to the entity persisted in the database:

    @RepositoryRestResource(collectionRessourceRel = "enitynames", path = "entitynames")
public interface EntitynameRepository extends JpaRepository<Entityname, Long>, EntitynameRepositoryCustom{

    //custom methods in here
}

This is the custom repository:

 public interface EntitynameRepositoryCustom {

    Page<Entityname> search(String exampleParam1, String exampleParam2, Pageable pageable);
}

This is the implementation of the custom repository:

public class EntitynameRepositoryCustomImpl implements EntitynameRepositoryCustom{

    @Autowired
    EntityManager em;

    @Autowired
    EntitynameRepositorySpec entitynameRepositorySpec;

    Specification<Entityname> querySpecification = null;

    @Override
    public Page<Entityname> search(String exampleParam1, String exampleParam2, Pageable pageable) {
        //Code here uses the criteria builder and Specification to generate a custom query with optional parameters
        CriteriaBuilder cb= em.getCriteriaBuilder();
        CriteriaQuery<Entityname> cq = cb.createQuery(Entityname.class);
        //Code below is done for every passed in parameter
        if(exampleParam1 != null){
            Specification<Entityname> param1Specification = EntitynameSpecification.likeParam1(exampleParam1);
            querySpecification = Specification.where(param1Specification);
        } else {
            return null;
        }

        return entitynameRepositorySpec.findAll(specification, pageable);
    }
}

This is the specification repository:

public interface EntitynameRepositorySpec extends JpaRepository<Entityname, Long>, JpaSpecificationExecutor<Entityname>{
}

And this is the implementation of the specification:

public class EntitynameSpecification {

    public static Specification<Entityname> likeExampleParam1(String exampleParam1){
        if(exampleParam1 == null){
            return null;
        }
        return(root, query, cb) -> {
            reutrn cb.like(root.get("fieldname"), "%"+ exampleParam1 + "%");
        };
    }
}

The URL of the repository gets changed to a part of the entity name compared to my example it would be something like: entityname has URL: /entityname if the bug occurs the URL changes to /name.

  • Can you give examples of the URLs under which the repository resides? – Jens Schauder Feb 23 '21 at 09:59
  • What has `NameRepositorySpec` to do with all this? It doesn't get mentioned before the last sentence. Could you post source code instead of prose describing the source? And finally: `@Repository` on an interface that is implemented by Spring Data JPA, assuming that is what `NameRepositorySpec` is, is superfluous. – Jens Schauder Feb 23 '21 at 10:03

0 Answers0