Spring boot - 不是托管类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28664064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot - Not an managed type
提问by user1578872
I use Spring boot+JPA and having a problem while starting the service.
我使用 Spring boot+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)
Here is the Application.java file,
这是 Application.java 文件,
@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
@SpringBootApplication
public class DialerApplication {
public static void main(String[] args) {
SpringApplication.run(DialerApplication.class, args);
}
}
I use UCp for connection pooling and the DataSource configuration is below,
我使用 UCp 进行连接池,数据源配置如下,
@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 Implementation,
UserDetailsService 实现,
@Service("userDetailsService")
@SessionAttributes("user")
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserService userService;
Service layer implementation,
服务层实现,
@Service
public class PhoneSettingsServiceImpl implements PhoneSettingsService {
}
The repository class,
存储库类,
@Repository
public interface PhoneSettingsRepository extends JpaRepository<PhoneSettings, Long> {
}
Entity class,
实体类,
@Entity
@Table(name = "phone_settings", catalog = "dialer")
public class PhoneSettings implements java.io.Serializable {
WebSecurityConfig class,
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());
}
}
The packages are as follows,
包裹如下,
Application
class is in -com.nervy.dialer
Datasource
class is in -com.nervy.dialer.common
- Entity classes are in -
com.nervy.dialer.domain
- Service classes are in -
com.nervy.dialer.domain.service.impl
- Controllers are in -
com.nervy.dialer.spring.controller
- Repository classes are in -
com.nervy.dialer.spring.jpa.repository
WebSecurityConfig
is in -com.nervy.dialer.spring.security
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
Thanks
谢谢
采纳答案by azizunsal
I think replacing @ComponentScan
with @ComponentScan("com.nervy.dialer.domain")
will work.
我认为替换@ComponentScan
为@ComponentScan("com.nervy.dialer.domain")
将工作。
Edit :
编辑 :
I have added a sample applicationto demonstrate how to set up a pooled datasource connection with BoneCP.
我添加了一个示例应用程序来演示如何设置与 BoneCP 的池化数据源连接。
The application has the same structure with yours. I hope this will help you to resolve your configuration problems
该应用程序的结构与您的相同。我希望这将帮助您解决您的配置问题
回答by Manish Maheshwari
Configure the location of entities using @EntityScanin Spring Boot entry point class.
在 Spring Boot 入口点类中使用@EntityScan配置实体的位置。
Update on Sept 2016: For Spring Boot 1.4+:
use org.springframework.boot.autoconfigure.domain.EntityScan
instead of org.springframework.boot.orm.jpa.EntityScan
, as ...boot.orm.jpa.EntityScan is deprecatedas of Spring Boot 1.4
2016 年 9 月更新:对于 Spring Boot 1.4+:
使用org.springframework.boot.autoconfigure.domain.EntityScan
代替org.springframework.boot.orm.jpa.EntityScan
,因为 ...boot.orm.jpa.EntityScan自 Spring Boot 1.4起已弃用
回答by manoj
Try adding All the following, In my application it is working fine with tomcat
尝试添加以下所有内容,在我的应用程序中,它与 tomcat 一起工作正常
@EnableJpaRepositories("my.package.base.*")
@ComponentScan(basePackages = { "my.package.base.*" })
@EntityScan("my.package.base.*")
I am using spring boot, and when i am using embedded tomcat it was working fine with out @EntityScan("my.package.base.*")
but when I tried to deploy the app to an external tomcat I got not a managed type
error for my entity.
我正在使用 spring boot,当我使用嵌入式 tomcat 时,它运行良好, @EntityScan("my.package.base.*")
但当我尝试将应用程序部署到外部 tomcat 时not a managed type
,我的实体出现错误。
回答by Farrukh Najmi
In my case the problem was due to my forgetting to have annotated my Entity classes with @javax.persistence.Entity annotation. Doh!
在我的情况下,问题是由于我忘记使用 @javax.persistence.Entity 注释来注释我的实体类。哦!
//The class reported as "not a amanaged type"
@javax.persistence.Entity
public class MyEntityClass extends my.base.EntityClass {
....
}
回答by Lazaruss
If you have copy-pasted the persistence configuration from another project, you must set the package in EntityManagerFactorymanually :
如果您从另一个项目复制粘贴了持久性配置,则必须手动在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!!!!!");
//...
}
回答by Nitesh Saxena
You can use @EntityScanannotation and provide your entity package for scanning all your jpa entities. You can use this annotation on your base application class where you have used @SpringBootApplication annotation.
您可以使用@EntityScan注释并提供您的实体包来扫描您的所有 jpa 实体。您可以在使用 @SpringBootApplication 注释的基础应用程序类上使用此注释。
e.g. @EntityScan("com.test.springboot.demo.entity")
例如 @EntityScan("com.test.springboot.demo.entity")
回答by Mohammad Badiuzzaman
never forget to add @Entity on domain class
永远不要忘记在域类上添加@Entity
回答by Marvo
I got this error because I stupidly wrote
我收到这个错误是因为我愚蠢地写了
public interface FooBarRepository extends CrudRepository<FooBarRepository, Long> { ...
公共接口 FooBarRepository 扩展 CrudRepository<FooBar Repository, Long> { ...
A brief explanation: One typically creates a FooBarRepository class to manage FooBar objects (often representing data in a table called something like foo_bar.) When extending the CrudRepository to create the specialized repository class, one needs to specify the type that's being managed -- in this case, FooBar. What I mistakenly typed, though, was FooBarRepository rather than FooBar. FooBarRepository is not the type (the class) I'm trying to manage with the FooBarRepository. Therefore, the compiler issues this error.
简要说明:通常会创建一个 FooBarRepository 类来管理 FooBar 对象(通常表示名为 foo_bar 之类的表中的数据。)当扩展 CrudRepository 以创建专门的存储库类时,需要指定正在管理的类型——在在这种情况下,FooBar。然而,我错误地输入的是 FooBarRepository 而不是 FooBar。FooBarRepository 不是我试图用 FooBarRepository 管理的类型(类)。因此,编译器会发出此错误。
I highlighted the mistaken bit of typing in bold. Delete the highlighted word Repository in my example and the code compiles.
我用粗体突出显示了错误的输入。删除我的示例中突出显示的单词 Repository 并编译代码。
回答by me.rkshrestha
Put this in your Application java file
把它放在你的应用程序java文件中
@ComponentScan(basePackages={"com.nervy.dialer"}) @EntityScan(basePackages="domain")
@ComponentScan(basePackages={"com.nervy.dialer"}) @EntityScan(basePackages="domain")
回答by Tamer Awad
You either missed @Entity on class definition or you have explicit component scan path and this path does not contain your class
您要么在类定义中遗漏了@Entity,要么您有显式的组件扫描路径并且此路径不包含您的类