Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 在spring boot应用程序中,实现jparepository的Serviceclass始终返回null_Hibernate_Jpa_Spring Data Jpa - Fatal编程技术网

Hibernate 在spring boot应用程序中,实现jparepository的Serviceclass始终返回null

Hibernate 在spring boot应用程序中,实现jparepository的Serviceclass始终返回null,hibernate,jpa,spring-data-jpa,Hibernate,Jpa,Spring Data Jpa,我不熟悉java和spring boot。 我创建了一个简单的spring应用程序,它使用JPArepository从数据库中获取学生详细信息。 以下是studentDetais实体: package com.example.webcustomertracker.entity; 导入javax.persistence.Entity; 导入javax.persistence.GeneratedValue; 导入javax.persistence.GenerationType; 导入javax.p

我不熟悉java和spring boot。 我创建了一个简单的spring应用程序,它使用JPArepository从数据库中获取学生详细信息。 以下是studentDetais实体:

package com.example.webcustomertracker.entity;
导入javax.persistence.Entity;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;
导入javax.persistence.Table;
@实体
@表(name=“StudentDetails”)
公开课学生详情{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人整数学生ID;
私有字符串名称;
私家姓;
私人城市;
公共学生详细信息(){}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
名称=名称;
}
公共字符串getNames(){
返回姓氏;
}
public void setSurname(字符串姓氏){
姓=姓;
}
公共字符串getCity(){
回归城市;
}
公共城市(字符串城市){
城市=城市;
}
公共学生详细信息(字符串名称、字符串姓氏、字符串城市){
名称=名称;
姓=姓;
城市=城市;
}
@凌驾
公共字符串toString(){
return“StudentDetails[Name=“+Name+”,姓氏=“+Name+”,City=“+City+”];
}

}
控制器层中的自动连线

假设您有一个名为IndexController的控制器 自动接线

比如说

    StudentDetailsService studentService;
    @Autowired
    public IndexController(StudentDetailsService studentService){
Optional<StudentDetails> objStudent = new studentService.getTheStudentDetails(11);
}
studentdetails服务studentService;
@自动连线
公共索引控制器(学生详细信息服务学生服务){
可选objStudent=newstudentservice.getTheStudentDetails(11);
}

另一个解决方案是

    @SpringBootApplication
public class WebCustomerTrackerApplication {

    @Autowired
    private StudentDetailsService studentDetailsService;

    public Optional<StudentDetails> getTheStudentDetails(int id) {
        return studentDetailsService.getStudentDetails(id);
    }

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(WebCustomerTrackerApplication.class, args); 
        Thread.sleep(1500);
        Optional<StudentDetails> objStudent = new WebCustomerTrackerApplication().getTheStudentDetails(11);

    }

}
@springboot应用程序
公共类WebCustomerTrackerApplication{
@自动连线
私人学生详情服务学生详情服务;
公共可选getTheStudentDetails(int-id){
return studentdetails服务.getStudentDetails(id);
}
公共静态void main(字符串[]args)引发InterruptedException{
run(WebCustomerTrackerApplication.class,args);
睡眠(1500);
可选objStudent=new WebCustomerTrackerApplication().getTheStudentDetails(11);
}
}
在这里,它将等待应用程序启动(所有组件都已加载)。
此时,由于服务对象已创建,您将不会得到空指针异常。

您需要自动连接StudentDetailsServiceDid。。。。。。。。。。。还是一样的问题。所以你们的意思是,我不能在主方法中使用服务。JPA数据将仅与控制器一起使用?您可以使用,但会得到Nullpointerexception。因为您在Application.run()之后立即使用服务,此时服务对象尚未创建。它是依赖项注入(构造函数级别)。因为控制器中的构造函数是需要服务对象的。因此,在创建控制器时,它正在实例化服务。一旦开发了控制器,我该如何调用控制器?一旦启动应用程序,就会调用Main方法。如何调用控制器?您是否在应用程序中使用控制器?