0

I spent my day trying to understand but I can't understand my problem.

I have 1 spring module (https://spring.io/guides/gs/multi-module/) [I also have JPA in the sub module] and 2 microservices (A and B).

In microservice A, I arrive at @Autowired CountryRepository (which extends from JpaRepository) and which works very well, SpringBootApplication (scanBasePackages = "**") with, I access my data and I can compile.

On microservice B, I did the same thing, I manage to access my data but impossible to compile:

****************************
FAILED TO START APPLICATION
****************************

Description:

Field countryRepository in * required a bean named 'entityManagerFactory' that could not be found.

The injection point has the following annotations:
        - @ org.springframework.beans.factory.annotation.Autowired (required = true)


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

I cannot understand or look for the error, how to unblock myself, I search again and again but I cannot understand why microservice B does not want to compile.

I use a POM Parent, and I was careful to put the following order:

<modules>
<module>sub module </module>
<module> microservice A </module>
<module> microservice B </module>
</modules>

So I hack, without understanding too much and sometimes I have different messages but always the compilation which fails

Concretely, it is possible to use the Repositories of the sub-module in microservice A and B?

Thank you for your attention and your help

2 Answers2

0

Yes, you can use Repositories of sub-modules.

You have to use @EnableJpaRepository in your main class and give the base package name there.

That's all, This will do the trick

Vishal Varshney
  • 105
  • 1
  • 8
  • Thank you ... But I must miss something ... In the sub module, Application I have @ Service, @ SpringbootApplication @ EnableConfigurationProperties. In the @ Entity model and in the repository no anotation. – Olivier4477 Jul 13 '20 at 22:15
  • In microservice A and B, I put @SpringBootApplicationcanBasePackages = "") in an empty class with its own package. It worked very well for implementing a model object in the sub module, but since I want to use JPA in microservice A and B I have a lot of problems. So I move @ SpringBootApplication (scanBasePackages = "") in the class of microservice A / B or I use @ Autowired, I added @ EnableJpaRepositories (suddenly I had an error that the bean is duplicated), this confirms the support for scanBasePackage, ... but it still doesn't work and I don't understand why – Olivier4477 Jul 13 '20 at 22:15
  • can you share your snippet ? – Vishal Varshney Jul 14 '20 at 08:16
  • ok thank you i will make a deposit during the day by recreating the project. But I also have a message from my IDE that says unmapped – Olivier4477 Jul 14 '20 at 12:09
  • I will have a look and get back to you – Vishal Varshney Jul 14 '20 at 13:57
  • I am surprised that on my project or I have the concern (which have changed) Description: Field societeRepository in * required a bean of type '*' that could not be found. The injection point has the following annotations: - @ org.springframework.beans.factory.annotation.Autowired (required = true) Action: Consider defining a bean of type '*' in your configuration. – Olivier4477 Jul 14 '20 at 13:59
  • receive a message from IntelliJ "Spring configuration Check, Unmapped Spring configuration files found. Please configuration Spring facet or use 'Create Default Context' to add one including all unmapped files" – Olivier4477 Jul 14 '20 at 13:59
  • https://gitlab.com/Olivier4477/submodule I redid the manipulation on a new project, obviously it works but I let you watch. The project works, but ./mvnw package does not work – Olivier4477 Jul 14 '20 at 14:09
0

I had a look at your repo and found few of the things are missing

In your CountryRepository.java class

@Repository 
public interface CountryRepository extends JpaRepository<Country, Long> {
}

@Repository annotation is missing, you need to add that

Then in your Main Class of service A and service B you have to add as below

@SpringBootApplication
@EnableJpaRepository("com.submodule.commun.modelandjpa.Repository")
public class ParentApplication {

public static void main(String[] args) {
    SpringApplication.run(ParentApplication.class, args);
}

}

That's all..

Vishal Varshney
  • 105
  • 1
  • 8
  • Ok thanks but 1) Why ./mvnw package puts me: [ERROR] * src / src / main / java / com / project / microservicea / Test.java: [6,46] package com.submodule.commun.modelandjpa.Model does not exist [ERROR] * / src / main / java / com / project / microservicea / Test.java: [7,51] package com.submodule.commun.modelandjpa.Repository does not exist [ERROR] * / src / main / java / com / project / microservicea / Test.java: [16,5] cannot find symbol [ERROR] symbol: class CountryRepository [ERROR] location: class com.projet.microservicea.Test – Olivier4477 Jul 14 '20 at 15:18
  • [ERROR] * src / main / java / com / project / microservicea / Test.java: [20,9] cannot find symbol [ERROR] symbol: class Country [ERROR] location: class com.projet.microservicea.Test [ERROR] * / src / main / java / com / project / microservicea / Test.java: [20,31] cannot find symbol [ERROR] symbol: class Country [ERROR] location: class com.projet.microservicea.Test – Olivier4477 Jul 14 '20 at 15:18
  • 2) microservice B works but not A ... It's annoying when you head in the handlebars when faced with bugs like this;) I have redone, I don't even know where I am – Olivier4477 Jul 14 '20 at 15:20