0
votes

I did:

  • created project with some dependencies.
  • created first maven module(web).
  • created second maven module(model).
  • moved code to web module: main class + controller.
  • deleted src from root(because it is already empty)
  • in model module created entity, added dependencies JPA/MySQl driver to pom.xml of this module.
  • in web module added dependency of model module.

Project runs without errors, web module sees model dependency. But code of entity doesnt work(not creating table in database). If i move class to web module - it works. Exactly - it works, but dependencies of JPA/MySQL are still in model module.

What i am doing wrong?

I tried to change names of groupId and artifactId, with package`s names of modules, moved application.propeties to model module - does not help.

My project:

-model/src/main/java and resources/com/example/demo/model

pom.xml:

<modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example.demo.model</groupId>
    <artifactId>model</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>module-model</name>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

-web/src/main/java and resources/com/example/demo/web

application.properties with database connection to MySQL.

pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example.demo.web</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>module-web</name>
    <description>Web project</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>com.example.demo.model</groupId>
            <artifactId>model</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

Main pom.xml:

<modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.1.0</version>
    <packaging>pom</packaging>

    <description>Demo project for Spring Boot</description>
    <name>demo</name>

    <modules>
        <module>web</module>
        <module>model</module>
    </modules>
2
Try with adding @EntityScan(basePackageClasses = EntityPackage.class) annotation to config class. "EntityPackage.class" can be interface in the root of entity package.Aleksandar
interface of what? Or you mean just empty interface just to for this annotation?andrew17
Yes just empty interface or you can use path to entity package @EntityScan("com.example.entity").Aleksandar

2 Answers

1
votes

Anrew you also may adjust @SpringBootApplication(scanBasePackages = { "com.example.demo.model", "com.example.demo.web" }) annotation with "scanBasePackages" it uses @ComponentScan. And bear in mind that you have to add this annotation in "Web project"

@SpringBootApplication(scanBasePackages = "com.example.demo.model","com.example.demo.web" })
public class WebApplication {

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

}
0
votes

@ComponentScan tells Spring in which packages you have annotated classes which should be managed by Spring. and by default, it scans the main package so if spring boot application under com. example only all Classes under this package will be scanned. so if you moved them under different package Spring application will not recognize them.

if this is the case you can add multiple packages to be scanned @ComponentScan({ "x.y.z", "x.y.z.dao" })