Home > Mobile >  Spring Boot says my entity is not managed
Spring Boot says my entity is not managed

Time:01-12

I'm trying to teach myself spring boot and I have read and watched multiple articles/videos

I feel like I am doing this right but clearly i am not!

My file structure

enter image description here

I have the most simple code i can right now with multiple variations attempted to make this work

Application

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

}


Controller

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";
    }
}


Entity

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

Repository

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> {

}


Service

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

I have looked at examples and samples and videos and articles

I think it is something small i am missing but i dont know what it is

The error is :

Error creating bean with name 'packageAssortmentRepository' defined in com.example.learning.PackageAssortment.PackageAssortmentRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.learning.PackageAssortment.PackageAssortment

CodePudding user response:

In your Service class, use the autowired annotation over the repository object as below and it works fine.

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

}

CodePudding user response:

Add an annotation @EnableJpaRepositories(basePackages="com.example.learning") in your Application class.

  • Related