【问题标题】:Springboot could not extract ResultSetSpring Boot 无法提取 ResultSet
【发布时间】:2018-09-24 09:58:43
【问题描述】:

目前我在为我的 springboot 项目获取 mysql 数据时遇到问题:

出现意外错误(类型=内部服务器错误,状态=500)。 无法提取结果集; SQL [不适用];嵌套异常是 org.hibernate.exception.SQLGrammarException: could not extract ResultSet

TestEntity.java

@Entity
public class TestEntity implements Serializable {

    @Id
    private int id;
    private String p1;
    private String p2;
    private String p3;

    public TestEntity() {
    }

    public TestEntity(int id, String p1, String p2, String p3){
        this.id = id;
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getP1() {
        return p1;
    }

    public void setP1(String p1) {
        this.p1 = p1;
    }

    public String getP2() {
        return p2;
    }

    public void setP2(String p2) {
        this.p2 = p2;
    }

    public String getP3() {
        return p3;
    }

    public void setP3(String p3) {
        this.p3 = p3;
    }



}

TestService.java

@Service
public class TestService {

    @Autowired
    private TestRepository testRepository;

    public ArrayList<TestEntity> getAllTestEntities(){
       ArrayList<TestEntity> list = new ArrayList();
       testRepository.findAll().forEach(list::add);
       return list;
    }

    public Optional getTestEntity(int id){
       return testRepository.findById(id);
    }
    public void addTestEntity(TestEntity t){
        testRepository.save(t);
    }
    public void removeTestEntity(int index){

        testRepository.deleteById(index);

    }

}

TestRepository.java

@Repository("mysql")
public interface TestRepository extends CrudRepository<TestEntity,Integer> {



}

TestController.java

@RestController
public class TestController {

    @Autowired
    private TestService testService;

    @RequestMapping("/test/AllUnits")
    public ArrayList<TestEntity> getAllTestUnits(){
        return testService.getAllTestEntities();
    }

    @RequestMapping("/test/{id}")
    public Optional getAllTestUnit(@PathVariable int id){
        return testService.getTestEntity(id);
    }

    @RequestMapping(method=RequestMethod.POST,value = "/test" )
    public void addTestUnit(@RequestBody TestEntity t){
        testService.addTestEntity(t);
    }

    @RequestMapping(method=RequestMethod.DELETE,value = "/test/{id}" )
    public void deleteTestUnit(@RequestBody Integer id){
        testService.removeTestEntity(id);
    }

    @RequestMapping("/test/welcome")
    public String welcome(){

        return "welcome to springboot";

    }


}

编辑application.properties

cloud.aws.region.auto=true
cloud.aws.region.static=us-east-2

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://alyxdev.czcdgqfkwsnr.us-east-2.rds.amazonaws.com:3306/CryptoCurrency
spring.datasource.username=*******
spring.datasource.password=*******

我能够使 /test/welcome 映射正常工作,因此我相信我的服务和控制器实现是正确的。所以我想知道我是否在访问存储库中的数据库时出错了,还是应该使用 JpaRepository 而不是 CrudRepository 并使用显式查询?

编辑堆栈跟踪: org.springframework.dao.InvalidDataAccessResourceUsageException:无法提取结果集; SQL [不适用];嵌套异常是 org.hibernate.exception.SQLGrammarException: could not extract ResultSet 引起:org.hibernate.exception.SQLGrammarException:无法提取 ResultSet 原因:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'CryptoCurrency.test_entity'不存在

【问题讨论】:

  • 你能发布完整的堆栈跟踪吗?
  • 这里有一些线索。如果有其他配置文件并检查它。如果您更改了属性,而数据库中的列未更改。
  • 将堆栈跟踪添加到编辑中。
  • 为什么要包装存储库,然后使用原始类型?
  • CryptoCurrency.test_entity真的存在吗?考虑在 Entity 类中使​​用 @Table 注释指定表名。另外,请同时发布application.properties 文件。

标签: java spring-boot jpa


【解决方案1】:

在您的实体类中,即 TestEntity.java,您需要指定您所指的表

@Entity
@Table(name="tbl_something")
public class TestEntity implements Serializable {

并且使用 CrudRepository 可以很好地超出数据库。 application.properties 文件对我来说看起来不错。

【讨论】:

  • 所以我添加了 '@Table("Test_table")' 注释但是我仍然遇到同样的问题。 'com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 表'CryptoCurrency.test_table' 不存在'
  • 表的属性值,即test_table是否与数据库表匹配...很明显,连接池无法连接。表名应该从实体到数据库匹配。
  • 对我来说,我忘了更改表名,所以它指的是错误的表。谢谢!
【解决方案2】:

我通过在 SQL 中将表重命名为所有小写字符 (test_table) 然后使用该表而不是 Test_table 找到了我显然遇到的问题的解决方案@ Springboot 能够找到该表并链接映射它到我的实体类。我不知道为什么它会这样工作。也许与我正在使用的 Netbeans IDE 有关?

【讨论】:

猜你喜欢
  • 2016-07-27
  • 2021-08-01
  • 1970-01-01
  • 2015-08-03
  • 2016-09-28
  • 2018-10-04
  • 2014-08-31
  • 1970-01-01
相关资源
最近更新 更多