spring-data:不是托管类型:类 java.lang.Object
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23812458/
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-data: Not an managed type: class java.lang.Object
提问by daydreamer
I started using spring-data
project yesterday and was trying to add a test to repository I have created.
我spring-data
昨天开始使用项目,并试图向我创建的存储库添加测试。
The project structure looks like
项目结构看起来像
persistence/pom.xml
src/main/java/
ApplicationConfig
BaseRepository
Network
src/main/test/BaseRepositoryTest
The ApplicationConfig
looks like
的ApplicationConfig
模样
@Configuration
@ComponentScan
@EnableJpaRepositories
public class ApplicationConfig {
@Bean
public DataSource dataSource() {
final EmbeddedDatabaseBuilder embeddedDatabaseBuilder = new EmbeddedDatabaseBuilder();
embeddedDatabaseBuilder.setType(EmbeddedDatabaseType.H2);
return embeddedDatabaseBuilder.build();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setDatabase(Database.H2);
jpaVendorAdapter.setGenerateDdl(true);
final LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
localContainerEntityManagerFactoryBean.setPackagesToScan(getClass().getPackage().getName());
localContainerEntityManagerFactoryBean.setDataSource(dataSource());
return localContainerEntityManagerFactoryBean;
}
}
The BaseRepository
looks like
的BaseRepository
模样
import org.springframework.data.repository.Repository;
public interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID> {
List<T> findAll();
}
The Network
is an Entity class looks like
这Network
是一个实体类看起来像
@Entity
public class Network extends com.org.comma.persistence.Entity {
private final long networkId;
private final String name;
private final boolean active;
private final DateTime createdAt;
private String createdBy;
private DateTime updatedAt;
private String updatedBy;
...
}
and BaseRepositoryTest
looks like
和BaseRepositoryTest
看起来像
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ApplicationConfig.class, BaseRepository.class})
public class BaseRepositoryTest {
@Autowired
BaseRepository<Network, Long> baseRepository;
@Test
public void testAllNetworks() {
final List<Network> networks = baseRepository.findAll();
assertTrue(networks.isEmpty());
}
}
My pom.xml
looks like
我的pom.xml
样子
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.5.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
When I run mvn clean install
, I see error as
当我运行时mvn clean install
,我看到错误为
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:319)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:212)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access@ContextConfiguration(classes = {ApplicationConfig.class, BaseRepository.class}, loader=AnnotationConfigContextLoader.class)
0(ParentRunner.java:42)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object
Please help me identify what am I doing wrong here, I am new to this project
请帮我确定我在这里做错了什么,我是这个项目的新手
回答by caburse
Try the following change
尝试以下更改
interface NetworkRepository extends BaseRepository<Network, Long> { … }
回答by Oliver Drotbohm
The type the repository manages has to be known at bootstrap time, which means that you need to actually create a concrete repository like this:
存储库管理的类型必须在引导时知道,这意味着您需要实际创建一个具体的存储库,如下所示:
@NoRepositoryBean
public interface BaseRespository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {}
There are a few other glitches in the code you showed:
您展示的代码中还有其他一些小故障:
- The
Network
class doesn't have any property annotated with@Id
BaseRepository
should carry an@NoRepositoryBean
annotation to indicate it's not supposed to be instantiated at all. See the reference documentationfor details.
- 本
Network
类没有带注释的任何财产@Id
BaseRepository
应该带有@NoRepositoryBean
注释以表明它根本不应该被实例化。有关详细信息,请参阅参考文档。
回答by Frank Fu
I got the same error due to my.packagename is wrong. once I provide the correct package name, it works fine.
由于my.package名称错误,我遇到了同样的错误。一旦我提供了正确的包名,它就可以正常工作。
@EnableJpaRepositories('my.package')
@EnableJpaRepositories(' my.package')
回答by AixNPanes
This also happens if you have one repository interface that extends another and you forgot the @NoRepositoryBean on the super-interface.
如果您有一个存储库接口扩展另一个存储库接口并且忘记了超级接口上的 @NoRepositoryBean,也会发生这种情况。
回答by buddha
I have faced the same issue, and resolved by adding @NoRepositoryBean
我遇到了同样的问题,并通过添加 @NoRepositoryBean 解决了
@Entity
class <classname>
{
//model variables & getters Setters
}
回答by NameNotFoundException
I was doing below mistake:
我在做以下错误:
At the time of creating a factory, I have passed repo package instead of entity package.
在创建工厂时,我通过了 repo 包而不是实体包。
回答by Bharat Bhamare
I think u miss @Entity Annotation on Model Class
我想你想念模型类上的@Entity Annotation
Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]
回答by Zeusox
Another issue might be due to forgetting to annotate your class with @Entity
as that is why Spring Boot sees it as Not a managed type
另一个问题可能是由于忘记注释您的类,@Entity
这就是为什么 Spring Boot 将其视为Not a managed type
After you annotate your class it should work. However, if you get the error below:
在您注释您的课程后,它应该可以工作。但是,如果您收到以下错误:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Then make sure your class has an id with the following annotations:
然后确保您的类具有带有以下注释的 id:
@EnableJpaRepositories("Your-Package-Name-That-Includes-Your-Repository")
Another scenario could be that Spring Boot is not detecting your JPA or CrusRepository Package. Make sure you annotate your main class with the following annotation:
另一种情况可能是 Spring Boot 没有检测到您的 JPA 或 CrusRepository 包。确保使用以下注释对主类进行注释:
##代码##回答by Prasad Giri
try replacing the ID with the datatype of class or model that you are mapping with and that would be Integer ,so replace ID inJpaRepository<T,ID>
the ID with Integer and i.e will be JpaRepository<T,Integer>
尝试用您要映射的类或模型的数据类型替换 ID,这将是 Integer ,因此将 ID 中JpaRepository<T,ID>
的 ID替换为 Integer ,即JpaRepository<T,Integer>