簡體   English   中英

請幫助我解決 Spring Boot 2 中的錯誤,

[英]Please help me with Error in Spring Boot 2,

我有一個存儲在 sql server 2014 中的過程:

create database db_spring_2
use db_spring_2
create table tb_usuario( id_usuario int primary key identity(1,1), nombre varchar(200), apellidopat varchar(200), apellidomat varchar(200), email varchar(200) unique, foto_url varchar(500) )
insert into tb_usuario values('Marcos','Marcos','MAr','mar@gmail.com', '123.jpg')

go alter proc usp_usuario as begin select nombre from tb_usuario end

我在 Spring boot 2 項目中調用,但出現此錯誤:

 019-08-21 19:51:31.284 ERROR 6752 --- [nio-8080-exec-2] ohengine.jdbc.spi.SqlExceptionHelper : The column name id_usuario is not valid. 2019-08-21 19:51:31.292 ERROR 6752 --- [nio-8080-exec-2] oaccC[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not execute query; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query] with root cause com.microsoft.sqlserver.jdbc.SQLServerException: The column name id_usuario is not valid. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:206) ~[mssql-jdbc-6.1.0.jre8.jar:na] at com.microsoft.sqlserver.jdbc.SQLServerResultSet.findColumn(SQLServerResultSet.java:686) ~[mssql-jdbc-6.1.0.jre8.jar:na] at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(SQLServerResultSet.java:2337) ~[mssql-jdbc-6.1.0.jre8.jar:na] at com.zaxxer.hikari.pool.HikariProxyResultSet.getInt(HikariProxyResultSet.java) ~[HikariCP-3.2.0.jar:na] at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$2.doExtract(IntegerTypeDescriptor.java:62) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:243) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:329) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:793) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]

這里是您調用存儲過程的方法

 package com.example.demo.repository; import java.util.ArrayList; import java.util.List; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; import com.example.demo.entity.Usuarios; @Repository public interface IUsuarioRepository extends CrudRepository<Usuarios, Integer>{ @Query(value = "exec usp_usuario" , nativeQuery=true ) public List<Usuarios> getUsuarios(); }

我的服務界面

 package com.example.demo.repository; import java.util.List; import com.example.demo.entity.Usuarios; public interface IUsuarioService { public List<Usuarios> getUsuarios(); }

還有我的服務

 package com.example.demo.services; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.example.demo.entity.Usuarios; import com.example.demo.repository.IUsuarioRepository; import com.example.demo.repository.IUsuarioService; @Transactional @Service public class UusuarioServices implements IUsuarioService { @Autowired private IUsuarioRepository usuarioR; @Override public List<Usuarios> getUsuarios() { // TODO Auto-generated method stub return usuarioR.getUsuarios(); } }

我的控制器休息

 @RestController @RequestMapping("/usuario") public class UsuarioController { @Autowired private IUsuarioService usuarioS; @GetMapping("/listadoUsuario") public List<Usuarios> getUsuario(){ return usuarioS.getUsuarios(); } }

看起來IUsuarioRepository正在嘗試填充整個Usuarios實體,但您的查詢僅返回了nombre字段。

您尚未顯示所有相關代碼,即Usuarios的定義,但查詢可能應該是select *... not select nombre...當前查詢僅返回單個nombre列,而框架期待返回結果集也包含(至少) id_usuario列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM