我在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','[email protected]', '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] o.h.engine.jdbc.spi.SqlExceptionHelper : The column name id_usuario is not valid.
2019-08-21 19:51:31.292 ERROR 6752 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[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 *...
而不是select nombre...
当前查询仅返回单个nombre
列,而框架希望返回该列结果集也包含(至少)id_usuario
列。