Search code examples
javaspring-bootmavenjpaentity

Spring Boot: Entities within a Maven dependency not recognized: "Not a managed type"


I have a main Spring Boot project, which relies on some Entity classes which we want to put in a dependency library.

The main Spring Boot app has a main class in package com.mycompany as follows:

package com.mycompany;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;


//@EntityScan("com.mycompany.library.*")
@SpringBootApplication
public class MainAppApplication {

    public static void main(String[] args) {

        SpringApplication.run(MainAppApplication.class, args);
    }

}

Note about the above, I have tried it with both the commented out line and without, to same effect.

The library dependency base package is com.mycompany.library and the package containing entities is com.mycompany.library.entity. Here is an example entity:

package com.mycompany.library.entity;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import com.mycompany.library.constants.MemberStatus;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Member {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "member_id")
    private Long memberId;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    private String address;

    private String email;

    @Column(name = "phone_number")
    private String phoneNumber;

    @Enumerated(EnumType.STRING)
    private MemberStatus status;

}

The JPA repositories are located also on the dependency library, for example:

package com.mycompany.library.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.mycompany.library.entity.Member;

public interface MemberRepository extends JpaRepository<Member, Long>{

}

When I try to start the main application. I get the following error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'memberRepository' defined in com.mycompany.library.repository.MemberRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.mycompany.library.entity.Member

What can I do to make the entities be successfully recognized and the JPA repositories to work correctly in this architecture?


Solution

  • OK, so the issue I had, which may have been impossible to figure out from the question, is that main application and the dependency library had different versions of Spring Boot.

    The library had Spring Boot version 2.7.5, and the newer main app had Spring Boot 3.0.0, which was a breaking change.

    When I changed the Spring Boot version to 3.0.0 on the library, I additionally had to rename the imports on the Entity classes. The packages were renamed.

    The following imports are no longer valid in the Spring Boot JPA starter that comes with Spring Boot 3.0.0:

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.EnumType;
    import javax.persistence.Enumerated;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
    

    Instead I used these and the application is working as expected:

    import jakarta.persistence.Column;
    import jakarta.persistence.Entity;
    import jakarta.persistence.EnumType;
    import jakarta.persistence.Enumerated;
    import jakarta.persistence.GeneratedValue;
    import jakarta.persistence.GenerationType;
    import jakarta.persistence.Id;
    import jakarta.persistence.OneToMany;