我有一个要查询的实体的原始存储库。所讨论的实体是其他两个实体(两个外键)到另一列值的映射。 “中间表”是RoleMapSkill,使用表角色和技能的ID作为外键。当我在crud存储库中调用findAll方法时,我收到一个空指针(见下文)。
我知道数据库中有数据,但是由于某种原因它没有被返回。
请在下面查看我的实体类,存储库和控制器;
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]
实体;
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;
}
}
回购;
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);
}
控制器
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;
}
}
答案 0 :(得分:0)
当人们仔细检查呈现的代码时,会发现RoleMapSkillRepository
没有注入点(设置者)。因此,由于引用为NullPointerException
,因此在findAll()
上调用roleMapSkillRepository
时会出现null
。
为防止再次发生这种情况,我建议切换到基于构造函数的注入,并将所有注入的字段设置为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;
}
...
}
这样,如果未注入字段,则程序将无法编译。
评论您的代码:建议您使用JSR-330's @Inject
-annotation。
答案 1 :(得分:0)
您必须从findAll()
中删除RoleMapSkillRepository
方法。因为它会停用默认值。扩展CrudRepository
时,必须添加Entity对象和Entity对象的ID数据类型。您的RoleMapSkill
实体ID数据类型为String
。因此,您必须如下更改RoleMapSkillRepository
:
@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);
}
在控制器内部使用@Autowired
注释,如下所示:
@Controller
public class SearchController {
@Autowired
public EmployeeRepository employeeRepository;
@Autowired
public RoleRepository roleRepository;
@Autowired
public SkillRepository skillRepository;
@Autowired
public RoleMapSkillRepository roleMapSkillRepository;
//Rest of the code
}
答案 2 :(得分:0)
在从“可选”获取数据之前,请检查数据是否可用
可选<> 类 isPresent()方法
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());
}