我创建了两个用于使用hibernate进行映射的类:
package com.pcd.ahmed.model;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
@Entity
@Table(name="etudiants")
public class Etudiant {
@Id
@GeneratedValue
@Size(min=4, max=6)
private String student_ID;
@NotEmpty
@Size(min=4, max=45)
private String firstname;
@NotEmpty
@Size(min=4, max=45)
private String lastname;
@NotNull
@Past
@DateTimeFormat(pattern="MM/dd/yyyy")
private Date date_birth;
@NotEmpty
@Size(min=3, max=4)
private String class_ID;
}
和
package com.pcd.ahmed.model;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
@Entity
@Table(name="student")
public class Student {
@Id
@GeneratedValue
private Long id;
@NotEmpty
@Size(min=4, max=20)
private String userName;
@NotEmpty
private String firstName;
@NotEmpty
private String lastName;
@NotEmpty
@Size(min=4, max=8)
private String password;
}
但是当我尝试使用存储库从mysql-database表中检索对象时:
package com.pcd.ahmed.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.pcd.ahmed.model.Student;
@Repository("studentRepository")
public interface StudentRepository extends JpaRepository<Student, Long> {
@Query("select s from Student s where s.userName = :userName")
Student findByUserName(@Param("userName") String userName);
}
和
package com.pcd.ahmed.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.pcd.ahmed.model.Etudiant;
@Repository("studentRepository1")
public interface StudentRepository1 extends JpaRepository<Etudiant, String> {
@Query("select s from etudiants s where s.student_ID = :student_ID")
Etudiant findBystudent_ID(@Param("student_ID") String student_ID);
}
错误:
package com.pcd.ahmed.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.pcd.ahmed.model.Etudiant;
import com.pcd.ahmed.model.Student;
import com.pcd.ahmed.repository.StudentRepository;
import com.pcd.ahmed.repository.StudentRepository1;
@Service("studentService")
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentRepository studentRepository;
private StudentRepository1 studentRepository1;
@Transactional
public Student save(Student student) {
return studentRepository.save(student);
}
public Student profileget(String userName, String password) {
Student stud = studentRepository.findByUserName(userName);
if(stud != null && stud.getPassword().equals(password) && stud.getroles().equals("Parents")) {
return stud;
}
if(stud != null && stud.getPassword().equals(password) && stud.getroles().equals("Administrateur")) {
return stud;
}
if(stud != null && stud.getPassword().equals(password) && stud.getroles().equals("Enseignant")) {
return stud;
}
if(stud != null && stud.getPassword().equals(password) && stud.getroles().equals("Infirmiere")) {
return stud;
}
return null;
}
public Etudiant studentget(String student_ID) {
Etudiant etud = studentRepository1.findBystudent_ID(student_ID);
return etud;
}
public int findByLogin(String userName, String password,Student stud) {
stud = studentRepository.findByUserName(userName);
if(stud != null && stud.getPassword().equals(password) && stud.getroles().equals("Parents")) {
return 1;
}
if(stud != null && stud.getPassword().equals(password) && stud.getroles().equals("Administrateur")) {
return 2;
}
if(stud != null && stud.getPassword().equals(password) && stud.getroles().equals("Enseignant")) {
return 3;
}
if(stud != null && stud.getPassword().equals(password) && stud.getroles().equals("Infirmiere")) {
return 4;
}
return 0;
}
public boolean findByUserName(String userName) {
Student stud = studentRepository.findByUserName(userName);
if(stud != null) {
return true;
}
return false;
}
}
当我调用此函数时:
public Etudiant studentget(String student_ID) {
Etudiant etud = studentRepository1.findBystudent_ID(student_ID);
return etud;
}
它说:
org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [studentHibernateServlet] in context with path [/StudentEnrollmentWithSpring] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.pcd.ahmed.service.StudentServiceImpl.studentget(StudentServiceImpl.java:42)
at com.pcd.ahmed.controller.StudentController.login(StudentController.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
第一个是工作但是第二个&#34; StudentRepository1&#34;没有 那我怎么解决呢?谢谢你
答案 0 :(得分:0)
我认为你的HQL中只有一个拼写错误。而不是etudiants
,请尝试将其更改为Etudiant
@Repository("studentRepository1")
public interface StudentRepository1 extends JpaRepository<Etudiant, String> {
// "Etudiant" was previously "etudiants"
@Query("select s from Etudiant s where s.student_ID = :student_ID")
Etudiant findBystudent_ID(@Param("student_ID") String student_ID);
}
<强>更新强> 您在上面的问题中仍然遗漏了@Autowired
@Autowired
private StudentRepository studentRepository;
private StudentRepository1 studentRepository1;
需要:
@Autowired
private StudentRepository studentRepository;
@Autowired
private StudentRepository1 studentRepository1;
因为你一直在编辑它,所以关注你的问题也很困惑。
答案 1 :(得分:0)
您没有在StudentServiceImpl
中连接studentRepository1我在IDE中重现了错误。请更新下面看到的这个块,然后一切都会好的。
@Autowired
private StudentRepository studentRepository;
@Autowired
private StudentRepository1 studentRepository1;