简体   繁体   中英

Getting “IllegalArgumentException: Not a managed type” When trying to create SpringFramework Bean using object that is not an Entity

In the application I am working on, I have several Entity classes. For example:

@Entity
public class Person implements Serializable {
    @Column
    private String columnA;

    public String getColumnA() {
        return columnA;
    }

    public void setColumnA(String columnA) {
        this.columnA= columnA;
    }
}

I have accompanying Repositories:

public interface PersonRepository extends CrudRepository<Person, Long> {
    Person findByPersonID(Long id);
}

Then I utilize the repositories like so:

@Autowired
private PersonRepository personRepository;

I have several Entities (eg, Person, Status, Training, Benefits, etc.)

I am needing to make a very specialized report query to the database that uses multiple joins across multiple tables. I have the query working in MySQL Workbench.

So, I created a new Repository:

@Repository
public interface ReportRepository extends CrudRepository<Report, Long> {
    @Query("SELECT ...")
    List<Report> queryReportData(String columnA, String columnB, String columnC, ...);
}

Where Report is just a POJO with the fields I need:

public class Report {
    private String columnA;
    private String columnB;
    // etc ...
}
// Getters and Setters here

My issue is when I try to use the repository like so:

@Autowired
private ReportRepository reportRepository;

I get run-time errors:

creating bean with name 'genController': Unsatisfied dependency expressed through field 'reportRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reportRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.me.Report

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reportRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.me.Report

Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.me.Report

Where genController is:

@RestController
public class GenController {
}

Granted, Report.java is not an actual @Entity as it is not "really" in the database.

So, am I going about this completely wrong, or am I kind of on the right path?

What do I need to do in order to get the data I need using my custom, cross table, query?

if Report is not an entity, you can not do that. if you are using a native query already, you can just put this method into an entity repositort. Such as person. and Autowire the personRepository instead report repository

Your Report object needs to be annotated as @Entity so it will be picked up by JPA

Without that annotation, Spring, as you can see in your stack trace, throws an exception when it tries to construct an instance of a CrudRepository because Report is not a managed object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM