Spring Data - How to reuse repository in repository fragment?












0















Update: the following is using Spring Boot 2.1.0



I have a Spring Data repository and I am trying to provide some custom functionality to it, following the fragments example from the documentation.



So I've added an extra interface to the repository:



public interface UserRepository extends JpaRepository<User, Long>, UserExtraLogic {
User findByFirstNameAndLastName(String firstName, String lastName);
}


with this custom interface:



interface UserExtraLogic {
void ensureHasAccess();
}


and its implementation:



class UserExtraLogicImpl implements UserExtraLogic {
public void ensureHasAccess() {
}
}


The problem is that I would like to be able to use my repositories inside UserExtraLogicImpl, so that I can reuse query methods like findByFirstNameAndLastName without having to write them by myself with EntityManager. So I tried this:



class UserExrtaLogicImpl implements UserExtraLogic {
@Autowired
private UserRepository userRepository;
}


But then the application does not start. I get a NullPointerException, but I think it's just Spring getting into a cycle trying to resolve these dependencies.



Is what I'm trying to do possible? Is there another way to do this?










share|improve this question

























  • Hello. Could you please provide the stacktrace of the exception ? Does your interface have @Repository annotation ? Did you enable JPA repositories in your configuration ?

    – Mickael
    Nov 15 '18 at 15:17













  • Hi Mickael, the stacktrace is too long and it doesn't fit the question. I have not added the @Repository annotation and I have not enabled "JPA repositories" (I don't know what that means). Please note that everything works as expected, until the moment I try to autowire the UserRepository inside the fragment.

    – Nikolaos Georgiou
    Nov 15 '18 at 15:33













  • Could you please explain what "works" ? Do you mean you have no exceptions ? Or do you mean you can actually use the repositories ?

    – Mickael
    Nov 15 '18 at 15:56











  • Hi Mikael, when I say "works", I mean that the data access works as expected. I'm currently using EntityManager and manually creating queries using the Java Persistence Query Language inside the fragments. That's the part I'd like to avoid, because now I'm implementing with EntityManager a method that I normally get for free (e.g. findByFirstNameAndLastName).

    – Nikolaos Georgiou
    Nov 15 '18 at 16:04
















0















Update: the following is using Spring Boot 2.1.0



I have a Spring Data repository and I am trying to provide some custom functionality to it, following the fragments example from the documentation.



So I've added an extra interface to the repository:



public interface UserRepository extends JpaRepository<User, Long>, UserExtraLogic {
User findByFirstNameAndLastName(String firstName, String lastName);
}


with this custom interface:



interface UserExtraLogic {
void ensureHasAccess();
}


and its implementation:



class UserExtraLogicImpl implements UserExtraLogic {
public void ensureHasAccess() {
}
}


The problem is that I would like to be able to use my repositories inside UserExtraLogicImpl, so that I can reuse query methods like findByFirstNameAndLastName without having to write them by myself with EntityManager. So I tried this:



class UserExrtaLogicImpl implements UserExtraLogic {
@Autowired
private UserRepository userRepository;
}


But then the application does not start. I get a NullPointerException, but I think it's just Spring getting into a cycle trying to resolve these dependencies.



Is what I'm trying to do possible? Is there another way to do this?










share|improve this question

























  • Hello. Could you please provide the stacktrace of the exception ? Does your interface have @Repository annotation ? Did you enable JPA repositories in your configuration ?

    – Mickael
    Nov 15 '18 at 15:17













  • Hi Mickael, the stacktrace is too long and it doesn't fit the question. I have not added the @Repository annotation and I have not enabled "JPA repositories" (I don't know what that means). Please note that everything works as expected, until the moment I try to autowire the UserRepository inside the fragment.

    – Nikolaos Georgiou
    Nov 15 '18 at 15:33













  • Could you please explain what "works" ? Do you mean you have no exceptions ? Or do you mean you can actually use the repositories ?

    – Mickael
    Nov 15 '18 at 15:56











  • Hi Mikael, when I say "works", I mean that the data access works as expected. I'm currently using EntityManager and manually creating queries using the Java Persistence Query Language inside the fragments. That's the part I'd like to avoid, because now I'm implementing with EntityManager a method that I normally get for free (e.g. findByFirstNameAndLastName).

    – Nikolaos Georgiou
    Nov 15 '18 at 16:04














0












0








0








Update: the following is using Spring Boot 2.1.0



I have a Spring Data repository and I am trying to provide some custom functionality to it, following the fragments example from the documentation.



So I've added an extra interface to the repository:



public interface UserRepository extends JpaRepository<User, Long>, UserExtraLogic {
User findByFirstNameAndLastName(String firstName, String lastName);
}


with this custom interface:



interface UserExtraLogic {
void ensureHasAccess();
}


and its implementation:



class UserExtraLogicImpl implements UserExtraLogic {
public void ensureHasAccess() {
}
}


The problem is that I would like to be able to use my repositories inside UserExtraLogicImpl, so that I can reuse query methods like findByFirstNameAndLastName without having to write them by myself with EntityManager. So I tried this:



class UserExrtaLogicImpl implements UserExtraLogic {
@Autowired
private UserRepository userRepository;
}


But then the application does not start. I get a NullPointerException, but I think it's just Spring getting into a cycle trying to resolve these dependencies.



Is what I'm trying to do possible? Is there another way to do this?










share|improve this question
















Update: the following is using Spring Boot 2.1.0



I have a Spring Data repository and I am trying to provide some custom functionality to it, following the fragments example from the documentation.



So I've added an extra interface to the repository:



public interface UserRepository extends JpaRepository<User, Long>, UserExtraLogic {
User findByFirstNameAndLastName(String firstName, String lastName);
}


with this custom interface:



interface UserExtraLogic {
void ensureHasAccess();
}


and its implementation:



class UserExtraLogicImpl implements UserExtraLogic {
public void ensureHasAccess() {
}
}


The problem is that I would like to be able to use my repositories inside UserExtraLogicImpl, so that I can reuse query methods like findByFirstNameAndLastName without having to write them by myself with EntityManager. So I tried this:



class UserExrtaLogicImpl implements UserExtraLogic {
@Autowired
private UserRepository userRepository;
}


But then the application does not start. I get a NullPointerException, but I think it's just Spring getting into a cycle trying to resolve these dependencies.



Is what I'm trying to do possible? Is there another way to do this?







java spring spring-boot spring-data-jpa spring-data






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 16:24







Nikolaos Georgiou

















asked Nov 15 '18 at 14:59









Nikolaos GeorgiouNikolaos Georgiou

1,63211421




1,63211421













  • Hello. Could you please provide the stacktrace of the exception ? Does your interface have @Repository annotation ? Did you enable JPA repositories in your configuration ?

    – Mickael
    Nov 15 '18 at 15:17













  • Hi Mickael, the stacktrace is too long and it doesn't fit the question. I have not added the @Repository annotation and I have not enabled "JPA repositories" (I don't know what that means). Please note that everything works as expected, until the moment I try to autowire the UserRepository inside the fragment.

    – Nikolaos Georgiou
    Nov 15 '18 at 15:33













  • Could you please explain what "works" ? Do you mean you have no exceptions ? Or do you mean you can actually use the repositories ?

    – Mickael
    Nov 15 '18 at 15:56











  • Hi Mikael, when I say "works", I mean that the data access works as expected. I'm currently using EntityManager and manually creating queries using the Java Persistence Query Language inside the fragments. That's the part I'd like to avoid, because now I'm implementing with EntityManager a method that I normally get for free (e.g. findByFirstNameAndLastName).

    – Nikolaos Georgiou
    Nov 15 '18 at 16:04



















  • Hello. Could you please provide the stacktrace of the exception ? Does your interface have @Repository annotation ? Did you enable JPA repositories in your configuration ?

    – Mickael
    Nov 15 '18 at 15:17













  • Hi Mickael, the stacktrace is too long and it doesn't fit the question. I have not added the @Repository annotation and I have not enabled "JPA repositories" (I don't know what that means). Please note that everything works as expected, until the moment I try to autowire the UserRepository inside the fragment.

    – Nikolaos Georgiou
    Nov 15 '18 at 15:33













  • Could you please explain what "works" ? Do you mean you have no exceptions ? Or do you mean you can actually use the repositories ?

    – Mickael
    Nov 15 '18 at 15:56











  • Hi Mikael, when I say "works", I mean that the data access works as expected. I'm currently using EntityManager and manually creating queries using the Java Persistence Query Language inside the fragments. That's the part I'd like to avoid, because now I'm implementing with EntityManager a method that I normally get for free (e.g. findByFirstNameAndLastName).

    – Nikolaos Georgiou
    Nov 15 '18 at 16:04

















Hello. Could you please provide the stacktrace of the exception ? Does your interface have @Repository annotation ? Did you enable JPA repositories in your configuration ?

