Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/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
Java 如何解决“问题”;“无法强制转换对象”;在spring boot中使用Microsoft SQL?_Java_Spring_Spring Boot - Fatal编程技术网

Java 如何解决“问题”;“无法强制转换对象”;在spring boot中使用Microsoft SQL?

Java 如何解决“问题”;“无法强制转换对象”;在spring boot中使用Microsoft SQL?,java,spring,spring-boot,Java,Spring,Spring Boot,我正在尝试使用Microsoft SQL为我的应用程序调用存储过程。但是,当我运行存储过程传回对象的内容时,它失败了。我的对象是AVSApplication,在这个类中,它有一个变量和方法列表。我尝试使用Iterable和列表,但两者都产生相同的错误。我不确定哪里出了错。我看了其他类似的StackOverflow问题,但我没有从中得到什么 错误: java.lang.ClassCastException: java.base/[Ljava.lang.Object; cannot be cast

我正在尝试使用Microsoft SQL为我的应用程序调用存储过程。但是,当我运行存储过程传回对象的内容时,它失败了。我的对象是
AVSApplication
,在这个类中,它有一个变量和方法列表。我尝试使用
Iterable
列表
,但两者都产生相同的错误。我不确定哪里出了错。我看了其他类似的StackOverflow问题,但我没有从中得到什么

错误:

java.lang.ClassCastException: java.base/[Ljava.lang.Object; cannot be cast to com.Mapping.AVSApplication
    at com.Mapping.Employeecontroller.getAll(Employeecontroller.java:33) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~
import java.util.*;

import javax.persistence.*;

@Entity
@NamedStoredProcedureQueries(value= {
        @NamedStoredProcedureQuery(name= "procedure-one", procedureName= "GetAllAppWithStatus")
})


public class AVSApplication implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String appcode;

    private String acronym;

    private String appname;

    private String sys_id;

    private String mapstatus;

    private String sdg;

    private String status;

    private String statuscode;



    //Constructor
    public AVSApplication(String appcode, String acronym, String appname, String sys_id, String mapstatus,
            String sdg, String status, String statuscode) {
        super();
        this.appcode = appcode;
        this.acronym = acronym;
        this.appname = appname;
        this.sys_id = sys_id;
        this.mapstatus = mapstatus;
        this.sdg = sdg;
        this.status = status;
        this.statuscode = statuscode;
    }




    //Getters

    public String getAppcode() {
        return appcode;
    }

    public String getAcronym() {
        return acronym;
    }

    public String getAppname() {
        return appname;
    }

    public String getSys_id() {
        return sys_id;
    }

    public String getMapstatus() {
        return mapstatus;
    }

    public String getSdg() {
        return sdg;
    }

    public String getStatus() {
        return status;
    }

    public String getStatuscode() {
        return statuscode;
    }




    //Setters

    public void setAppcode(String appcode) {
        this.appcode = appcode;
    }

    public void setAcronym(String acronym) {
        this.acronym = acronym;
    }

    public void setAppname(String appname) {
        this.appname = appname;
    }

    public void setSys_id(String sys_id) {
        this.sys_id = sys_id;
    }

    public void setMapstatus(String mapstatus) {
        this.mapstatus = mapstatus;
    }

    public void setSdg(String sdg) {
        this.sdg = sdg;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setStatuscode(String statuscode) {
        this.statuscode = statuscode;
    }


}


@Repository
public class Employeedao {
    @Autowired
    private EntityManager em;

    /**
     * Method to fetch all employees from the db.
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<AVSApplication> getAllEmployees() {
        return em.createNamedStoredProcedureQuery("procedure-one").getResultList();
    }

}

@RestController
public class Employeecontroller {


   @Autowired
   Employeedao edao;



   /**
    * Method to fetch all employees from the db.
    * @return
    */
   @RequestMapping(value= "/getall")
   public void getAll() {
      System.out.println("All objects: " + edao.getAllEmployees());

      System.out.println("Get the first item in list: " + edao.getAllEmployees().get(0).getAppcode());




   }

}

Java实体代码:

java.lang.ClassCastException: java.base/[Ljava.lang.Object; cannot be cast to com.Mapping.AVSApplication
    at com.Mapping.Employeecontroller.getAll(Employeecontroller.java:33) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~
import java.util.*;

import javax.persistence.*;

@Entity
@NamedStoredProcedureQueries(value= {
        @NamedStoredProcedureQuery(name= "procedure-one", procedureName= "GetAllAppWithStatus")
})


public class AVSApplication implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String appcode;

    private String acronym;

    private String appname;

    private String sys_id;

    private String mapstatus;

    private String sdg;

    private String status;

    private String statuscode;



    //Constructor
    public AVSApplication(String appcode, String acronym, String appname, String sys_id, String mapstatus,
            String sdg, String status, String statuscode) {
        super();
        this.appcode = appcode;
        this.acronym = acronym;
        this.appname = appname;
        this.sys_id = sys_id;
        this.mapstatus = mapstatus;
        this.sdg = sdg;
        this.status = status;
        this.statuscode = statuscode;
    }




    //Getters

    public String getAppcode() {
        return appcode;
    }

    public String getAcronym() {
        return acronym;
    }

    public String getAppname() {
        return appname;
    }

    public String getSys_id() {
        return sys_id;
    }

    public String getMapstatus() {
        return mapstatus;
    }

    public String getSdg() {
        return sdg;
    }

    public String getStatus() {
        return status;
    }

    public String getStatuscode() {
        return statuscode;
    }




    //Setters

    public void setAppcode(String appcode) {
        this.appcode = appcode;
    }

    public void setAcronym(String acronym) {
        this.acronym = acronym;
    }

    public void setAppname(String appname) {
        this.appname = appname;
    }

    public void setSys_id(String sys_id) {
        this.sys_id = sys_id;
    }

    public void setMapstatus(String mapstatus) {
        this.mapstatus = mapstatus;
    }

    public void setSdg(String sdg) {
        this.sdg = sdg;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setStatuscode(String statuscode) {
        this.statuscode = statuscode;
    }


}


@Repository
public class Employeedao {
    @Autowired
    private EntityManager em;

    /**
     * Method to fetch all employees from the db.
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<AVSApplication> getAllEmployees() {
        return em.createNamedStoredProcedureQuery("procedure-one").getResultList();
    }

}

@RestController
public class Employeecontroller {


   @Autowired
   Employeedao edao;



   /**
    * Method to fetch all employees from the db.
    * @return
    */
   @RequestMapping(value= "/getall")
   public void getAll() {
      System.out.println("All objects: " + edao.getAllEmployees());

      System.out.println("Get the first item in list: " + edao.getAllEmployees().get(0).getAppcode());




   }

}

DAO:

java.lang.ClassCastException: java.base/[Ljava.lang.Object; cannot be cast to com.Mapping.AVSApplication
    at com.Mapping.Employeecontroller.getAll(Employeecontroller.java:33) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~
import java.util.*;

import javax.persistence.*;

@Entity
@NamedStoredProcedureQueries(value= {
        @NamedStoredProcedureQuery(name= "procedure-one", procedureName= "GetAllAppWithStatus")
})


public class AVSApplication implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String appcode;

    private String acronym;

    private String appname;

    private String sys_id;

    private String mapstatus;

    private String sdg;

    private String status;

    private String statuscode;



    //Constructor
    public AVSApplication(String appcode, String acronym, String appname, String sys_id, String mapstatus,
            String sdg, String status, String statuscode) {
        super();
        this.appcode = appcode;
        this.acronym = acronym;
        this.appname = appname;
        this.sys_id = sys_id;
        this.mapstatus = mapstatus;
        this.sdg = sdg;
        this.status = status;
        this.statuscode = statuscode;
    }




    //Getters

    public String getAppcode() {
        return appcode;
    }

    public String getAcronym() {
        return acronym;
    }

    public String getAppname() {
        return appname;
    }

    public String getSys_id() {
        return sys_id;
    }

    public String getMapstatus() {
        return mapstatus;
    }

    public String getSdg() {
        return sdg;
    }

    public String getStatus() {
        return status;
    }

    public String getStatuscode() {
        return statuscode;
    }




    //Setters

    public void setAppcode(String appcode) {
        this.appcode = appcode;
    }

    public void setAcronym(String acronym) {
        this.acronym = acronym;
    }

    public void setAppname(String appname) {
        this.appname = appname;
    }

    public void setSys_id(String sys_id) {
        this.sys_id = sys_id;
    }

    public void setMapstatus(String mapstatus) {
        this.mapstatus = mapstatus;
    }

    public void setSdg(String sdg) {
        this.sdg = sdg;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setStatuscode(String statuscode) {
        this.statuscode = statuscode;
    }


}


@Repository
public class Employeedao {
    @Autowired
    private EntityManager em;

    /**
     * Method to fetch all employees from the db.
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<AVSApplication> getAllEmployees() {
        return em.createNamedStoredProcedureQuery("procedure-one").getResultList();
    }

}

@RestController
public class Employeecontroller {


   @Autowired
   Employeedao edao;



   /**
    * Method to fetch all employees from the db.
    * @return
    */
   @RequestMapping(value= "/getall")
   public void getAll() {
      System.out.println("All objects: " + edao.getAllEmployees());

      System.out.println("Get the first item in list: " + edao.getAllEmployees().get(0).getAppcode());




   }

}


在给定代码中,不会映射存储过程AVSApplication实例返回的行:

@NamedStoredProcedureQueries(value= {
        @NamedStoredProcedureQuery(name= "procedure-one", procedureName= "GetAllAppWithStatus")
})
若存储过程和实体很好地匹配,那个么定义结果类就足够了:

@NamedStoredProcedureQueries(value= {
    @NamedStoredProcedureQuery(
        name= "procedure-one", 
        procedureName= "GetAllAppWithStatus", 
        resultClasses = {AVSApplication.class}
})

如果存在一些差异,则必须定义并参考。

AVSApplication
AQSApplication
?请准确点@查看其AVS应用程序我刚刚在将其写入堆栈溢出时出错。现在错误消息不适用于类!显然,您没有显示正在运行的确切代码!请不要手动输入代码,而是复制粘贴代码和错误消息。这样您就可以确保它是合适的。@Seelenvirtuose它应该是,我只使用了一个名为
AVSApplication
的类。我发布了我所有的代码。@Seelenvirtuose从错误中可以看出,它似乎来自对控制器中的
getAll()
方法的调用。