私はHibernateでマッピングのための2つのクラスを作成しました:spring + hibernateプロジェクトのmysql-databaseからオブジェクトを取得できない?
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データベースのテーブルからオブジェクトを取得しようとすると:
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);
}
0123中
エラー:
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)
最初の1が動作しているが、第2 "StudentRepository1は" トン をdoesnのどうすれば修正できますか?あなたに感謝します。
正確には2番目の方法は機能しません。 –
あなたは何を得ているのですか? – aribeiro
私はそれを修正しました – ahmedhelali100