Jose
Jose

Reputation: 136

How can I Autowire an entity from external Jar?

I'm new to spring boot, and I have spent some hours trying to figure out why when I move my entity to another project I'm getting an error: "java.lang.TypeNotPresentException: Type net.learn.spring.entity.Book not present". What I'm missing?

This is my project structure:

enter image description here

This is my entity project structure:

enter image description here

Entity project POM:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.mystest</groupId>
    <artifactId>CommonVO</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name> </name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.2.7.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.11.0</version>
        </dependency>
    </dependencies>
</project>

This is my StartApplication class:

package net.learn.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import net.learn.spring.entity.Book;
import net.learn.spring.repository.Repository;

@SpringBootApplication
public class StartApplication implements CommandLineRunner{
    
    @Autowired
    private Repository repository;
    
    public static void main(String[] args) {
        SpringApplication spa = new SpringApplication(StartApplication.class);
        spa.run(args);
    }

    @Override
    public void run(String... args) throws Exception {

        repository.save(new Book("Las 1000 y una noches"));
        
    }

}
package net.learn.spring.repository;

import org.springframework.data.repository.CrudRepository;

import net.learn.spring.entity.Book;

public interface Repository extends CrudRepository<Book,Integer>{

}
package net.learn.spring.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "ta_book")
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    public Book() {
    }

    public Book(String name) {
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Book [id=");
        builder.append(id);
        builder.append(", name=");
        builder.append(name);
        builder.append("]");
        return builder.toString();
    }

    
}

POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.learn</groupId>
  <artifactId>LearningSpringBoot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <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>
        <dependency>
            <groupId>net.test</groupId>
            <artifactId>CommonVO</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

console output

enter image description here

Upvotes: 1

Views: 1992

Answers (3)

ardatosun
ardatosun

Reputation: 467

Update your main class like below:

...
@SpringBootApplication
@EntityScan(basePackages = "net.learn.spring.entity")
public class StartApplication implements CommandLineRunner {
...

@EntityScan tells Spring where you keep your @Entity files (which will be used by the persistence context). Since your main class and Entity classes lie in different packages, Spring can not find your entities by default and needs you to show the location for them.

Upvotes: 1

Gourava Sharma
Gourava Sharma

Reputation: 59

You have to scan the package that contains the entity class.

@Configuration
@ComponentScan("com.package.external.class")
class Config {
...
}

Upvotes: 0

Alex
Alex

Reputation: 136

Can you post pom.xml file of the CommonVO project? Also, I advise you to add EntityScan annotation to your StartApplication like this: @EntityScan("net.learn.spring.entity.*")

It is used to identify the base package where entities are scanned during application startup.

Upvotes: 2

Related Questions