【问题标题】:Spring Boot says my entity is not managedSpring Boot 说我的实体不受管理
【发布时间】:2023-01-12 10:34:37
【问题描述】:

我正在尝试自学 spring boot,我已经阅读并观看了多篇文章/视频

我觉得我这样做是对的,但显然我不是!

我的文件结构


-申请
-实体
-资料库
-服务
-控制器

我现在拥有最简单的代码,尝试使用多种变体来完成这项工作

应用

package com.example.learning;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import java.util.List;
 @SpringBootApplication
public class LearningApplication {

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

}


控制器

package com.example.learning;

import com.example.learning.PackageAssortment.PackageAssortmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {


    private final PackageAssortmentService service;

    @Autowired
    public TestController(PackageAssortmentService service) {
        this.service = service;
    }

    @RequestMapping("/")
    public String Hello() {

        return "Hello";
    }
}


实体

package com.example.learning;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name= "PA_ASSORTMENT", schema = "redacted_schema_name")
public class PackageAssortment {


    @Id
    @GeneratedValue
    public Long packageAssortmentId;
    // private PackageBarCode primaryPackageBarCode;
    // private PackageInformation packageInformation;
    // private PackageRatio packageRatio;
    public Date serverUpdateTimestamp;
    public char recordStatus;
    public char logicalDeleteFlag;
    public Date createdDate;
    public Date changedDate;
    public String createdBy;
    public String changedBy;
    public long createdApplicationId;
    public long createdFunctionId;
    public long changedApplicationId;
    public long changedFunctionId;
    public Long tenantBuId;
    @Column(name="package_assortment_type")
    public String assortmentType;
    // private PaConsumable paConsumable;
    // private PaSellable paSellable;
    // private PaOrderable paOrderable;
}

资料库

package com.example.learning.PackageAssortment;

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

@Repository
public interface PackageAssortmentRepository  extends JpaRepository<PackageAssortment, Long> {

}


服务

package com.example.learning.PackageAssortment;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PackageAssortmentService {


    private final PackageAssortmentRepository repository;

    @Autowired
    public PackageAssortmentService(PackageAssortmentRepository repository) {
        this.repository = repository;
    }


    public PackageAssortment GetStuf() {
        return  new PackageAssortment();
    }
}

我看过示例、样本、视频和文章

我认为这是我遗漏的小东西,但我不知道它是什么

错误是:

在 JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration 上声明的 @EnableJpaRepositories 中定义的 com.example.learning.PackageAssortment.PackageAssortmentRepository 中定义的名称“packageAssortmentRepository”创建 bean 时出错:不是托管类型:类 com.example.learning.PackageAssortment.PackageAssortment

【问题讨论】:

    标签: spring-boot spring-data-jpa


    【解决方案1】:

    在您的服务类中,如下所示在存储库对象上使用自动装配注释,它工作正常。

    package com.example.learning;
    
    import com.example.learning.PackageAssortmentRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    @Service
    public class PackageAssortmentService {
    
    @Autowired
    private final PackageAssortmentRepository repository;
    
    
    public PackageAssortmentService(PackageAssortmentRepository repository) {
        this.repository = repository;
    }
    
    
    public PackageAssortment GetStuf() {
        return  new PackageAssortment();
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 2013-05-03
      • 2021-07-12
      • 2019-01-03
      • 1970-01-01
      • 2019-03-18
      • 2015-09-01
      相关资源
      最近更新 更多