– Mickael
Nov 15 '18 at 15:17







Hello. Could you please provide the stacktrace of the exception ? Does your interface have @Repository annotation ? Did you enable JPA repositories in your configuration ?

– Mickael
Nov 15 '18 at 15:17















Hi Mickael, the stacktrace is too long and it doesn't fit the question. I have not added the @Repository annotation and I have not enabled "JPA repositories" (I don't know what that means). Please note that everything works as expected, until the moment I try to autowire the UserRepository inside the fragment.

– Nikolaos Georgiou
Nov 15 '18 at 15:33







Hi Mickael, the stacktrace is too long and it doesn't fit the question. I have not added the @Repository annotation and I have not enabled "JPA repositories" (I don't know what that means). Please note that everything works as expected, until the moment I try to autowire the UserRepository inside the fragment.

– Nikolaos Georgiou
Nov 15 '18 at 15:33















Could you please explain what "works" ? Do you mean you have no exceptions ? Or do you mean you can actually use the repositories ?

– Mickael
Nov 15 '18 at 15:56





Could you please explain what "works" ? Do you mean you have no exceptions ? Or do you mean you can actually use the repositories ?

– Mickael
Nov 15 '18 at 15:56













Hi Mikael, when I say "works", I mean that the data access works as expected. I'm currently using EntityManager and manually creating queries using the Java Persistence Query Language inside the fragments. That's the part I'd like to avoid, because now I'm implementing with EntityManager a method that I normally get for free (e.g. findByFirstNameAndLastName).

– Nikolaos Georgiou
Nov 15 '18 at 16:04





Hi Mikael, when I say "works", I mean that the data access works as expected. I'm currently using EntityManager and manually creating queries using the Java Persistence Query Language inside the fragments. That's the part I'd like to avoid, because now I'm implementing with EntityManager a method that I normally get for free (e.g. findByFirstNameAndLastName).

– Nikolaos Georgiou
Nov 15 '18 at 16:04












1 Answer
1






active

oldest

votes


















0














Repositories enablement



From documentation



If you're using Spring XML configuration, you should have this:



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

<jpa:repositories base-package="com.acme.repositories" />

</beans>


If you're using Java configuration instead, you should have this:



@Configuration
@EnableJpaRepositories("com.acme.repositories")
class ApplicationConfiguration {

@Bean
EntityManagerFactory entityManagerFactory() {
// …
}
}


Repositories configuration



Also, you need to add @Repository annotation on your repositories:



@Repository
public interface UserRepository extends JpaRepository<User, Long>, UserExtraLogic {
User findByFirstNameAndLastName(String firstName, String lastName);
}


Explanation



From documentation




Using the repositories element looks up Spring Data repositories as
described in “Creating Repository Instances”. Beyond that, it
activates persistence exception translation for all beans annotated
with @Repository, to let exceptions being thrown by the JPA
persistence providers be converted into Spring’s DataAccessException
hierarchy.







share|improve this answer
























  • Hi Mickael, thank you for the help. I realise that I forgot to mention that my app is in Spring Boot 2.1.0. I'm sorry I forgot to mention it. I think what you're mentioning is already provided for me by Spring Boot.

    – Nikolaos Georgiou
    Nov 15 '18 at 16:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322230%2fspring-data-how-to-reuse-repository-in-repository-fragment%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Repositories enablement



From documentation



If you're using Spring XML configuration, you should have this:



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

<jpa:repositories base-package="com.acme.repositories" />

</beans>


If you're using Java configuration instead, you should have this:



@Configuration
@EnableJpaRepositories("com.acme.repositories")
class ApplicationConfiguration {

@Bean
EntityManagerFactory entityManagerFactory() {
// …
}
}


Repositories configuration



Also, you need to add @Repository annotation on your repositories:



@Repository
public interface UserRepository extends JpaRepository<User, Long>, UserExtraLogic {
User findByFirstNameAndLastName(String firstName, String lastName);
}


Explanation



From documentation




Using the repositories element looks up Spring Data repositories as
described in “Creating Repository Instances”. Beyond that, it
activates persistence exception translation for all beans annotated
with @Repository, to let exceptions being thrown by the JPA
persistence providers be converted into Spring’s DataAccessException
hierarchy.







share|improve this answer
























  • Hi Mickael, thank you for the help. I realise that I forgot to mention that my app is in Spring Boot 2.1.0. I'm sorry I forgot to mention it. I think what you're mentioning is already provided for me by Spring Boot.

    – Nikolaos Georgiou
    Nov 15 '18 at 16:25
















0














Repositories enablement



From documentation



If you're using Spring XML configuration, you should have this:



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

<jpa:repositories base-package="com.acme.repositories" />

</beans>


If you're using Java configuration instead, you should have this:



@Configuration
@EnableJpaRepositories("com.acme.repositories")
class ApplicationConfiguration {

@Bean
EntityManagerFactory entityManagerFactory() {
// …
}
}


Repositories configuration



Also, you need to add @Repository annotation on your repositories:



@Repository
public interface UserRepository extends JpaRepository<User, Long>, UserExtraLogic {
User findByFirstNameAndLastName(String firstName, String lastName);
}


Explanation



From documentation




Using the repositories element looks up Spring Data repositories as
described in “Creating Repository Instances”. Beyond that, it
activates persistence exception translation for all beans annotated
with @Repository, to let exceptions being thrown by the JPA
persistence providers be converted into Spring’s DataAccessException
hierarchy.







share|improve this answer
























  • Hi Mickael, thank you for the help. I realise that I forgot to mention that my app is in Spring Boot 2.1.0. I'm sorry I forgot to mention it. I think what you're mentioning is already provided for me by Spring Boot.

    – Nikolaos Georgiou
    Nov 15 '18 at 16:25














0












0








0







Repositories enablement



From documentation



If you're using Spring XML configuration, you should have this:



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

<jpa:repositories base-package="com.acme.repositories" />

</beans>


If you're using Java configuration instead, you should have this:



@Configuration
@EnableJpaRepositories("com.acme.repositories")
class ApplicationConfiguration {

@Bean
EntityManagerFactory entityManagerFactory() {
// …
}
}


Repositories configuration



Also, you need to add @Repository annotation on your repositories:



@Repository
public interface UserRepository extends JpaRepository<User, Long>, UserExtraLogic {
User findByFirstNameAndLastName(String firstName, String lastName);
}


Explanation



From documentation




Using the repositories element looks up Spring Data repositories as
described in “Creating Repository Instances”. Beyond that, it
activates persistence exception translation for all beans annotated
with @Repository, to let exceptions being thrown by the JPA
persistence providers be converted into Spring’s DataAccessException
hierarchy.







share|improve this answer













Repositories enablement



From documentation



If you're using Spring XML configuration, you should have this:



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

<jpa:repositories base-package="com.acme.repositories" />

</beans>


If you're using Java configuration instead, you should have this:



@Configuration
@EnableJpaRepositories("com.acme.repositories")
class ApplicationConfiguration {

@Bean
EntityManagerFactory entityManagerFactory() {
// …
}
}


Repositories configuration



Also, you need to add @Repository annotation on your repositories:



@Repository
public interface UserRepository extends JpaRepository<User, Long>, UserExtraLogic {
User findByFirstNameAndLastName(String firstName, String lastName);
}


Explanation



From documentation




Using the repositories element looks up Spring Data repositories as
described in “Creating Repository Instances”. Beyond that, it
activates persistence exception translation for all beans annotated
with @Repository, to let exceptions being thrown by the JPA
persistence providers be converted into Spring’s DataAccessException
hierarchy.








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 16:10









MickaelMickael

3,03021730




3,03021730













  • Hi Mickael, thank you for the help. I realise that I forgot to mention that my app is in Spring Boot 2.1.0. I'm sorry I forgot to mention it. I think what you're mentioning is already provided for me by Spring Boot.

    – Nikolaos Georgiou
    Nov 15 '18 at 16:25



















  • Hi Mickael, thank you for the help. I realise that I forgot to mention that my app is in Spring Boot 2.1.0. I'm sorry I forgot to mention it. I think what you're mentioning is already provided for me by Spring Boot.

    – Nikolaos Georgiou
    Nov 15 '18 at 16:25

















Hi Mickael, thank you for the help. I realise that I forgot to mention that my app is in Spring Boot 2.1.0. I'm sorry I forgot to mention it. I think what you're mentioning is already provided for me by Spring Boot.

– Nikolaos Georgiou
Nov 15 '18 at 16:25





Hi Mickael, thank you for the help. I realise that I forgot to mention that my app is in Spring Boot 2.1.0. I'm sorry I forgot to mention it. I think what you're mentioning is already provided for me by Spring Boot.

– Nikolaos Georgiou
Nov 15 '18 at 16:25




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322230%2fspring-data-how-to-reuse-repository-in-repository-fragment%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python