简体   繁体   中英

Spring JPA - Why is my findAll returning null when there is data in my database?

I have a crudrepository for an entity that I am attempting to query. The entity in question is a mapping of two other entities (two foreign keys) to another column of values. The "middle table" is RoleMapSkill and uses the id of tables role and skill as foreign keys. When I call the findAll method in the crud repository I receive a null pointer (see below).

I know there is data in the database, but for some reason it is not being returned.

Please see below my entity class, repository and controller;

java.lang.NullPointerException: null
    at main.SearchController.getRoleSkills(SearchController.java:52) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_241]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_241]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_241]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_241]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.29.jar:9.0.29]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526) [tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367) [tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860) [tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1591) [tomcat-embed-core-9.0.29.jar:9.0.29]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.29.jar:9.0.29]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_241]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_241]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.29.jar:9.0.29]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_241]

Entity;

package main;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;

@Entity
public class RoleMapSkill {
    @Id
    private String mapId;
    @OneToOne
    Role role;
    @OneToOne
    Skill skill;

    private String rating;
//  
//  public RoleMapSkill(){
//      
//    }
    public RoleMapSkill(String Id, Role idRole, Skill idSkill, String rating){
        this.mapId=Id;
        this.role=idRole;
        this.skill=idSkill;
        this.rating=rating;
    }
    public String getIdRoleSkillRatings() {
        return mapId;
    }
    public void setIdRoleSkillRatings(String idRoleSkillRatings) {
        this.mapId = idRoleSkillRatings;
    }
    public Role getIdRole() {
        return role;
    }
    public void setIdRole(Role idRole) {
        this.role = idRole;
    }
    public Skill getIdSkill() {
        return skill;
    }
    public void setIdSkill(Skill idSkill) {
        this.skill = idSkill;
    }
    public String getRating() {
        return rating;
    }
    public void setRating(String rating) {
        this.rating = rating;
    }

}

Repo;

package main;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RoleMapSkillRepository extends CrudRepository<RoleMapSkill, Integer> {

    List<RoleMapSkill> findAll();
    @Query(value = "SELECT t FROM RoleMapSkill t RIGHT JOIN FETCH t.role where t.role = tester")            
    Optional<List<RoleMapSkill>> test(String tester);
//     Optional<List<RoleMapSkill>> findAllByroleIs(String roleId);


    }

Controller

package main;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SearchController {
      public EmployeeRepository employeeRepository;
      public RoleRepository roleRepository; 
      public SkillRepository skillRepository;   
      public RoleMapSkillRepository roleMapSkillRepository;

      @Autowired
        public void  employeeRepository(EmployeeRepository employeeRepository){
            this.employeeRepository = employeeRepository;
        }
      @Autowired
        public void  roleRepository(RoleRepository roleRepository){
            this.roleRepository = roleRepository;
        }
      @Autowired
        public void  skillRepository(SkillRepository skillRepository){
            this.skillRepository = skillRepository;
        }
//    @Autowired
//      public void  roleSkillRatingsRepository(roleSkillRatingsRepository roleSkillRatingsRepository){
//          this.roleSkillRatingsRepository = roleSkillRatingsRepository;
//      }


      @ResponseBody
        public List<Employee> getAllemployees() {
            System.out.println(employeeRepository.findAll());
            return employeeRepository.findAll();
        }

      @GetMapping("/test") 
      @ResponseBody
        public Map<String,Integer> getRoleSkills(String roleName) {
          //    List<Role> currentRole=roleRepository.findAll();
            Optional<Role> currentRole=roleRepository.findByroleNameIs("SWEM");
            String roleId=currentRole.get().getRoleId();
        //  Optional<List<RoleMapSkill>> skills = roleMapSkillRepository.test(roleId);
        //  List<RoleMapSkill> test=roleMapSkillRepository.findAll();
       //   System.out.print(test.get(0).getRating());
            List<RoleMapSkill> test=roleMapSkillRepository.findAll();
            System.out.print(test.get(0).getIdRoleSkillRatings());
            Optional<List<RoleMapSkill>> skills = roleMapSkillRepository.test(roleId);
            System.out.println(skills.get().get(0).getIdSkill());
//          System.out.print(currentRole.get(0).getRoleId());
//          System.out.print(currentRole.get(0).getRoleName());
//          System.out.print(currentRole.get(1).getRoleId());
//          System.out.print(currentRole.get(1).getRoleName());
        //  roleSkillRatingsRepository.findAllById(Integer.valueOf(currentRole.getIdRole()));
            return null;
        }
}

You have to remove the findAll() method from RoleMapSkillRepository . Because it deactivates the default one. When you extends the CrudRepository you have to add Entity object and Entity object's Id data type. Your RoleMapSkill entity Id data type is String . So you have to change RoleMapSkillRepository as below:

@Repository
public interface RoleMapSkillRepository extends CrudRepository<RoleMapSkill, String> {

@Query(value = "SELECT t FROM RoleMapSkill t RIGHT JOIN FETCH t.role where t.role = tester")            
Optional<List<RoleMapSkill>> test(String tester);
//     Optional<List<RoleMapSkill>> findAllByroleIs(String roleId);
}

Use @Autowired annotations inside controller as below:

@Controller
public class SearchController {
    @Autowired
    public EmployeeRepository employeeRepository;
    @Autowired
    public RoleRepository roleRepository;
    @Autowired
    public SkillRepository skillRepository;
    @Autowired
    public RoleMapSkillRepository roleMapSkillRepository;

    //Rest of the code
}

When one inspects the code presented closely, one will find that there is no injection point (setter) for RoleMapSkillRepository . Thus, the NullPointerException arises when findAll() is called on roleMapSkillRepository since the reference is null .

To prevent this from happening again, I would suggest switching to constructor-based injection and set all injected fields to final :

@Controller
public class SearchController {
     private final EmployeeRepository employeeRepository;
     private final RoleRepository roleRepository; 
     private final SkillRepository skillRepository;   
     private final RoleMapSkillRepository roleMapSkillRepository;

    @Inject
    public SearchController(
            final EmployeeRepository employeeRepository,
            final RoleRepository roleRepository,
            final SkillRepository skillRepository,
            RoleMapSkillRepository roleMapSkillRepository) {
        this.employeeRepository = employeeRepository;
        this.roleRepository = roleRepository;
        this.skillRepository = skillRepository;
        this.roleMapSkillRepository = roleMapSkillRepository;
    }
    ...
}

This way, if a field is not injected, the program will not compile.


A comment on your code: I would suggest using JSR-330's @Inject -annotation .

please check if data is available or not before getting from Optional

Optional<> class isPresent() method

if(currentRole.isPresent()){
   String roleId=currentRole.get().getRoleId();
}

List<RoleMapSkill> test=roleMapSkillRepository.findAll();
   if(test != null && !test.isEmpty()){
     System.out.print(test.get(0).getIdRoleSkillRatings());
   }


Optional<List<RoleMapSkill>> skills = roleMapSkillRepository.test(roleId);
   if(skills.isPresent() && !skills.get().isEmpty()){
     System.out.println(skills.get().get(0).getIdSkill());
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM