I have a big problem in my diploma project and would be very glad if you guys could help me! I made a Maven Multi Module Project and have 3 \"Core-projects\"
In spring boot I get same exception by using CrudRepository because I forgot to set generic types. I want to write it here in case it helps someone.
errorneous definition:
public interface OctopusPropertiesRepository extends CrudRepository
error:
Caused by: java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
successfull definition:
public interface OctopusPropertiesRepository extends CrudRepository<OctopusProperties,Long>{
Just in case some other poor sod ends up here because they are having the same issue I was: if you have multiple data sources and this is happening with the non-primary data source, then the problem might be with that config. The data source, entity manager factory, and transaction factory all need to be correctly configured, but also -- and this is what tripped me up -- MAKE SURE TO TIE THEM ALL TOGETHER! @EnableJpaRepositories (configuration class annotation) must include entityManagerFactoryRef and transactionManagerRef to pick up all the configuration!
The example in this blog finally helped me see what I was missing, which (for quick reference) were the refs here:
@EnableJpaRepositories(
entityManagerFactoryRef = "barEntityManagerFactory",
transactionManagerRef = "barTransactionManager",
basePackages = "com.foobar.bar")
Hope this helps save someone else from the struggle I've endured!
you need check packagesToScan.
<bean id="entityManagerFactoryDB" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
<property name="dataSource" ref="dataSourceDB" />
<property name="persistenceUnitName" value="persistenceUnitDB" />
<property name="packagesToScan" value="at.naviclean.domain" />
//here
.....
If anyone is strugling with the same problem I solved it by adding @EntityScan in my main class. Just add your model package to the basePackages property.
For Controllers, @SpringBootApplication(scanBasePackages = {"com.school.controllers"})
For Respositories, @EnableJpaRepositories(basePackages = {"com.school.repos"})
For Entities, @EntityScan(basePackages = {"com.school.models"})
This will slove
"Can't Autowire @Repository annotated interface"
problem as well as
Not an managed Type
problem. Sample configuration below
package com.school.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication(scanBasePackages = {"com.school.controllers"})
@EnableJpaRepositories(basePackages = {"com.school.repos"})
@EntityScan(basePackages = {"com.school.models"})
public class SchoolApplication {
public static void main(String[] args) {
SpringApplication.run(SchoolApplication.class, args);
}
}
In my case, when using IntelliJ, I had multiple modules in the project. The main module was dependent on another module which had the maven dependencies on Spring.
The main module had Entitys and so did the second module. But when I ran the main module, only the Entitys from the second module got recognized as managed classes.
I then added Spring dependencies on the main module as well, and guess what? It recognized all the Entitys.