daydreamer
daydreamer

Reputation: 91959

spring-data: Not an managed type: class java.lang.Object

I started using spring-data project yesterday and was trying to add a test to repository I have created.

The project structure looks like

persistence/pom.xml
           src/main/java/
                        ApplicationConfig
                        BaseRepository
                        Network
           src/main/test/BaseRepositoryTest

The ApplicationConfig looks like

@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

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

@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

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

<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

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$1.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$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.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

Upvotes: 15

Views: 53753

Answers (10)

S&#246;ren
S&#246;ren

Reputation: 2432

In my case, I had reversed the type arguments for CrudRepository. That is, I had written

interface FooRepository extends CrudRepository<Long, Foo>

instead of

interface FooRepository extends CrudRepository<Foo, Long>

Upvotes: 0

Prasad Giri
Prasad Giri

Reputation: 118

try replacing the ID with the datatype of class or model that you are mapping with and that would be Integer ,so replace ID in
JpaRepository<T,ID> the ID with Integer and i.e will be JpaRepository<T,Integer>

Upvotes: 0

Zeusox
Zeusox

Reputation: 8448

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

After you annotate your class it should work. However, if you get the error below:

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]

Then make sure your class has an id with the following annotations:

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)

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:

@EnableJpaRepositories("Your-Package-Name-That-Includes-Your-Repository")

Upvotes: -1

Bharat Bhamare
Bharat Bhamare

Reputation: 66

I think u miss @Entity Annotation on Model Class

    @Entity
    class <classname>
    {
       //model variables   & getters Setters   

    }

Upvotes: -1

NameNotFoundException
NameNotFoundException

Reputation: 896

I was doing below mistake:

At the time of creating a factory, I have passed repo package instead of entity package.

Upvotes: 0

buddha
buddha

Reputation: 833

I have faced the same issue, and resolved by adding @NoRepositoryBean

@NoRepositoryBean
public interface BaseRespository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {}

Upvotes: 12

AixNPanes
AixNPanes

Reputation: 1260

This also happens if you have one repository interface that extends another and you forgot the @NoRepositoryBean on the super-interface.

Upvotes: 16

Frank Fu
Frank Fu

Reputation: 778

I got the same error due to my.package name is wrong. once I provide the correct package name, it works fine.

@EnableJpaRepositories('my.package')

Upvotes: 4

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83051

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:

interface NetworkRepository extends BaseRepository<Network, Long> { … }

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 documentation for details.

Upvotes: 42

caburse
caburse

Reputation: 156

Try the following change

@ContextConfiguration(classes = {ApplicationConfig.class, BaseRepository.class}, loader=AnnotationConfigContextLoader.class)

Upvotes: 0

Related Questions