programing

"불법 인수예외:Spring Boot 어플리케이션에서 관리 타입이 아님"

javaba 2023. 2. 11. 22:39
반응형

"불법 인수예외:Spring Boot 어플리케이션에서 관리 타입이 아님"

Spring Boot와 JPA를 사용하고 있습니다.여기 있습니다

다음과 같은 도메인이 있습니다.또 그런 것 같다Entity주석은 권장되지 않기 때문에 사용하고 있습니다.@DynamicUpdate대신.

@Data
@AllArgsConstructor
@NoArgsConstructor
@DynamicUpdate
public class Person {
    private String id;
    private String name;
}

나의@SpringBootApplication수업은 이렇게 생겼다.

@SpringBootApplication
@ComponentScan("com.lapots.breed.platform.cloud.boot")
@EnableJpaRepositories("com.lapots.breed.platform.cloud.boot.repository")
@EntityScan("com.lapots.breed.platform.cloud.boot.domain")
public class JavaCloudSampleApplication {

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

    // executed after all beans instantiated
    @Bean
    public CommandLineRunner data(PersonRepository repository) {
        return (args) -> {
            repository.save(new Person(UUID.randomUUID().toString(), "Nike"));
        };
    }
}

@Repository수업은 이렇게 생겼다.

@Repository
public interface PersonRepository extends JpaRepository<Person, String> {
}

@Service이 클래스를 사용하는 것은 다음과 같습니다.

@Service
@Transactional
public class PersonService implements IPersonService {

    @Autowired
    private PersonRepository personRepository;

    @Override
    public Person findPerson(String id) {
        return personRepository.findOne(id);
    }

    @Override
    public Person generatePerson() {
        Person person = new Person();
        person.setId(UUID.randomUUID().toString());
        person.setName("Nike");
        return person;
    }
}

그러나 응용 프로그램을 시작할 때 다음 오류가 발생합니다.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personService': Unsatisfied dependency expressed through field 'personRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error
 creating bean with name 'personRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.lapots.breed.platform.cloud.boot.domain.Person
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
        at com.lapots.breed.platform.cloud.boot.app.JavaCloudSampleApplication.main(JavaCloudSampleApplication.java:23) [classes!/:0.0.1-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_121]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_121]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_121]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [java-cloud-rest-api-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [java-cloud-rest-api-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [java-cloud-rest-api-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [java-cloud-rest-api-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.lapots.breed.platform.cl
oud.boot.domain.Person
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        ... 27 common frames omitted
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.lapots.breed.platform.cloud.boot.domain.Person
        at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:210) ~[hibernate-entitymanager-5.0.12.Final.jar!/:5.0.12.Final]
        at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:70) ~[spring-data-jpa-1.11.6.RELEASE.jar!/:na]
        at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:68) ~[spring-data-jpa-1.11.6.RELEASE.jar!/:na]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:153) ~[spring-data-jpa-1.11.6.RELEASE.jar!/:na]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:100) ~[spring-data-jpa-1.11.6.RELEASE.jar!/:na]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:82) ~[spring-data-jpa-1.11.6.RELEASE.jar!/:na]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:199) ~[spring-data-commons-1.13.6.RELEASE.jar!/:na]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.6.RELEASE.jar!/:na]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.6.RELEASE.jar!/:na]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.6.RELEASE.jar!/:na]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        ... 37 common frames omitted

뭐가 문제죠?

아마도요.Person필요한 것은Entity주석에 추가해당신이 JPA를 사용하고 있기 때문입니다.

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@DynamicUpdate
public class Person {
    private String id;
    private String name;
}

를 추가해야 합니다.@Entity모델/POJO 클래스에서 다음과 같이 작업합니다.

@Entity
public class Person {
  //your model goes here
}

Spring Data 3을 사용하고 있는 분들을 위해 많은 연구와 시도 끝에 대부분의 오래된 조언은 이전 버전의 Spring Data에서는 유효하지만 버전에서는 유효하지 않다는 것을 알게 되었습니다.3.0.0그래서 모든 시도가 실패했습니다.나는 다음과 같이 시도했다.

  • 주석을 추가합니다(@EnableJpaRepositories,@ComponentScan,@EntityScan패키지명에 아스타리스크를 붙이거나 붙이지 않습니다.나는 아직 있다Not a managed type: class […]프로그램 시작 시 치명적인 오류가 발생합니다.
  • 를 추가하다entityManagerFactory()bean. 순환 참조 오류가 발생했습니다.

결국, 는 이 샘플 프로젝트를 예로 든 공식 가이드를 찾았습니다.이것은 자카르타의 주석을 사용합니다(예:jakarta.persistence.Entity대신 )를 사용합니다.javax.persistence그리고 자카르타 주석으로 제 프로젝트는 성공하기 시작했습니다.

스프링 부트에서는 repo 패키지의 기본 이름이 엔티티 패키지의 기본 이름과 일치해야 합니다.

예:
지정된 기본 이름:

com.abc.mcm

엔티티 패키지 이름:

com.abc.mcm.entities 

Repo 패키지 이름:

com.abc.mcm.repositories

이 질문은 3년 이상 지났지만, 나에게 효과가 있었던 해결책을 말하고 싶다.

모든 클래스/인터페이스/리포지토리/모델 등을 같은 패키지에 넣습니다.이거면 될 거야.

향후 검색 대상자를 위해 작성:

엔티티 매니저 메서드에서LocalContainerEntityManagerFactoryBeanDTO/POJO 클래스가 있는 패키지를 다음과 같이 설정합니다.setPackagesToScan

예:

@Bean
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Exception {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(new String[] {"com.abc.mainRepository.model"});
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    HashMap<String, Object> properties = new HashMap<>();
    properties.put("hibernate.dialect", dialect);
    em.setJpaPropertyMap(properties);
    return em;
}

날 위해 일했다!

(모델 패키지 안에 POJO가 들어 있었습니다).

스프링 부츠 프로젝트에서도 같은 에러가 발생했습니다.새 패키지를 추가했지만 편집하지 않았습니다.componentScan.

그랬더니 에러가 났어요.

com.okan.entity

추가한 대로 변경했습니다.

com.okan.model.entity
import javax.persistence.Entity;

@Entity수업에 추가되면 효과가 있을 것이다.

persistence.xml에 엔티티가 매핑되어 있습니까?왜냐하면 같은 오류가 있었고 거기서 엔티티가 매핑되지 않았다는 것을 깨달았기 때문입니다.

저 같은 경우에는 멀티매븐 모듈 프로젝트가 있었는데, 그 중 하나가model및 는 '''입니다.microservices 에서 정의한 model package내 됨), (도메인이 호출됨), (도메인이 호출됨), (도메인이 호출됨), (도메인)을 추가해야 .@EntityScan(basePackages = {"com.example.domain"})의 주석@SpringBootApplication class:

@SpringBootApplication
@EnableEurekaClient
@EntityScan(basePackages = {"com.example.domain"}) // add this so the spring boot context knows where to look after entities
public class DoctorServiceApplication {

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

}

언급URL : https://stackoverflow.com/questions/46246104/illegalargumentexception-not-a-managed-type-in-spring-boot-application

반응형