简体   繁体   English

请帮助我解决 Spring Boot 2 中的错误,

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

I have a procedure stored in sql server 2014:我有一个存储在 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

I'm calling from in a Spring boot 2 project but I get this error:我在 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]

Here the method where you called the stored procedure这里是您调用存储过程的方法

 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(); }

My Service interface我的服务界面

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

And my service还有我的服务

 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(); } }

My Controller Rest我的控制器休息

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

Looks like IUsuarioRepository is attempting to populate the entire Usuarios entity but your query returned only the nombre field.看起来IUsuarioRepository正在尝试填充整个Usuarios实体,但您的查询仅返回了nombre字段。

You haven't shown all the relevant code, ie the definition of Usuarios , but the query should probably be select *... not select nombre... The current query returns only the single nombre column, while the framework is expecting the returned result set to contain (at a minimum) the id_usuario column as well.您尚未显示所有相关代码,即Usuarios的定义,但查询可能应该是select *... not select nombre...当前查询仅返回单个nombre列,而框架期待返回结果集也包含(至少) id_usuario列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM