1
votes

I am getting following error in my code

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'locationServiceImpl': Unsatisfied dependency expressed through method 'setLocationrepo' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'locationRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.logan.location.entities.Location

This is my repository Interface

package com.logan.location.repos;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.logan.location.entities.Location;

@Repository
public interface LocationRepository extends JpaRepository<Location, Integer> {

}

This is my Service Interface

package com.logan.location.service;

import java.util.List;
import org.springframework.stereotype.Service;
import com.logan.location.entities.Location;

@Service
public interface LocationService {
    Location saveLocation(Location location);
    Location updateLocation(Location location);
    void deleteLocation(Location location);
    Location getLocationById(int id);
    List<Location> getAllLocations();
}

This is my serviceImpl

package com.logan.location.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.logan.location.entities.Location;
import com.logan.location.repos.LocationRepository;

@Service
public class LocationServiceImpl implements LocationService {

    private LocationRepository locationrepo;
    @Autowired
    public void setLocationrepo(LocationRepository locationrepo) {
        this.locationrepo = locationrepo;
    }

    public Location saveLocation(Location location) {
        // TODO Auto-generated method stub
        return locationrepo.save(location);
    }


    public Location updateLocation(Location location) {
        // TODO Auto-generated method stub
        return locationrepo.save(location);
    }


    public void deleteLocation(Location location) {
        // TODO Auto-generated method stub
        locationrepo.delete(location);
    }


    public Location getLocationById(int id) {
        // TODO Auto-generated method stub
        return locationrepo.findById(id).get();
    }


    public List<Location> getAllLocations() {
        // TODO Auto-generated method stub
        return locationrepo.findAll();
    }


    public LocationRepository getLocationrepo() {
        return locationrepo;
    }
}

And this is my entity Class

package com.logan.location.entities;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Location {

    @Id
    private int id;
    private String code;
    private String name;
    private String type;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

This is the starter class

package com.logan.location;

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;

@EntityScan("com.logan.location.entities")
@EnableJpaRepositories(basePackages = {"com.logan.location.repos"})
@SpringBootApplication
public class LocationApplication {

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

It is showing my location entity class is unmanaged ,I have tried various answers but its not working ,any help??

2
Remove @EntityScan and remove @EnableJpaRepositories. - M. Deinum
Doesnt works @M.Deinum - user10127693
Do you mess around with @Configuration classes yourself? - M. Deinum
I didnt get you?? @M.Deinum - user10127693
Doyou have classes annotated with @Configuration - M. Deinum

2 Answers

1
votes

Remove the @Repository annotation before LocationRepository. There is no need to add it.

public interface LocationRepository extends JpaRepository<Location, Integer> {
}

Also remove @EntityScan("com.logan.location.entities") and @EnableJpaRepositories(basePackages = {"com.logan.location.repos"})

@SpringBootApplication
public class LocationApplication {

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

Add location repository bean like this:

@Service
public class LocationServiceImpl implements LocationService {

    private LocationRepository locationrepo;

    public LocationServiceImpl(LocationRepository locationrepo){
        this.locationrepo = locationrepo;
    }
}

Try with this.

-4
votes

Please add @Configuration and @ComponentScan annotations in your LocationApplication class. And also you are missing the @Column annotaions in the entity class as well and please autowire the service properly.

@Autowired 
private LocationRepository locationrepo;