Spring Boot Sping Boot 指出我的实体未被托管

rkkpypqq  于 2023-01-13  发布在  Spring
关注(0)|答案(3)|浏览(171)

我正在努力自学春靴,我已经阅读和观看了多篇文章/视频
我觉得我这样做是对的,但显然我不是!
我的文件结构

我有最简单的代码,我可以现在与多种变化试图使这项工作
应用

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();
    }
}

我看了很多例子、视频和文章
我想这是我错过的一些小东西,但我不知道它是什么
错误为:
创建在com中定义的名为"packageAssortmentRepository"的Bean时出错。example. learning. PackageAssortment. PackageAssortmentRepository在JpaRepositoriesRegistrator上声明的@EnableJpaRepositories中定义。不是托管类型:类别com.示例.学习. Package 产品组合. Package 产品组合

piv4azn7

piv4azn71#

在Service类中,在存储库对象上使用autowired注解,如下所示,它可以正常工作。

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();
}

}

ql3eal8s

ql3eal8s2#

在Application类中添加注解@EnableJpaRepositories(basePackages="com.example.learning")

wlzqhblo

wlzqhblo3#

在进一步的研究和更多的关注细节,我使用旧教程
将注解包从Javax.persistence -〉更改为jakarta.persistence
答案就是
它们看起来是同一个包裹

相关问题