简体   繁体   English

java.lang.IllegalArgumentException:不是托管类型:class com.modernmail.model.Group

[英]java.lang.IllegalArgumentException: Not a managed type: class com.modernmail.model.Group

I am using Spring Boot 3.0.我正在使用 Spring Boot 3.0。 I think that I have a configuration error wrt my package structure + annotations, but can't quite figure it out.我认为我的 package 结构 + 注释存在配置错误,但无法完全弄清楚。 There are similar questions on here, but I've tried so many variations of the solutions and still scratching my head.这里有类似的问题,但我已经尝试了很多不同的解决方案,但仍然摸不着头脑。 Thanks in advance for the help!先谢谢您的帮助!

The error I am seeing:我看到的错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'groupController' defined in URL [jar:file:/workspace/modernmail-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/modernmail/web/GroupController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'groupRepository' defined in com.modernmail.model.GroupRepository defined in @EnableJpaRepositories declared on ModernMailApplication: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.modernmail.model.Group

My application:我的应用程序:

package com.modernmail;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@ComponentScan(basePackages = {"com.modernmail", "com.modernmail.model"})
@EntityScan(basePackages = {"com.modernmail", "com.modernmail.model"})
@EnableJpaRepositories("com.modernmail.model")
@SpringBootApplication
public class ModernMailApplication {

    public static void main(String[] args) {
        SpringApplication.run(ModernMailApplication.class, args);
    }

}

My initializer Component:我的初始化组件:

package com.modernmail;

import com.modernmail.model.Event;
import com.modernmail.model.Group;
import com.modernmail.model.GroupRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.util.Collections;
import java.util.stream.Stream;

@Component
class Initializer implements CommandLineRunner {

    private final GroupRepository repository;

    public Initializer(GroupRepository repository) {
        this.repository = repository;
    }
    ...

My repository:我的存储库:

package com.modernmail.model;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface GroupRepository extends JpaRepository<Group, Long> {
    Group findByName(String name);
}

My entity:我的实体:

package com.modernmail.model;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

import jakarta.persistence.*;
import java.util.Set;

@Data
@NoArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(name = "user_group")
public class Group {

    @Id
    @GeneratedValue
    private Long id;
    @NonNull
    private String name;
    private String address;
    private String city;
    private String stateOrProvince;
    private String country;
    private String postalCode;
    @ManyToOne(cascade=CascadeType.PERSIST)
    private User user;

    @OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
    private Set<Event> events;
}

My controller:我的 controller:

package com.modernmail.web;

import com.modernmail.model.Group;
import com.modernmail.model.GroupRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Optional;

@RestController
@RequestMapping("/api")
class GroupController {

    private final Logger log = LoggerFactory.getLogger(GroupController.class);
    private GroupRepository groupRepository;

    public GroupController(GroupRepository groupRepository) {
        this.groupRepository = groupRepository;
    }

    @GetMapping("/groups")
    Collection<Group> groups() {
        return groupRepository.findAll();
    }

    @GetMapping("/group/{id}")
    ResponseEntity<?> getGroup(@PathVariable Long id) {
        Optional<Group> group = groupRepository.findById(id);
        return group.map(response -> ResponseEntity.ok().body(response))
                .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @PostMapping("/group")
    ResponseEntity<Group> createGroup(@Valid @RequestBody Group group) throws URISyntaxException {
        log.info("Request to create group: {}", group);
        Group result = groupRepository.save(group);
        return ResponseEntity.created(new URI("/api/group/" + result.getId()))
                .body(result);
    }

    @PutMapping("/group/{id}")
    ResponseEntity<Group> updateGroup(@Valid @RequestBody Group group) {
        log.info("Request to update group: {}", group);
        Group result = groupRepository.save(group);
        return ResponseEntity.ok().body(result);
    }

    @DeleteMapping("/group/{id}")
    public ResponseEntity<?> deleteGroup(@PathVariable Long id) {
        log.info("Request to delete group: {}", id);
        groupRepository.deleteById(id);
        return ResponseEntity.ok().build();
    }
}

My pom.xml (commented out items are things I've tried that also didn't work):我的 pom.xml(注释掉的项目是我试过但也没有用的东西):

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>jakarta.persistence</groupId>
            <artifactId>jakarta.persistence-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.hibernate</groupId>-->
<!--            <artifactId>hibernate-core</artifactId>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>org.hibernate.javax.persistence</groupId>-->
<!--            <artifactId>hibernate-jpa-2.1-api</artifactId>-->
<!--            <version>1.0.0.Final</version>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>javax.xml.bind</groupId>-->
<!--            <artifactId>jaxb-api</artifactId>-->
<!--            <version>2.3.0</version>-->
<!--        </dependency>-->

You need to remove @RequiredArgsConstructor from entity, AllArgsConsructor NoArgsConstructor are enough.您需要从实体中删除 @RequiredArgsConstructor,AllArgsConsructor NoArgsConstructor 就足够了。

Delete jakarta.persistence-api, hibernate-core, hibernate-entitymanager.删除 jakarta.persistence-api、hibernate-core、hibernate-entitymanager。 Code doesn't contain hibernate imports, jakarta, it seems, included into boot by default.代码不包含 hibernate 导入,雅加达似乎默认包含在引导中。

One thing i really don't understand that why are you initizing you bean using construtor?一件事我真的不明白你为什么要使用构造函数初始化你的bean? You should autowire your beans and let them manage by spring container.你应该自动装配你的 beans 并让它们由 spring 容器管理。 Remove below code from your controller for initializing the repository and use autowire it like beolw.从您的 controller 中删除以下代码以初始化存储库,并像 beolw 一样使用自动装配。

Remove it -去掉它 -

 private GroupRepository groupRepository;

public GroupController(GroupRepository groupRepository) {
    this.groupRepository = groupRepository;
}

And Add it -并添加它 -

@Autowired private GroupRepository groupRepository; @Autowired private GroupRepository groupRepository;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalArgumentException:不是托管类型:class - java.lang.IllegalArgumentException: Not a managed type: class java.lang.IllegalArgumentException:不是托管类型:类 com.SportyShoe.Entity.Shoe - java.lang.IllegalArgumentException: Not a managed type: class com.SportyShoe.Entity.Shoe java.lang.IllegalArgumentException:不是托管类型:class models.User - java.lang.IllegalArgumentException: Not a managed type: class models.User java.lang.IllegalArgumentException:不是托管类型:classTestEntity - java.lang.IllegalArgumentException: Not an managed type: classTestEntity java.lang.IllegalArgumentException问题:不是托管类型 - Problem with java.lang.IllegalArgumentException: Not a managed type java.lang.IllegalArgumentException:不是托管类型: - java.lang.IllegalArgumentException: Not a managed type: java.lang.IllegalArgumentException:来自托管类型[T]的属性[A]不存在 - java.lang.IllegalArgumentException: The attribute [A] from the managed type [T] is not present 在 springboot 应用程序 java.lang.IllegalArgumentException 中出现此错误:不是托管类型 - getting this error in springboot application java.lang.IllegalArgumentException: Not a managed type 当实体类和Spring数据存储库位于同一包中时,“ java.lang.IllegalArgumentException:不是托管类型…” - “java.lang.IllegalArgumentException: Not a managed type …” when entity class and Spring data repository in same package java.lang.IllegalArgumentException:在Spring Boot应用程序中不是托管类型 - java.lang.IllegalArgumentException: Not a managed type in spring boot app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM