스프링 부트 + JPA를 사용하고 서비스를 시작하는 동안 문제가 발생했습니다.
Caused by: java.lang.IllegalArgumentException: Not an managed type: class com.nervytech.dialer.domain.PhoneSettings
at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:219)
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:68)
at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:65)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:145)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:89)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:69)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:177)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
다음은 Application.java 파일입니다.
@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
@SpringBootApplication
public class DialerApplication {
public static void main(String[] args) {
SpringApplication.run(DialerApplication.class, args);
}
}
연결 풀링에 UCp를 사용하며 DataSource 구성은 다음과 같습니다.
@Configuration
@ComponentScan
@EnableTransactionManagement
@EnableAutoConfiguration
@EnableJpaRepositories(entityManagerFactoryRef = "dialerEntityManagerFactory", transactionManagerRef = "dialerTransactionManager", basePackages = { "com.nervy.dialer.spring.jpa.repository" })
public class ApplicationDataSource {
/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory
.getLogger(ApplicationDataSource.class);
/** The Constant TEST_SQL. */
private static final String TEST_SQL = "select 1 from dual";
/** The pooled data source. */
private PoolDataSource pooledDataSource;
UserDetailsService 구현,
@Service("userDetailsService")
@SessionAttributes("user")
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserService userService;
서비스 계층 구현
@Service
public class PhoneSettingsServiceImpl implements PhoneSettingsService {
}
저장소 클래스
@Repository
public interface PhoneSettingsRepository extends JpaRepository<PhoneSettings, Long> {
}
엔터티 클래스
@Entity
@Table(name = "phone_settings", catalog = "dialer")
public class PhoneSettings implements java.io.Serializable {
WebSecurityConfig 클래스
@Configuration
@EnableWebMvcSecurity
@ComponentScan
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsServiceImpl userDetailsService;
/**
* Instantiates a new web security config.
*/
public WebSecurityConfig() {
super();
}
/**
* {@inheritDoc}
* @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.web.builders.HttpSecurity)
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login", "/logoffUser", "/sessionExpired", "/error", "/unauth", "/redirect", "*support*").permitAll()
.anyRequest().authenticated().and().rememberMe().and().httpBasic()
.and()
.csrf()
.disable().logout().deleteCookies("JSESSIONID").logoutSuccessUrl("/logoff").invalidateHttpSession(true);
}
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
}
}
패키지는 다음과 같습니다.
Application
수업은-com.nervy.dialer
Datasource
수업은-com.nervy.dialer.common
- 엔터티 클래스는-
com.nervy.dialer.domain
- 서비스 클래스는-
com.nervy.dialer.domain.service.impl
- 컨트롤러는-
com.nervy.dialer.spring.controller
- 리포지토리 클래스는-
com.nervy.dialer.spring.jpa.repository
WebSecurityConfig
~에있다com.nervy.dialer.spring.security
감사
답변
나는 교체 @ComponentScan
로 생각@ComponentScan("com.nervy.dialer.domain")
가 합니다.
편집하다 :
샘플 애플리케이션 을 추가했습니다BoneCP와 풀링 된 데이터 소스 연결을 설정하는 방법을 보여주는 을 .
응용 프로그램은 당신과 같은 구조를 가지고 있습니다. 이것이 구성 문제를 해결하는 데 도움이되기를 바랍니다.
답변
Spring Boot 진입 점 클래스에서 @EntityScan 을 사용하여 엔티티의 위치를 구성하십시오 .
2016 년 9 월 업데이트 : Spring Boot 1.4+의 경우 : … boot.orm.jpa.EntityScan은 Spring Boot 1.4에서 사용되지 않으므로
대신
사용하십시오 .org.springframework.boot.autoconfigure.domain.EntityScan
org.springframework.boot.orm.jpa.EntityScan
답변
다음을 모두 추가하십시오. 응용 프로그램에서 Tomcat과 잘 작동합니다.
@EnableJpaRepositories("my.package.base.*")
@ComponentScan(basePackages = { "my.package.base.*" })
@EntityScan("my.package.base.*")
스프링 부트를 사용하고 있으며 내장 된 Tomcat을 사용할 때 제대로 작동 @EntityScan("my.package.base.*")
했지만 외부 Tomcat에 앱을 배포하려고 할 때 not a managed type
엔티티에 오류 가 발생했습니다.
답변
필자의 경우 문제는 주석으로 엔티티 클래스에 @javax.persistence.Entity
주석을 달지 않았기 때문 입니다. 도!
//The class reported as "not a amanaged type"
@javax.persistence.Entity
public class MyEntityClass extends my.base.EntityClass {
....
}
답변
다른 프로젝트에서 지속성 구성을 복사하여 붙여 넣은 경우 EntityManagerFactory에서 수동으로 패키지를 설정해야합니다 .
@Bean
public EntityManagerFactory entityManagerFactory() throws PropertyVetoException {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory;
factory = new LocalContainerEntityManagerFactoryBean();
factory.setPackagesToScan("!!!!!!misspelled.package.path.to.entities!!!!!");
//...
}
답변
@EntityScan 을 사용할 수 있습니다 어노테이션을 하고 모든 jpa 엔티티를 스캔하기위한 엔티티 패키지를 제공 . @SpringBootApplication 주석을 사용한 기본 애플리케이션 클래스에서이 주석을 사용할 수 있습니다.
예 :
@EntityScan ( “com.test.springboot.demo.entity”)
답변
도메인 클래스에 @Entity를 추가하는 것을 잊지 마십시오